Skip to content

Commit 845574f

Browse files
committed
Enhance ImportAnalyzer to support src/ root file imports
This commit updates the ImportAnalyzer class to include checks for files located at the src/ root directory, such as _globals.py, when building subfolders. It ensures that these files are correctly identified and included in the import analysis, improving the handling of imports from subfolders. Additionally, new tests are added to verify the functionality of importing files from the src/ root, ensuring robustness in the build process for subfolder packages.
1 parent c7999d7 commit 845574f

7 files changed

Lines changed: 2700 additions & 4415 deletions

src/python_package_folder/analyzer.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,15 @@ def resolve_local_import(self, import_info: ImportInfo, src_dir: Path) -> Path |
310310
self.project_root / module_path_str / "__init__.py",
311311
(self.project_root / module_path_str).with_suffix(".py"),
312312
]
313+
314+
# Also check project_root/src/ for files at src/ root (like _globals.py)
315+
# This is important when building subfolders where src_dir points to temp directory
316+
src_base = self.project_root / "src"
317+
if src_base.exists():
318+
potential_paths.extend([
319+
src_base / module_path_str / "__init__.py",
320+
(src_base / module_path_str).with_suffix(".py"),
321+
])
313322

314323
for path in potential_paths:
315324
if path.exists():
@@ -335,6 +344,13 @@ def resolve_local_import(self, import_info: ImportInfo, src_dir: Path) -> Path |
335344
potential_file = parent / f"{module_name.split('.')[-1]}.py"
336345
if potential_file.exists():
337346
return potential_file
347+
348+
# Also check project_root/src/ for files at src/ root
349+
# This handles cases like _globals.py at src/_globals.py when building subfolders
350+
if parent == self.project_root and src_base.exists():
351+
src_file = src_base / f"{module_name.split('.')[-1]}.py"
352+
if src_file.exists():
353+
return src_file
338354

339355
# Check all subdirectories in parent (not just common ones)
340356
# This handles cases like src/data/spreadsheet_creation/spreadsheet_formatting_dataclasses.py

0 commit comments

Comments
 (0)