1313def extract_dependent_function (main_function : str , code_context : CodeOptimizationContext ) -> str | Literal [False ]:
1414 """Extract the single dependent function from the code context excluding the main function."""
1515 dependent_functions = set ()
16+
17+ # Compare using bare name since AST extracts bare function names
18+ bare_main = main_function .rsplit ("." , 1 )[- 1 ] if "." in main_function else main_function
19+
1620 for code_string in code_context .testgen_context .code_strings :
17- ast_tree = ast . parse ( code_string . code )
18- dependent_functions . update (
19- { node . name for node in ast_tree . body if isinstance ( node , ( ast . FunctionDef , ast . AsyncFunctionDef ))}
20- )
21+ # Quick heuristic: skip parsing entirely if there is no 'def' token,
22+ # since no function definitions can be present without it.
23+ if "def" not in code_string . code :
24+ continue
2125
22- if main_function in dependent_functions :
23- dependent_functions .discard (main_function )
26+ ast_tree = ast .parse (code_string .code )
27+ # Add function names directly, skipping the bare main name.
28+ for node in ast_tree .body :
29+ if isinstance (node , (ast .FunctionDef , ast .AsyncFunctionDef )):
30+ name = node .name
31+ if name == bare_main :
32+ continue
33+ dependent_functions .add (name )
34+ # If more than one dependent function (other than the main) is found,
35+ # we can return False early since the final result cannot be a single name.
36+ if len (dependent_functions ) > 1 :
37+ return False
2438
2539 if not dependent_functions :
2640 return False
@@ -32,6 +46,9 @@ def extract_dependent_function(main_function: str, code_context: CodeOptimizatio
3246
3347
3448def build_fully_qualified_name (function_name : str , code_context : CodeOptimizationContext ) -> str :
49+ # If the name is already qualified (contains a dot), return as-is
50+ if "." in function_name :
51+ return function_name
3552 full_name = function_name
3653 for obj_name , parents in code_context .preexisting_objects :
3754 if obj_name == function_name :
0 commit comments