mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-24 18:24:20 +00:00
commitb12a9892b7
Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Wed Apr 2 08:09:56 2025 -0700 fix(utils.py): don't modify openai_token_counter commit294de31803
Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Mon Mar 24 21:22:40 2025 -0700 fix: fix linting error commitcb6e9fbe40
Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Mon Mar 24 19:52:45 2025 -0700 refactor: complete migration commitbfc159172d
Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Mon Mar 24 19:09:59 2025 -0700 refactor: refactor more constants commit43ffb6a558
Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Mon Mar 24 18:45:24 2025 -0700 fix: test commit04dbe4310c
Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Mon Mar 24 18:28:58 2025 -0700 refactor: refactor: move more constants into constants.py commit3c26284aff
Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Mon Mar 24 18:14:46 2025 -0700 refactor: migrate hardcoded constants out of __init__.py commitc11e0de69d
Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Mon Mar 24 18:11:21 2025 -0700 build: migrate all constants into constants.py commit7882bdc787
Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Mon Mar 24 18:07:37 2025 -0700 build: initial test banning hardcoded numbers in repo
152 lines
3.3 KiB
Python
152 lines
3.3 KiB
Python
import sys
|
|
import ast
|
|
import os
|
|
|
|
# Extremely restrictive set of allowed numbers
|
|
ALLOWED_NUMBERS = {
|
|
0,
|
|
1,
|
|
-1,
|
|
2,
|
|
10,
|
|
100,
|
|
1000,
|
|
4,
|
|
3,
|
|
500,
|
|
6,
|
|
60,
|
|
3600,
|
|
0.75,
|
|
7,
|
|
1024,
|
|
1011,
|
|
600,
|
|
12,
|
|
1000000000.0,
|
|
0.1,
|
|
50,
|
|
128,
|
|
6000,
|
|
30,
|
|
1000000,
|
|
5,
|
|
15,
|
|
25,
|
|
10000,
|
|
60000,
|
|
8,
|
|
2048,
|
|
16000000000,
|
|
16,
|
|
16383,
|
|
14,
|
|
24,
|
|
128000,
|
|
0.01,
|
|
20,
|
|
}
|
|
|
|
# Add all standard HTTP status codes
|
|
HTTP_STATUS_CODES = {
|
|
200, # OK
|
|
201, # Created
|
|
202, # Accepted
|
|
204, # No Content
|
|
300, # Multiple Choices
|
|
301, # Moved Permanently
|
|
302, # Found
|
|
303, # See Other
|
|
304, # Not Modified
|
|
307, # Temporary Redirect
|
|
308, # Permanent Redirect
|
|
400, # Bad Request
|
|
401, # Unauthorized
|
|
402, # Payment Required
|
|
403, # Forbidden
|
|
404, # Not Found
|
|
406, # Not Acceptable
|
|
408, # Request Timeout
|
|
409, # Conflict
|
|
413, # Payload Too Large
|
|
422, # Unprocessable Entity
|
|
424, # Failed Dependency
|
|
429, # Too Many Requests
|
|
498, # Invalid Token
|
|
499, # Client Closed Request
|
|
500, # Internal Server Error
|
|
501, # Not Implemented
|
|
502, # Bad Gateway
|
|
503, # Service Unavailable
|
|
504, # Gateway Timeout
|
|
520, # Web server is returning an unknown error
|
|
522, # Connection timed out
|
|
524, # A timeout occurred
|
|
529, # Site is overloaded
|
|
}
|
|
|
|
# Combine the sets
|
|
ALLOWED_NUMBERS = ALLOWED_NUMBERS.union(HTTP_STATUS_CODES)
|
|
|
|
|
|
class HardcodedNumberFinder(ast.NodeVisitor):
|
|
def __init__(self):
|
|
self.hardcoded_numbers = []
|
|
|
|
def visit_Constant(self, node):
|
|
# For Python 3.8+
|
|
if isinstance(node.value, (int, float)) and node.value not in ALLOWED_NUMBERS:
|
|
self.hardcoded_numbers.append((node.lineno, node.value))
|
|
self.generic_visit(node)
|
|
|
|
def visit_Num(self, node):
|
|
# For older Python versions
|
|
if node.n not in ALLOWED_NUMBERS:
|
|
self.hardcoded_numbers.append((node.lineno, node.n))
|
|
self.generic_visit(node)
|
|
|
|
|
|
def check_file(filename):
|
|
try:
|
|
with open(filename, "r") as f:
|
|
content = f.read()
|
|
|
|
tree = ast.parse(content)
|
|
finder = HardcodedNumberFinder()
|
|
finder.visit(tree)
|
|
|
|
if finder.hardcoded_numbers:
|
|
print(f"ERROR in {filename}: Hardcoded numbers detected:")
|
|
for line, value in finder.hardcoded_numbers:
|
|
print(f" Line {line}: {value}")
|
|
return 1
|
|
return 0
|
|
except SyntaxError:
|
|
print(f"Syntax error in {filename}")
|
|
return 0
|
|
|
|
|
|
def main():
|
|
exit_code = 0
|
|
folder = "../../litellm"
|
|
ignore_files = [
|
|
"constants.py",
|
|
"proxy_cli.py",
|
|
"token_counter.py",
|
|
"mock_functions.py",
|
|
"duration_parser.py",
|
|
"utils.py",
|
|
]
|
|
ignore_folder = "types"
|
|
for root, dirs, files in os.walk(folder):
|
|
for filename in files:
|
|
if filename.endswith(".py") and filename not in ignore_files:
|
|
full_path = os.path.join(root, filename)
|
|
if ignore_folder in full_path:
|
|
continue
|
|
exit_code |= check_file(full_path)
|
|
sys.exit(exit_code)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|