Skip to content

⚡️ Speed up method ImportAnalyzer.visit_Attribute by 11% in PR #310 (test-filter-cleanup) #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions codeflash/discovery/discover_unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,24 +212,24 @@ def visit_Attribute(self, node: ast.Attribute) -> None:
if self.found_any_target_function:
return

# Check if this is accessing a target function through an imported module
if (
isinstance(node.value, ast.Name)
and node.value.id in self.imported_modules
and node.attr in self.function_names_to_find
):
fnames = self.function_names_to_find
imported_mods = self.imported_modules

# Fast path: imported module + target function
val = node.value
if isinstance(val, ast.Name) and val.id in imported_mods and node.attr in fnames:
self.found_any_target_function = True
self.found_qualified_name = node.attr
return

# Check if this is accessing a target function through a dynamically imported module
# Only if we've detected dynamic imports are being used
if self.has_dynamic_imports and node.attr in self.function_names_to_find:
# Dynamic import fast path
if self.has_dynamic_imports and node.attr in fnames:
self.found_any_target_function = True
self.found_qualified_name = node.attr
return

self.generic_visit(node)
# Still need to traverse for deeply nested attr
ast.NodeVisitor.generic_visit(self, node)

def visit_Name(self, node: ast.Name) -> None:
"""Handle direct name usage like target_function()."""
Expand Down Expand Up @@ -260,7 +260,7 @@ def generic_visit(self, node: ast.AST) -> None:
"""Override generic_visit to stop traversal if a target function is found."""
if self.found_any_target_function:
return
super().generic_visit(node)
ast.NodeVisitor.generic_visit(self, node)


def analyze_imports_in_test_file(test_file_path: Path | str, target_functions: set[str]) -> bool:
Expand Down
Loading