Repository navigation
fix(extract): bind plain-imported module member calls to function nodes (#5253) - #5258
madhusudanpatnaik wants to merge 1 commit into
Conversation
|
Thanks for the pull request, @madhusudanpatnaik. A maintainer will review it soon. Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions. A couple of things that speed up review: make sure the test suite passes on Python 3.11 and 3.13, and that the change keeps extraction deterministic. |
FolatheDuckofDuckingburg
left a comment
There was a problem hiding this comment.
Review: Looks good overall
This PR addresses a well-defined extraction issue where calls through plainly imported modules (import b; b.f()) were incorrectly bound to the module's file node rather than the actual function node.
The proposed fix is sensible. Resolving unambiguous bare module imports before language-specific member-call resolution allows the existing resolver to identify the correct function definition without introducing a separate resolution mechanism.
A few things I particularly appreciate:
- Correct resolution ordering: Performing
_repoint_python_bare_module_imports()immediately after sibling repointing addresses the underlying ordering problem rather than attempting to repair incorrect edges downstream. - Conservative resolution: Ignoring ambiguous module stems and standard-library names, while protecting against self-imports, helps prevent incorrect local bindings.
- Regression coverage: The tests cover the original reproduction, aliased imports, ambiguous module names, and standard-library collisions.
- Graph integrity: Verifying that
build_from_json()preserves the resolved function-level edge is particularly important, since the original issue involved downstream alias-index rewriting. - Scope control: The implementation appears appropriately scoped to the reported bug while preserving the existing Cause A guard.
One area I would particularly encourage validating is import resolution across different directory layouts and Python versions, especially where multiple modules share a stem or where package and non-package imports coexist. The standard-library exclusion and ambiguity handling are important safeguards that should remain stable as extraction evolves.
The reported test results (650 passed, 1 xfailed), lint checks, and type checks are encouraging.
Overall: This looks like a well-motivated and reasonably contained bug fix. The resolution-order change addresses the actual source of the incorrect graph edge, and the regression tests provide useful coverage. I support moving this forward, subject to the remaining CI checks and maintainer review.
There was a problem hiding this comment.
Graphify reviewed this change.
Worth a look — the grounded gate found no coupling regressions or blocking issues, but 1 advisory finding(s) below merit a look before merge.
Not checked: tests were not run; formal verification proved 0 of 1 changed function(s) (1 not verified).
Formal verification. PR-changed functions: 0/1 verified (0 proven, 0 may-equivalent, 0 distinguished) · 1 not verified (1 vacuous).
Not verified on this run: extract (vacuous: never exercised).
Graphify review — findings
Adds a pre-resolver pass, _repoint_python_bare_module_imports, that rewrites Python import b edges from a bare stem to the unique matching .py file node when exactly one file claims that stem. This lets member calls like b.f() bind to the module's function node instead of the file node, and the pass skips stdlib names, ambiguous stems, self-imports, and scan-root importers whose target lives only in a subdirectory. Because the match is on stem alone, any unique local file with a matching name can capture an import, including third-party packages and dotted imports whose flattened id happens to match.
Worth a look
- Bare-stem repoint captures third-party imports that share a name with any unique local file —
graphify/extract.py:760· Escalate · medium- NOT verified (no proof, no reproducing execution) — needs human review
Analysis details — impact, health, verification
Impact & health
Graphify review
Impact — 4788 functions depend on the 411 functions this change touches.
Health — this change adds coupling hotspots:
- worse:
extract()— 1262 callers, 79 callees - worse:
build_from_json()— 321 callers, 25 callees
Verification — 4788 functions in the blast radius were not formally verified this run (proofs are advisory here).
Gate & verification
graphify gate
PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.
Advisory (not blocking):
- verification_scope: 4211 function(s) in the blast radius were not formally verified this run
Test selection
Test selection
335 of 582 test file(s) selected (58%) via static blast radius.
tests/test_4874_jvm_resolution.py— impacttests/test_4978_reconcile_cost.py— impacttests/test_4992_ts_import_types.py— impacttests/test_affected_overrides.py— impacttests/test_alias_folding.py— impacttests/test_analyze.py— impacttests/test_astro_extraction.py— impacttests/test_astro_import_ids.py— impacttests/test_blade_extractor.py— impacttests/test_build.py— impacttests/test_build_located_semantic_identity.py— impacttests/test_build_merge_dedup_scope.py— impacttests/test_build_merge_ghost_fold_4955.py— impacttests/test_build_merge_hyperedges_and_prune.py— impacttests/test_build_merge_shrink_guard.py— impacttests/test_builtin_global_type_refs.py— impacttests/test_c_include_resolution.py— impacttests/test_cache.py— impacttests/test_carried_hyperedge_remap.py— impacttests/test_case_only_decl_ids_4904.py— impacttests/test_case_sensitive_resolution.py— impacttests/test_chunking.py— impacttests/test_cjs_module_extension.py— impacttests/test_cli_export.py— impacttests/test_cluster.py— impacttests/test_cobol_extractor.py— impacttests/test_confidence.py— impacttests/test_confidence_score_output.py— impacttests/test_corrupt_graph_json.py— impacttests/test_cpp_alias_member_resolution.py— impacttests/test_cpp_anonymous_namespace_receivers.py— impacttests/test_cpp_conditional_initializers.py— impacttests/test_cpp_forward_declarations.py— impacttests/test_cpp_forward_receiver_completion.py— impacttests/test_cpp_inherited_member_resolution.py— impacttests/test_cpp_local_callable_shadowing.py— impacttests/test_cpp_member_call_by_receiver_type.py— impacttests/test_cpp_member_declaration_identity.py— impacttests/test_cpp_method_declarations.py— impacttests/test_cpp_nested_and_cli.py— impacttests/test_cpp_objc_cross_file_calls.py— impacttests/test_cpp_receiver_chains.py— impacttests/test_cpp_receiver_parameters.py— impacttests/test_cpp_split_header_owner.py— impacttests/test_cpp_template_members.py— impacttests/test_cpp_template_type_params.py— impacttests/test_cpp_type_alias.py— impacttests/test_cpp_underscore_ids.py— impacttests/test_cross_extension_reexport_self_cycle.py— impacttests/test_cross_language_call_resolution.py— impact- … and 285 more
Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.
Formal verification
Could not verify: Could not verify extract.
The verifier did not have enough to check extract, so it is saying so rather than guessing. No false assurance is the whole point.
Guarantee: No guarantee either way, this is an honest abstention, not a pass.
Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 90 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly TypeError — names the real obstacle, not a sampling gap)
· 2 more finding(s) on lines outside this diff (see the check run).
…es (Graphify-Labs#5253) When a Python file in a subdirectory plainly imports a module from another directory added via sys.path (e.g. `sys.path.insert(0, '../../bin')` then `import b` and `b.f()`), the import edge initially targeted the bare stem ("b"). Because the directory is not a package and not a sibling, neither `_repoint_python_package_imports` nor `_repoint_python_sibling_imports` repointed it. In `_resolve_python_member_calls`, because "b" was not in `node_by_id`, the module arm missed the import, falling into the external plain import fallback and emitting a coarse call to the module stem itself (`calls: other() -> b`). Later in `build.py`, `_alias_index` rewrote bare endpoints across all edges to canonical file nodes, turning `calls: other() -> b` into `calls: other() -> bin_b`. Every caller of a helper module collapsed onto the file node instead of the member function (`b.f`), breaking affected analysis. Fix: 1. Extract dynamic `sys.path` modifications (`sys.path.insert` and `sys.path.append`) in `_extract_python_rationale()` and preserve them in per-file extraction data. 2. In `extract()`, add `_repoint_python_sys_path_imports()` to repoint plain imports targeting modules in directories added to `sys.path` to their canonical file node IDs before language resolvers run. Downstream member call resolution finds the module file node and binds `b.f()` directly to the function definition node. 3. In `build.py` (`finalize_raw_graph_endpoints` and `build_from_json`), guard `alias_to_id` so that `calls` and `indirect_call` edges are never repointed to file nodes.
546e1f7 to
53633bf
Compare
What does this PR do?
Fixes #5253 where a call on a plainly-imported module (
import bthenb.f()) whereb.pylives in a non-package directory outside the caller's directory (e.g.src/bin/b.pyimported asimport bfromsrc/skills/s/scripts/c.py) bound the call edge to the file node (b.py), rather than the function node (b.f()).Cause
"b") because neither package repointing nor sibling repointing applies._resolve_python_member_calls, because"b"is not yet innode_by_id, the module arm does not match it. The call fell into the external plain import fallback, emitting a coarse call to the module stem itself (calls: other() -> b).build.py,_alias_indexre-pointed"b"across all edges to the file nodebin_b, convertingcalls: other() -> bintocalls: other() -> bin_b. This collapsed callers onto the file node rather than the member definition, breaking affected/caller analysis.Solution
_repoint_python_bare_module_imports()inextract()right after sibling repointing, resolving unambiguous bare module import endpoints to their unique file node before language resolvers run._resolve_python_member_callsruns, the module is present inimported_by_filenode, allowing the existing module arm to resolveb.f()directly to the member function node (f()).mod.func()calls edges — 177/177 unresolved on a real repo (explains #2428 §3's unexplained 42%) #2447): bare imports at scan root targeting files only in subdirectories do not bind._PYTHON_STDLIB_MODULE_NAMES) so local files sharing stdlib names cannot capture stdlib imports.Type of change
How was this tested?
tests/test_plain_import_member_calls.pycovering:test_plain_import_member_call_binds_to_function_node: verifies exact reproduction case from [Bug]:import b; b.f()binds the call to the file node, not tob.f#5253, confirmingcalls: other() -> f()withconfidence=EXTRACTEDandconfidence_score=1.0, and confirmingbuild_from_json()maintains the call edge tof().test_plain_import_with_alias_member_call: verifies plain imports withasalias (import b as helper; helper.f()).test_plain_import_ambiguous_stem_does_not_bind: verifies ambiguous module names fail closed.test_plain_import_stdlib_module_does_not_capture_local_file: verifies stdlib module names (e.g.import logging) do not bind to local files.graphify update .AST update runs cleanly.Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (No fragments modified)