Skip to content

Commit cc77394

Browse files
authored
Merge pull request #1500 from codeflash-ai/codeflash/optimize-pr1498-2026-02-16T20.53.40
⚡️ Speed up function `collect_existing_class_names` by 351% in PR #1498 (`cf-simplify-context-extraction`)
2 parents fadf6d4 + ea14b2f commit cc77394

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

codeflash/languages/python/context/code_context_extractor.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,28 @@ def _parse_and_collect_imports(code_context: CodeStringsMarkdown) -> tuple[ast.M
563563

564564

565565
def collect_existing_class_names(tree: ast.Module) -> set[str]:
566-
return {node.name for node in ast.walk(tree) if isinstance(node, ast.ClassDef)}
566+
class_names = set()
567+
stack = list(tree.body)
568+
569+
while stack:
570+
node = stack.pop()
571+
if isinstance(node, ast.ClassDef):
572+
class_names.add(node.name)
573+
stack.extend(node.body)
574+
elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
575+
stack.extend(node.body)
576+
elif isinstance(node, (ast.If, ast.For, ast.While, ast.With)):
577+
stack.extend(node.body)
578+
if hasattr(node, "orelse"):
579+
stack.extend(node.orelse)
580+
elif isinstance(node, ast.Try):
581+
stack.extend(node.body)
582+
stack.extend(node.orelse)
583+
stack.extend(node.finalbody)
584+
for handler in node.handlers:
585+
stack.extend(handler.body)
586+
587+
return class_names
567588

568589

569590
def enrich_testgen_context(code_context: CodeStringsMarkdown, project_root_path: Path) -> CodeStringsMarkdown:

0 commit comments

Comments
 (0)