@@ -146,7 +146,8 @@ class ImportAnalyzer(ast.NodeVisitor):
146
146
def __init__ (self , function_names_to_find : set [str ]) -> None :
147
147
self .function_names_to_find = function_names_to_find
148
148
self .found_any_target_function : bool = False
149
- self .imported_modules : set [str ] = set () # Track imported modules for usage analysis
149
+ self .found_qualified_name = None
150
+ self .imported_modules : set [str ] = set ()
150
151
self .has_dynamic_imports : bool = False
151
152
self .wildcard_modules : set [str ] = set ()
152
153
@@ -166,11 +167,13 @@ def visit_Import(self, node: ast.Import) -> None:
166
167
# Check if module itself is a target qualified name
167
168
if module_name in self .function_names_to_find :
168
169
self .found_any_target_function = True
170
+ self .found_qualified_name = module_name
169
171
return
170
172
# Check if any target qualified name starts with this module
171
173
for target_func in self .function_names_to_find :
172
174
if target_func .startswith (f"{ module_name } ." ):
173
175
self .found_any_target_function = True
176
+ self .found_qualified_name = target_func
174
177
return
175
178
176
179
def visit_ImportFrom (self , node : ast .ImportFrom ) -> None :
@@ -195,11 +198,13 @@ def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
195
198
# Check if imported name is a target qualified name
196
199
if alias .name in self .function_names_to_find :
197
200
self .found_any_target_function = True
201
+ self .found_qualified_name = alias .name
198
202
return
199
203
# Check if module.name forms a target qualified name
200
204
qualified_name = f"{ node .module } .{ alias .name } "
201
205
if qualified_name in self .function_names_to_find :
202
206
self .found_any_target_function = True
207
+ self .found_qualified_name = qualified_name
203
208
return
204
209
205
210
def visit_Attribute (self , node : ast .Attribute ) -> None :
@@ -214,12 +219,14 @@ def visit_Attribute(self, node: ast.Attribute) -> None:
214
219
and node .attr in self .function_names_to_find
215
220
):
216
221
self .found_any_target_function = True
222
+ self .found_qualified_name = node .attr
217
223
return
218
224
219
225
# Check if this is accessing a target function through a dynamically imported module
220
226
# Only if we've detected dynamic imports are being used
221
227
if self .has_dynamic_imports and node .attr in self .function_names_to_find :
222
228
self .found_any_target_function = True
229
+ self .found_qualified_name = node .attr
223
230
return
224
231
225
232
self .generic_visit (node )
@@ -235,6 +242,7 @@ def visit_Name(self, node: ast.Name) -> None:
235
242
236
243
if node .id in self .function_names_to_find :
237
244
self .found_any_target_function = True
245
+ self .found_qualified_name = node .id
238
246
return
239
247
240
248
# Check if this name could come from a wildcard import
@@ -243,6 +251,7 @@ def visit_Name(self, node: ast.Name) -> None:
243
251
# Check if target_func is from this wildcard module and name matches
244
252
if target_func .startswith (f"{ wildcard_module } ." ) and target_func .endswith (f".{ node .id } " ):
245
253
self .found_any_target_function = True
254
+ self .found_qualified_name = target_func
246
255
return
247
256
248
257
self .generic_visit (node )
@@ -266,7 +275,11 @@ def analyze_imports_in_test_file(test_file_path: Path | str, target_functions: s
266
275
logger .debug (f"Failed to analyze imports in { test_file_path } : { e } " )
267
276
return True
268
277
else :
269
- return analyzer .found_any_target_function
278
+ if analyzer .found_any_target_function :
279
+ logger .debug (f"Test file { test_file_path } imports target function: { analyzer .found_qualified_name } " )
280
+ return True
281
+ logger .debug (f"Test file { test_file_path } does not import any target functions." )
282
+ return False
270
283
271
284
272
285
def filter_test_files_by_imports (
@@ -288,7 +301,6 @@ def filter_test_files_by_imports(
288
301
logger .debug (f"Target functions for import filtering: { target_functions } " )
289
302
290
303
filtered_map = {}
291
-
292
304
for test_file , test_functions in file_to_test_map .items ():
293
305
should_process = analyze_imports_in_test_file (test_file , target_functions )
294
306
if should_process :
@@ -315,7 +327,6 @@ def discover_unit_tests(
315
327
functions_to_optimize = None
316
328
if file_to_funcs_to_optimize :
317
329
functions_to_optimize = [func for funcs_list in file_to_funcs_to_optimize .values () for func in funcs_list ]
318
-
319
330
function_to_tests , num_discovered_tests = strategy (cfg , discover_only_these_tests , functions_to_optimize )
320
331
return function_to_tests , num_discovered_tests
321
332
0 commit comments