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
31 changes: 9 additions & 22 deletions codeflash/languages/python/context/code_context_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ def _parse_and_collect_imports(code_context: CodeStringsMarkdown) -> tuple[ast.M

def collect_existing_class_names(tree: ast.Module) -> set[str]:
class_names = set()
stack = [tree]
stack: list[ast.AST] = [tree]

while stack:
node = stack.pop()
Expand All @@ -586,27 +586,14 @@ def collect_existing_class_names(tree: ast.Module) -> set[str]:
class_names.add(node.name)

# Only traverse nodes that can contain ClassDef nodes
if isinstance(
node,
(
ast.Module,
ast.ClassDef,
ast.FunctionDef,
ast.AsyncFunctionDef,
ast.If,
ast.For,
ast.AsyncFor,
ast.While,
ast.With,
ast.AsyncWith,
ast.Try,
ast.ExceptHandler,
),
):
stack.extend(getattr(node, "body", []))
stack.extend(getattr(node, "orelse", []))
stack.extend(getattr(node, "finalbody", []))
stack.extend(getattr(node, "handlers", []))
if hasattr(node, "body"):
stack.extend(node.body)
if hasattr(node, "orelse"):
stack.extend(node.orelse)
if hasattr(node, "finalbody"):
stack.extend(node.finalbody)
if hasattr(node, "handlers"):
stack.extend(node.handlers)

return class_names

Expand Down
Loading