fix: honor pyproject-declared source roots in Python import resolution#623
Open
jkri-ch wants to merge 1 commit into
Open
fix: honor pyproject-declared source roots in Python import resolution#623jkri-ch wants to merge 1 commit into
jkri-ch wants to merge 1 commit into
Conversation
Projects that keep importable code in a subdirectory of the repo root (e.g. scripts/ run with PYTHONPATH=scripts) previously had every absolute import fail to resolve: the dependency graph reported 0 importers for every module and the test-coverage mapper flagged fully-tested modules as 'Untested module (N LOC, 0 importers)' across the board. resolve_absolute_import and the test-coverage import-spec mapper now also try source roots declared in pyproject.toml: - [tool.desloppify] python_source_roots (explicit override) - [tool.pytest.ini_options] pythonpath - [tool.mypy] mypy_path Existing resolution order is preserved (scan root, then project root, then declared roots); src/ layout support is unchanged.
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
Projects that keep importable code in a subdirectory of the repo root (e.g.
scripts/run withPYTHONPATH=scripts, declared via[tool.pytest.ini_options] pythonpath = ["scripts"]) get completely broken import resolution:resolve_absolute_importonly tries the scan root and the project root, soimport riot_hydrationnever resolves toscripts/riot_hydration/__init__.py— the dependency graph reports 0 importers for every module.languages/python/test_coverage.py::resolve_import_spec) only tries root-relative andsrc/-prefixed candidates, so everyfrom mypkg.store import ...in a test file fails to map — every production module is flagged "Untested module (N LOC, 0 importers)" even when it has a dedicated multi-thousand-line test file.Observed on a real ~115k-LOC project (
desloppify scan --path .): 205 open test_coverage findings, ~80% of which were false positives, dragging Test health to 37% — independently adjudicated as detector noise by two blind review rounds.Fix
New
desloppify/languages/python/source_roots.pywith a cacheddeclared_source_roots(project_root)that reads source roots frompyproject.toml:[tool.desloppify] python_source_roots(explicit override)[tool.pytest.ini_options] pythonpath[tool.mypy] mypy_path(absolute paths,
..traversal, and.are dropped; duplicates removed)resolve_absolute_importnow tries scan root → project root → declared roots (existing order preserved).resolve_import_specextends its layout-prefix list (src/) with"<root>/"for each declared root.Verification
desloppify/languages/python/tests/test_py_source_roots.py(declaration parsing, resolution precedence, spec mapping,src/regression).declared roots: ('scripts',);riot_hydration.storeresolves toscripts/riot_hydration/store.py; test-spec mapping fortests/unit/riot_hydration/test_riot_hydration_store.py→scripts/riot_hydration/store.py.🤖 Generated with Claude Code