@@ -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+
19622029def test_async_generators_and_coroutines ():
19632030 """Test detection with async generators and coroutines."""
19642031 temp_dir = Path (tempfile .mkdtemp ())
0 commit comments