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
4 changes: 3 additions & 1 deletion codeflash/context/code_context_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,12 @@ def get_function_sources_from_jedi(
and definition.full_name
and definition.type == "function"
and not belongs_to_function_qualified(definition, qualified_function_name)
# Avoid nested functions or classes. Only class.function is allowed
and len((qualified_name := get_qualified_name(definition.module_name, definition.full_name)).split(".")) <= 2
):
function_source = FunctionSource(
file_path=definition_path,
qualified_name=get_qualified_name(definition.module_name, definition.full_name),
qualified_name=qualified_name,
fully_qualified_name=definition.full_name,
only_function_name=definition.name,
source_code=definition.get_line_code(),
Expand Down
10 changes: 10 additions & 0 deletions tests/test_code_context_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ def innocent_bystander(self):
def helper_method(self):
return self.name

class NestedClass:
def __init__(self, name):
self.name = name

def nested_method(self):
return self.name

def main_method():
return "hello"
Expand All @@ -33,6 +39,7 @@ def __init__(self, name):
self.name = name

def main_method(self):
self.name = HelperClass.NestedClass("test").nested_method()
return HelperClass(self.name).helper_method()


Expand Down Expand Up @@ -73,6 +80,8 @@ def test_code_replacement10() -> None:
)

code_ctx = get_code_optimization_context(function_to_optimize=func_top_optimize, project_root_path=file_path.parent)
qualified_names = {func.qualified_name for func in code_ctx.helper_functions}
assert qualified_names == {"HelperClass.helper_method"} # Nested method should not be in here
read_write_context, read_only_context = code_ctx.read_writable_code, code_ctx.read_only_context_code

expected_read_write_context = """
Expand All @@ -91,6 +100,7 @@ def __init__(self, name):
self.name = name

def main_method(self):
self.name = HelperClass.NestedClass("test").nested_method()
return HelperClass(self.name).helper_method()
"""
expected_read_only_context = """
Expand Down
Loading