Skip to content

Commit b74f4cc

Browse files
committed
fix
Signed-off-by: Saurabh Misra <misra.saurabh1@gmail.com>
1 parent 690e7b7 commit b74f4cc

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

codeflash/context/unused_definition_remover.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ def detect_unused_helper_functions(
679679
imported_names_map = _analyze_imports_in_optimized_code(optimized_ast, code_context)
680680

681681
# Extract all function calls in the entrypoint function
682-
called_function_names = set()
682+
called_function_names = {function_to_optimize.function_name}
683683
for node in ast.walk(entrypoint_function_ast):
684684
if isinstance(node, ast.Call):
685685
if isinstance(node.func, ast.Name):

tests/test_unused_helper_revert.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,73 @@ async def async_entrypoint(n):
19591959
shutil.rmtree(temp_dir, ignore_errors=True)
19601960

19611961

1962+
def test_recursive_helper_function_not_detected_as_unused():
1963+
"""Test that recursive helper functions are NOT incorrectly detected as unused."""
1964+
temp_dir = Path(tempfile.mkdtemp())
1965+
1966+
try:
1967+
# Main file with recursive helper function
1968+
main_file = temp_dir / "main.py"
1969+
main_file.write_text("""
1970+
def gcd_recursive(a: int, b: int) -> int:
1971+
\"\"\"Calculate greatest common divisor using Euclidean algorithm with recursion.\"\"\"
1972+
if b == 0:
1973+
return a
1974+
return gcd_recursive(b, a % b)
1975+
""")
1976+
1977+
# Optimized version that still uses the recursive helper
1978+
optimized_code = """
1979+
```python:main.py
1980+
def gcd_recursive(a: int, b: int) -> int:
1981+
\"\"\"Calculate greatest common divisor using Euclidean algorithm with recursion.\"\"\"
1982+
if b == 0:
1983+
return a
1984+
return gcd_recursive(b, a % b)
1985+
```
1986+
"""
1987+
1988+
# Create test config
1989+
test_cfg = TestConfig(
1990+
tests_root=temp_dir / "tests",
1991+
tests_project_rootdir=temp_dir,
1992+
project_root_path=temp_dir,
1993+
test_framework="pytest",
1994+
pytest_cmd="pytest",
1995+
)
1996+
1997+
# Create FunctionToOptimize instance
1998+
function_to_optimize = FunctionToOptimize(
1999+
file_path=main_file, function_name="gcd_recursive", parents=[]
2000+
)
2001+
2002+
# Create function optimizer
2003+
optimizer = FunctionOptimizer(
2004+
function_to_optimize=function_to_optimize,
2005+
test_cfg=test_cfg,
2006+
function_to_optimize_source_code=main_file.read_text(),
2007+
)
2008+
2009+
# Get original code context
2010+
ctx_result = optimizer.get_code_optimization_context()
2011+
assert ctx_result.is_successful(), f"Failed to get context: {ctx_result.failure()}"
2012+
2013+
code_context = ctx_result.unwrap()
2014+
2015+
# Test unused helper detection
2016+
unused_helpers = detect_unused_helper_functions(optimizer.function_to_optimize, code_context, CodeStringsMarkdown.parse_markdown_code(optimized_code))
2017+
2018+
# Should NOT detect gcd_recursive as unused
2019+
unused_names = {uh.qualified_name for uh in unused_helpers}
2020+
2021+
assert "gcd_recursive" not in unused_names, f"Recursive function gcd_recursive should NOT be detected as unused, but got unused: {unused_names}"
2022+
2023+
finally:
2024+
# Cleanup
2025+
import shutil
2026+
shutil.rmtree(temp_dir, ignore_errors=True)
2027+
2028+
19622029
def test_async_generators_and_coroutines():
19632030
"""Test detection with async generators and coroutines."""
19642031
temp_dir = Path(tempfile.mkdtemp())

0 commit comments

Comments
 (0)