mirror of
https://github.com/tiennm99/litellm.git
synced 2026-08-03 14:23:28 +00:00
Squashed commit of the following: (#9709)
commit b12a9892b750f6f838b1049110626261732c8804 Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Wed Apr 2 08:09:56 2025 -0700 fix(utils.py): don't modify openai_token_counter commit 294de3180391f4bb36dcf3ffdd80a0cd387003eb Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Mon Mar 24 21:22:40 2025 -0700 fix: fix linting error commit cb6e9fbe402dacfde36d9d28f872483818ac1f12 Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Mon Mar 24 19:52:45 2025 -0700 refactor: complete migration commit bfc159172dbfffcd9fee47153c554a59cc2758c7 Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Mon Mar 24 19:09:59 2025 -0700 refactor: refactor more constants commit 43ffb6a5583cc3da927042c158ff543af552d2ac Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Mon Mar 24 18:45:24 2025 -0700 fix: test commit 04dbe4310cd588c326194e8e36a0a31cdf0e61fc Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Mon Mar 24 18:28:58 2025 -0700 refactor: refactor: move more constants into constants.py commit 3c26284affeffcec6dd904fdb7ecd3a3d5724660 Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Mon Mar 24 18:14:46 2025 -0700 refactor: migrate hardcoded constants out of __init__.py commit c11e0de69d13f6a97ffed5733cf6008fcdd8e0ee Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Mon Mar 24 18:11:21 2025 -0700 build: migrate all constants into constants.py commit 7882bdc787b21e8e0b1800dd5c1180bbe808d228 Author: Krrish Dholakia <krrishdholakia@gmail.com> Date: Mon Mar 24 18:07:37 2025 -0700 build: initial test banning hardcoded numbers in repo
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user