Repository navigation
Conversation
The absolute-import walk-up offered every ancestor directory as a candidate sys.path root, including directories holding an `__init__.py`. Python puts a package's PARENT on sys.path, never the package itself, so a package directory must not be a root. Two consequences followed. A module inside a package that shadows a stdlib or third-party name (`logging`, `types`, `json`, `email`, `queue`, ...) resolved to the in-tree file instead of the real module, attracting a false in-tree edge from every sibling that imported the genuine stdlib module. And a module could resolve to itself: `import logging` inside `service/app/logging.py` produced the edge `service/app/logging.py -> service/app/logging.py`. Both are now excluded: a candidate root carrying `__init__.py` is skipped, and a match equal to the importing file is dropped so the walk continues to a shallower root. The multi-service case the walk exists for is unaffected \u2014 those service directories carry no `__init__.py`, and the parent of a skipped package is still a candidate, so `from app.logging import ...` keeps resolving to the in-tree module.
This branch has not been deployed
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.
Summary
resolvePythonImport()walks up from the importer's directory and tries each ancestor as a candidatesys.pathroot, first match wins. The walk treated every ancestor as a root candidate, including directories that contain an__init__.py.A directory with an
__init__.pyis a package, and Python puts a package's parent onsys.path— never the package itself. Offering one as a root produces two wrong edges:service/app/__init__.pyandservice/app/logging.py, the bareimport logginginsideservice/app/logging.pyresolved to the in-tree file, so every sibling importing the real stdlibloggingpicked up a false in-tree edge.service/app/logging.pygot the edgeservice/app/logging.py → service/app/logging.py. Separately,import svc.mainfromsrc/svc/main.pyreached itself whenever an enclosing namespace directory was a root candidate.Closes #621.
Changes
In
understand-anything-plugin/skills/understand/extract-import-map.mjs, the absolute-import branch ofresolvePythonImport():__init__.py;The two rules are independent: the first covers a package under a real root, the second covers the PEP 420 namespace-package case where there is no
__init__.pyto key on.The multi-service case is preserved
This is the case the walk-up exists for, and both rules leave it alone:
src/emailservice/carries no__init__.py, so it stays a valid implicit root andimport demo_pb2_grpcstill resolves tosrc/emailservice/demo_pb2_grpc.py. The existing regression test for it is unchanged and still passes. A skipped package's parent is still a candidate on the next iteration, so the genuinefrom app.logging import get_loggeredge survives.Verification
Two new regression tests in
tests/skill/understand/test_extract_import_map.test.mjs, both of which fail onmainand pass with this change:does not treat a package directory as a sys.path root— assertsservice/app/logging.pygets no edges for its bareimport logging, and thatservice/app/main.pystill resolves toservice/app/logging.py.never resolves a python import to the importing file— assertsimport svc.mainfromsrc/svc/main.pyis not a self-edge while the realimport helperssibling edge is kept.Before the fix, the first test observed the self-edge directly:
After:
All 69 tests in the file pass, including the 67 pre-existing ones.
--testTimeoutis only needed on a loaded machine: the suite spawns anodesubprocess per case, and the default 5 s per-test timeout can trip on an unrelated case.Notes
types.py,json.py,email.py,queue.py) — those are the same defect and are covered by the same rule, since the skip keys on__init__.pyrather than on any module name.