Resolve absolute imports under the src/ layout (and recognize Sphinx conf.py)#621
Open
willfrey wants to merge 2 commits into
Open
Resolve absolute imports under the src/ layout (and recognize Sphinx conf.py)#621willfrey wants to merge 2 commits into
willfrey wants to merge 2 commits into
Conversation
The Python import resolver only tried <scan_root>/<pkg> and <project_root>/<pkg> when resolving an absolute import such as `from pkg.sub import x`. In a src/ layout (the PyPA-recommended packaging layout) the package lives at <root>/src/pkg, which was never tried, so the edge resolved to nothing: the imported module was recorded with zero importers and misreported as orphaned, and every other graph-based detector (coupling, single-use, facade) silently lost those edges too. Factor the candidate roots into candidate_source_roots() and add the src/ variants, tried after the existing flat roots. The change is strictly additive: any import that resolved before resolves to the same file; only previously-unresolved src/-layout imports gain an edge.
docs/conf.py is loaded by sphinx-build and has zero importers by design, so it was misreported as an orphaned file. Add conf.py to PY_ENTRY_PATTERNS alongside the other framework entry points (manage.py, wsgi.py, settings.py).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Two false positives in orphaned-file detection (and the import graph that feeds coupling / single-use / facade detection) for standard Python project structures.
1. Absolute imports don't resolve under the
src/layoutresolve_absolute_importonly tries<scan_root>/<pkg>and<project_root>/<pkg>. In the PyPA-recommendedsrc/layout the importable package lives at<root>/src/<pkg>, which is never tried. So an absolute import likefrom pkg.sub import xresolves to nothing, the imported module is recorded with zero importers, and:Relative imports (
from .sub import x) were unaffected, which is why this only bites packages imported via their absolute path. On one real-worldsrc/-layout project, scanning produced ~70 false findings that disappeared once edges resolved (6 bogus "orphaned file" reports plus ~64 downstream across the other detectors).2. Sphinx
conf.pyis flagged as orphaneddocs/conf.pyis loaded bysphinx-buildand has zero importers by design, but it was not inPY_ENTRY_PATTERNS.Fix
candidate_source_roots()and add thesrc/variants, tried after the existing flat roots. The change is strictly additive: any import that resolved before resolves to the same file; only previously-unresolvedsrc/-layout imports gain an edge. Duplicate roots (common when scan root == project root) are collapsed.conf.pytoPY_ENTRY_PATTERNS, alongside the existingmanage.py/wsgi.py/settings.pyframework entry points.Tests
test_absolute_import_resolves_under_src— constructs asrc/-layout package and asserts the imported module'simporter_count >= 1. Red before the fix (importer_count == 0), green after.test_sphinx_conf_py_excluded_by_python_entry_patterns— feeds the realPY_ENTRY_PATTERNSthroughdetect_orphaned_filesand asserts adocs/conf.pyis not flagged while a genuine dead file still is.