Merge branch 'main' into pytest-discovery-optimization

This commit is contained in:
Alvin Ryanputra 2025-04-01 18:59:18 -04:00 committed by GitHub
commit 2e18cf517b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 1 deletions

View file

@ -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(),

View file

@ -23,6 +23,12 @@ class HelperClass:
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"
@ -33,6 +39,7 @@ class MainClass:
self.name = name
def main_method(self):
self.name = HelperClass.NestedClass("test").nested_method()
return HelperClass(self.name).helper_method()
@ -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 = """
@ -91,6 +100,7 @@ class MainClass:
self.name = name
def main_method(self):
self.name = HelperClass.NestedClass("test").nested_method()
return HelperClass(self.name).helper_method()
"""
expected_read_only_context = """