Skip to content

fix(extract): bind plain-imported module member calls to function nodes (#5253) - #5258

Open
madhusudanpatnaik wants to merge 1 commit into
Graphify-Labs:v8from
madhusudanpatnaik:fix/python-plain-import-call-target
Open

madhusudanpatnaik wants to merge 1 commit into
Graphify-Labs:v8from
madhusudanpatnaik:fix/python-plain-import-call-target

Conversation

@madhusudanpatnaik

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #5253 where a call on a plainly-imported module (import b then b.f()) where b.py lives in a non-package directory outside the caller's directory (e.g. src/bin/b.py imported as import b from src/skills/s/scripts/c.py) bound the call edge to the file node (b.py), rather than the function node (b.f()).

Cause

  1. During AST extraction, plain imports targeting modules across non-package directory boundaries are initially left targeting the bare stem ("b") because neither package repointing nor sibling repointing applies.
  2. In _resolve_python_member_calls, because "b" is not yet in node_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).
  3. Later in build.py, _alias_index re-pointed "b" across all edges to the file node bin_b, converting calls: other() -> b into calls: other() -> bin_b. This collapsed callers onto the file node rather than the member definition, breaking affected/caller analysis.

Solution

  • Added _repoint_python_bare_module_imports() in extract() right after sibling repointing, resolving unambiguous bare module import endpoints to their unique file node before language resolvers run.
  • When _resolve_python_member_calls runs, the module is present in imported_by_filenode, allowing the existing module arm to resolve b.f() directly to the member function node (f()).
  • Preserves the Cause A guard (Python: three independent causes drop 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.
  • Ignores Python standard library module names (_PYTHON_STDLIB_MODULE_NAMES) so local files sharing stdlib names cannot capture stdlib imports.
  • Ignores ambiguous module stems (multiple local files with the same name).
  • Protects against self-imports.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Performance
  • Security fix

How was this tested?

  • Added tests/test_plain_import_member_calls.py covering:
    • 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 to b.f #5253, confirming calls: other() -> f() with confidence=EXTRACTED and confidence_score=1.0, and confirming build_from_json() maintains the call edge to f().
    • test_plain_import_with_alias_member_call: verifies plain imports with as alias (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.
  • Ran test suite:
    pytest tests/test_plain_import_member_calls.py tests/test_extract.py tests/test_python*.py -q
    # 650 passed, 1 xfailed
    
  • Checked linter and types:
    ruff check graphify/extract.py tests/test_plain_import_member_calls.py
    # All checks passed!
    pyright tests/test_plain_import_member_calls.py
    # 0 errors, 0 warnings, 0 informations
    python -m tools.skillgen --check
    # check OK: 134 artifact(s) match committed output and expected/.
    
  • Verified graphify update . AST update runs cleanly.

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (No fragments modified)
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

@github-actions

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Nov 16, 2026

@FolatheDuckofDuckingburg FolatheDuckofDuckingburg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 — impact
  • tests/test_4978_reconcile_cost.py — impact
  • tests/test_4992_ts_import_types.py — impact
  • tests/test_affected_overrides.py — impact
  • tests/test_alias_folding.py — impact
  • tests/test_analyze.py — impact
  • tests/test_astro_extraction.py — impact
  • tests/test_astro_import_ids.py — impact
  • tests/test_blade_extractor.py — impact
  • tests/test_build.py — impact
  • tests/test_build_located_semantic_identity.py — impact
  • tests/test_build_merge_dedup_scope.py — impact
  • tests/test_build_merge_ghost_fold_4955.py — impact
  • tests/test_build_merge_hyperedges_and_prune.py — impact
  • tests/test_build_merge_shrink_guard.py — impact
  • tests/test_builtin_global_type_refs.py — impact
  • tests/test_c_include_resolution.py — impact
  • tests/test_cache.py — impact
  • tests/test_carried_hyperedge_remap.py — impact
  • tests/test_case_only_decl_ids_4904.py — impact
  • tests/test_case_sensitive_resolution.py — impact
  • tests/test_chunking.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cli_export.py — impact
  • tests/test_cluster.py — impact
  • tests/test_cobol_extractor.py — impact
  • tests/test_confidence.py — impact
  • tests/test_confidence_score_output.py — impact
  • tests/test_corrupt_graph_json.py — impact
  • tests/test_cpp_alias_member_resolution.py — impact
  • tests/test_cpp_anonymous_namespace_receivers.py — impact
  • tests/test_cpp_conditional_initializers.py — impact
  • tests/test_cpp_forward_declarations.py — impact
  • tests/test_cpp_forward_receiver_completion.py — impact
  • tests/test_cpp_inherited_member_resolution.py — impact
  • tests/test_cpp_local_callable_shadowing.py — impact
  • tests/test_cpp_member_call_by_receiver_type.py — impact
  • tests/test_cpp_member_declaration_identity.py — impact
  • tests/test_cpp_method_declarations.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_cpp_objc_cross_file_calls.py — impact
  • tests/test_cpp_receiver_chains.py — impact
  • tests/test_cpp_receiver_parameters.py — impact
  • tests/test_cpp_split_header_owner.py — impact
  • tests/test_cpp_template_members.py — impact
  • tests/test_cpp_template_type_params.py — impact
  • tests/test_cpp_type_alias.py — impact
  • tests/test_cpp_underscore_ids.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/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.
@madhusudanpatnaik
madhusudanpatnaik force-pushed the fix/python-plain-import-call-target branch from 546e1f7 to 53633bf Compare November 16, 2026 16:02

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: import b; b.f() binds the call to the file node, not to b.f

2 participants