mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-25 18:54:30 +00:00
Litellm router code coverage 3 (#6274)
* refactor(router.py): move assistants api endpoints to using 1 pass-through factory function Reduces code, increases testing coverage * refactor(router.py): reduce _common_check_available_deployment function size make code more maintainable - reduce possible errors * test(router_code_coverage.py): include batch_utils + pattern matching in enforced 100% code coverage Improves reliability * fix(router.py): fix model id match model dump
This commit is contained in:
parent
891e9001b5
commit
e22e8d24ef
8 changed files with 407 additions and 244 deletions
|
@ -75,29 +75,28 @@ def get_functions_from_router(file_path):
|
|||
|
||||
ignored_function_names = [
|
||||
"__init__",
|
||||
"_acreate_file",
|
||||
"_acreate_batch",
|
||||
"acreate_assistants",
|
||||
"adelete_assistant",
|
||||
"aget_assistants",
|
||||
"acreate_thread",
|
||||
"aget_thread",
|
||||
"a_add_message",
|
||||
"aget_messages",
|
||||
"arun_thread",
|
||||
"try_retrieve_batch",
|
||||
]
|
||||
|
||||
|
||||
def main():
|
||||
router_file = "./litellm/router.py" # Update this path if it's located elsewhere
|
||||
# router_file = "../../litellm/router.py" ## LOCAL TESTING
|
||||
router_file = [
|
||||
"./litellm/router.py",
|
||||
"./litellm/router_utils/batch_utils.py",
|
||||
"./litellm/router_utils/pattern_match_deployments.py",
|
||||
]
|
||||
# router_file = [
|
||||
# "../../litellm/router.py",
|
||||
# "../../litellm/router_utils/pattern_match_deployments.py",
|
||||
# "../../litellm/router_utils/batch_utils.py",
|
||||
# ] ## LOCAL TESTING
|
||||
tests_dir = (
|
||||
"./tests/" # Update this path if your tests directory is located elsewhere
|
||||
)
|
||||
# tests_dir = "../../tests/" # LOCAL TESTING
|
||||
|
||||
router_functions = get_functions_from_router(router_file)
|
||||
router_functions = []
|
||||
for file in router_file:
|
||||
router_functions.extend(get_functions_from_router(file))
|
||||
print("router_functions: ", router_functions)
|
||||
called_functions_in_tests = get_all_functions_called_in_tests(tests_dir)
|
||||
untested_functions = [
|
||||
|
|
66
tests/code_coverage_tests/router_enforce_line_length.py
Normal file
66
tests/code_coverage_tests/router_enforce_line_length.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
import ast
|
||||
import os
|
||||
|
||||
MAX_FUNCTION_LINES = 100
|
||||
|
||||
|
||||
def get_function_line_counts(file_path):
|
||||
"""
|
||||
Extracts all function names and their line counts from a given Python file.
|
||||
"""
|
||||
with open(file_path, "r") as file:
|
||||
tree = ast.parse(file.read())
|
||||
|
||||
function_line_counts = []
|
||||
|
||||
for node in tree.body:
|
||||
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
|
||||
# Top-level functions
|
||||
line_count = node.end_lineno - node.lineno + 1
|
||||
function_line_counts.append((node.name, line_count))
|
||||
elif isinstance(node, ast.ClassDef):
|
||||
# Functions inside classes
|
||||
for class_node in node.body:
|
||||
if isinstance(class_node, (ast.FunctionDef, ast.AsyncFunctionDef)):
|
||||
line_count = class_node.end_lineno - class_node.lineno + 1
|
||||
function_line_counts.append((class_node.name, line_count))
|
||||
|
||||
return function_line_counts
|
||||
|
||||
|
||||
ignored_functions = [
|
||||
"__init__",
|
||||
]
|
||||
|
||||
|
||||
def check_function_lengths(router_file):
|
||||
"""
|
||||
Checks if any function in the specified file exceeds the maximum allowed length.
|
||||
"""
|
||||
function_line_counts = get_function_line_counts(router_file)
|
||||
long_functions = [
|
||||
(name, count)
|
||||
for name, count in function_line_counts
|
||||
if count > MAX_FUNCTION_LINES and name not in ignored_functions
|
||||
]
|
||||
|
||||
if long_functions:
|
||||
print("The following functions exceed the allowed line count:")
|
||||
for name, count in long_functions:
|
||||
print(f"- {name}: {count} lines")
|
||||
raise Exception(
|
||||
f"{len(long_functions)} functions in {router_file} exceed {MAX_FUNCTION_LINES} lines"
|
||||
)
|
||||
else:
|
||||
print("All functions in the router file are within the allowed line limit.")
|
||||
|
||||
|
||||
def main():
|
||||
# Update this path to point to the correct location of router.py
|
||||
router_file = "../../litellm/router.py" # LOCAL TESTING
|
||||
|
||||
check_function_lengths(router_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue