Skip to content

Commit a41a465

Browse files
committed
Refactor module file search logic in ImportAnalyzer
This commit enhances the ImportAnalyzer class by implementing a recursive search for module files in nested directories, ensuring that the search is limited to the project root and its subdirectories. It also improves the exclusion logic for certain patterns, preventing unnecessary searches in specific directories. This refactor streamlines the process of locating module files, making it more efficient and reliable.
1 parent 2a69d6e commit a41a465

1 file changed

Lines changed: 22 additions & 45 deletions

File tree

src/python_package_folder/analyzer.py

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -330,56 +330,33 @@ def resolve_local_import(self, import_info: ImportInfo, src_dir: Path) -> Path |
330330

331331
# Check all subdirectories in parent (not just common ones)
332332
# This handles cases like src/data/spreadsheet_creation/spreadsheet_formatting_dataclasses.py
333-
if parent.is_dir():
333+
# Use recursive search to find modules in nested directories
334+
if parent.is_dir() and parent.is_relative_to(self.project_root):
335+
# Recursively search for the module file in subdirectories
336+
# Limit search to project_root and its subdirectories to avoid searching too broadly
337+
module_basename = module_name.split(".")[-1]
334338
try:
335-
for subdir in parent.iterdir():
336-
if not subdir.is_dir():
339+
# Search recursively for the module file
340+
for potential_file in parent.rglob(f"{module_basename}.py"):
341+
# Only search within project_root to avoid going too far
342+
if not potential_file.is_relative_to(self.project_root):
337343
continue
338-
# Skip common excluded patterns
339-
if subdir.name.startswith("_SS") or subdir.name.startswith("__SS"):
340-
continue
341-
# Check if module file exists directly in subdirectory
342-
potential_subdir_file = subdir / f"{module_name.split('.')[-1]}.py"
343-
if potential_subdir_file.exists():
344-
return potential_subdir_file
345-
# Check if module directory exists in subdirectory
346-
potential_subdir_module = subdir / module_name.replace(".", "/")
347-
if (
348-
potential_subdir_module.is_dir()
349-
and (potential_subdir_module / "__init__.py").exists()
344+
# Skip excluded patterns
345+
if any(
346+
part.startswith("_SS")
347+
or part.startswith("__SS")
348+
or part.startswith("_sandbox")
349+
or part.startswith("__sandbox")
350+
for part in potential_file.parts
350351
):
351-
return potential_subdir_module / "__init__.py"
352-
if potential_subdir_module.with_suffix(".py").is_file():
353-
return potential_subdir_module.with_suffix(".py")
354-
# Check nested subdirectories (e.g., data/spreadsheet_creation)
355-
# Recursively check subdirectories up to 2 levels deep
356-
try:
357-
for nested_subdir in subdir.iterdir():
358-
if not nested_subdir.is_dir():
359-
continue
360-
# Check if module file exists in nested subdirectory
361-
potential_nested_file = (
362-
nested_subdir / f"{module_name.split('.')[-1]}.py"
363-
)
364-
if potential_nested_file.exists():
365-
return potential_nested_file
366-
# Check if module directory exists in nested subdirectory
367-
potential_nested_module = nested_subdir / module_name.replace(
368-
".", "/"
369-
)
370-
if (
371-
potential_nested_module.is_dir()
372-
and (potential_nested_module / "__init__.py").exists()
373-
):
374-
return potential_nested_module / "__init__.py"
375-
if potential_nested_module.with_suffix(".py").is_file():
376-
return potential_nested_module.with_suffix(".py")
377-
except (OSError, PermissionError):
378-
# Skip nested directories we can't read
379352
continue
353+
# Skip if it's in the src_dir (we're looking for external dependencies)
354+
if potential_file.is_relative_to(src_dir):
355+
continue
356+
return potential_file
380357
except (OSError, PermissionError):
381-
# Skip directories we can't read
382-
continue
358+
# Skip if we can't read the directory
359+
pass
383360

384361
# Check common subdirectories in parent (e.g., _shared, shared, common)
385362
# This handles cases like src/_shared/better_enum.py

0 commit comments

Comments
 (0)