fix(parsers/python): match imports by module components in both directions#116
Open
gadievron wants to merge 1 commit into
Open
fix(parsers/python): match imports by module components in both directions#116gadievron wants to merge 1 commit into
gadievron wants to merge 1 commit into
Conversation
…tions
`_resolve_import` Strategy 2 bound an imported function to a file whose dotted
module path string-`endswith` the imported module (either direction). That crosses
component boundaries: `from utils import helper` matched `extra_utils.py`
(`'extra_utils'.endswith('utils')`) — a false call-graph edge to the wrong module
that no Python layout can produce.
Fix: match by dotted COMPONENTS, in EITHER direction — the imported module is a
component-suffix of the file path (a repo-root prefix on the file, `src/pkg/auth.py`
for `from pkg.auth import ...`), OR the file path is a component-suffix of the
imported module (the repo-IS-the-package self-import, `auth.py` == `myapp/auth.py`
recorded shallow via relative_to(repo_path), for `from myapp.auth import ...`).
Both are real layouts and are KEPT; only the substring-crossing matches are dropped.
An adversarial review caught that an earlier forward-only version dropped the
reverse direction (repo-is-package self-imports) — a recall regression. Because the
reachability filter keeps only reachable units, a dropped real edge silently removes
attack surface; so an edge that COULD be real (e.g. `from pkg.auth import login` to a
top-level `auth.py`, structurally identical to a self-import) is kept, not forbidden.
Net: strictly fewer false edges than master, never fewer real ones.
Tests (tests/parsers/python/test_call_graph_import_resolution.py): 2 precision drops
(substring-crossing `extra_utils`/`utils`, `extra_auth`/`auth`) + 6 recall guards
(exact, repo-root-prefix, top-level, repo-is-package self-import, nested reverse-prefix,
relative). Full python suite: 12 passed.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Collaborator
Author
|
Merge-order note (not a defect — flagging for landing order)
Whichever lands second will conflict on that one |
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.
Root cause
_resolve_importStrategy 2 (parsers/python/call_graph_builder.py) bound an imported function to a file whose dotted module path string-endswiththe imported module (in either direction). That crosses component boundaries:from utils import helpermatchedextra_utils.py:helperbecause'extra_utils'.endswith('utils'), andfrom auth import loginmatchedextra_auth.py:login. No Python module layout produces those — they are false call-graph edges to the wrong module, and they inflate the reachable set with units that are never really called.Fix
Match by dotted components, in either direction:
src/pkg/auth.pyforfrom pkg.auth import ...); orauth.py==myapp/auth.pyrecorded shallow viarelative_to(repo_path), forfrom myapp.auth import ...).Both are real layouts and are kept. Only the substring-crossing matches are dropped. Relative/bare imports (
from . import x, no module) keep the prior name-only behavior.Reachability / recall safety
An adversarial review caught that an earlier forward-only version dropped the reverse direction (repo-is-package self-imports) — a recall regression. Because the reachability filter keeps only reachable units, a dropped real edge silently removes attack surface. So an edge that could be real —
from pkg.auth import loginto a top-levelauth.py, which is structurally identical to a self-import and indistinguishable from the data — is kept, not forbidden. Net: strictly fewer false edges than master, never fewer real ones.Regression test
tests/parsers/python/test_call_graph_import_resolution.py— RED→GREEN:extra_utils/utils,extra_auth/auth.Full Python suite: 12 passed.
Compatibility
None. Resolution only; the change is strictly more precise on false edges and recall-preserving on real ones.
Coordination with open PRs
This edits the
_resolve_importStrategy-2 block inparsers/python/call_graph_builder.py, which #72 also reworks (it adds aseenparam for__init__.pyre-exports) and #112 also touches. Neither fixes this bug — both still carry the baremodule_path.endswith(expected_module) or expected_module.endswith(module_path). Expect a merge conflict in that block; happy to rebase onto whichever of #72/#112 lands first.