Skip to content
Merged
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
23 changes: 22 additions & 1 deletion codeflash/languages/python/context/code_context_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,28 @@ def _parse_and_collect_imports(code_context: CodeStringsMarkdown) -> tuple[ast.M


def collect_existing_class_names(tree: ast.Module) -> set[str]:
return {node.name for node in ast.walk(tree) if isinstance(node, ast.ClassDef)}
class_names = set()
stack = list(tree.body)

while stack:
node = stack.pop()
if isinstance(node, ast.ClassDef):
class_names.add(node.name)
stack.extend(node.body)
elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
stack.extend(node.body)
elif isinstance(node, (ast.If, ast.For, ast.While, ast.With)):
stack.extend(node.body)
if hasattr(node, "orelse"):
stack.extend(node.orelse)
elif isinstance(node, ast.Try):
stack.extend(node.body)
stack.extend(node.orelse)
stack.extend(node.finalbody)
for handler in node.handlers:
stack.extend(handler.body)

return class_names


def enrich_testgen_context(code_context: CodeStringsMarkdown, project_root_path: Path) -> CodeStringsMarkdown:
Expand Down
Loading