Skip to content
Merged
Show file tree
Hide file tree
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
21 changes: 18 additions & 3 deletions codeflash/languages/python/context/unused_definition_remover.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ def detect_unused_helper_functions(
# First, analyze imports to build a mapping of imported names to their original qualified names
imported_names_map = _analyze_imports_in_optimized_code(optimized_ast, code_context)

# Extract all function calls in the entrypoint function
# Extract all function calls and attribute references in the entrypoint function
called_function_names = {function_to_optimize.function_name}
for node in ast.walk(entrypoint_function_ast):
if isinstance(node, ast.Call):
Expand All @@ -794,7 +794,6 @@ def detect_unused_helper_functions(
# self.method_name() -> add both method_name and ClassName.method_name
called_function_names.add(attr_name)
# For class methods, also add the qualified name
# For class methods, also add the qualified name
if hasattr(function_to_optimize, "parents") and function_to_optimize.parents:
class_name = function_to_optimize.parents[0].name
called_function_names.add(f"{class_name}.{attr_name}")
Expand All @@ -807,9 +806,25 @@ def detect_unused_helper_functions(
if mapped_names:
called_function_names.update(mapped_names)
# Handle nested attribute access like obj.attr.method()
# Handle nested attribute access like obj.attr.method()
else:
called_function_names.add(node.func.attr)
elif isinstance(node, ast.Attribute) and isinstance(node.value, ast.Name):
# Attribute reference without call: e.g. self._parse1 = self._parse_literal
# This covers methods used as callbacks, stored in variables, passed as arguments, etc.
attr_name = node.attr
value_id = node.value.id
if value_id == "self":
called_function_names.add(attr_name)
if hasattr(function_to_optimize, "parents") and function_to_optimize.parents:
class_name = function_to_optimize.parents[0].name
called_function_names.add(f"{class_name}.{attr_name}")
else:
called_function_names.add(attr_name)
full_ref = f"{value_id}.{attr_name}"
called_function_names.add(full_ref)
mapped_names = imported_names_map.get(full_ref)
if mapped_names:
called_function_names.update(mapped_names)

logger.debug(f"Functions called in optimized entrypoint: {called_function_names}")
logger.debug(f"Imported names mapping: {imported_names_map}")
Expand Down
Loading
Loading