Skip to content

fix(python): don't resolve a bare call in a method to a method - #4933

Merged
safishamsi merged 1 commit into
Graphify-Labs:v8from
nothariharan:fix/python-bare-call-not-enclosing-method
Nov 1, 2026
Merged

safishamsi merged 1 commit into
Graphify-Labs:v8from
nothariharan:fix/python-bare-call-not-enclosing-method

Conversation

@nothariharan

Copy link
Copy Markdown
Contributor

What does this PR do?

In Python, a bare call inside a method body was linked to a method with the same name, usually the enclosing method itself. The call should have been linked to the imported or module-level function it actually names. Python never resolves a bare name to a method. Methods are only reachable through self, cls, or the class. A bare name goes local -> enclosing function -> module globals (including imports) -> builtins.

Minimal repro (v8 at f5155ba, 0.9.100):

# tpl/build_api.py
def get_build_status(c, b):
    return 1

# tpl/main.py
from tpl.build_api import get_build_status

class AsyncTemplate:
    @classmethod
    def get_build_status(cls, b):
        return get_build_status(None, b)

v8 emits a false self-loop:

calls  .get_build_status()@tpl/main.py -> .get_build_status()@tpl/main.py  EXTRACTED

Expected (and emitted with this PR):

calls  .get_build_status()@tpl/main.py -> get_build_status()@tpl/build_api.py  EXTRACTED

The same thing happens with a plain (non-classmethod) method. It also happens with a same-file module function: def helper(): ... at module level plus class C: def helper(self): return helper() linked .helper() to itself. The real-world case where this shows up is the E2B SDK's AsyncTemplate.get_build_status (template_async/main.py), which calls the imported get_build_status.

Root cause. In _extract_generic (graphify/extractors/engine.py), the call walk first resolves a Python bare call through the lexical scope chain (lexical_nids_by_scope), then falls back to the file-wide label_to_nid map. That map is built from every node label, methods included (.get_build_status() normalises to get_build_status). Its entries are overwritten in file order, so a method can also replace a same-named module function. The fallback therefore landed on the method. That is an in-file hit, so the call never reached raw_calls, and the cross-file import resolution never saw it.

Change. While building label_to_nid, also build python_free_label_to_nid: the same map with Python method nodes (targets of method edges) left out. In the Python bare-call branch, if the fallback hit is a method and the call does not sit directly in that method's class body, look the name up in the method-free map instead. With no in-file hit, the call goes to raw_calls, and the existing cross-file resolver binds it through the import. Code that is not Python is untouched. self.m() / cls.m() / super().m() take the _self_call_target branch, which is untouched. A module function that calls itself by bare name still resolves to itself, because it is not a method.

I found no existing issue or PR for this. #2366 is about module-qualified mod.f() calls from methods, which is a different path.

Type of change

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

Verification & Invariants

Invariant: a Python bare call f() made from a method body never resolves to a method. It resolves to a nested function, a module-level definition, or an import, or else it gets no in-file edge.

Kept as before:

  • Genuine module-function recursion (def fact(n): ... fact(n - 1)) still links to itself.
  • self.b() still links to the sibling method.
  • A nested function inside a method still resolves through the lexical scope chain first.

Persisted state: this changes Python extraction output (it removes false self-loop calls edges and adds the correct cross-file/module edges). Cached Python AST extractions from earlier builds keep the old edges until they are re-extracted. I did not bump _AST_CACHE_SCHEMA, because recent fix PRs leave that to the maintainer's batched bump. I can add it here if you prefer.

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

Limitation: a bare call written directly in a class body (x = f() next to def f) can legitimately name the class's own function. The guard leaves that case alone (method_owner[tgt] == caller_nid). On v8, that case produces no edge either before or after this change.

How was this tested?

# Ad-hoc probe (extract(..., parallel=False)) over: classmethod + import, plain method + import,
# same-file module function, nested function in method, module recursion, self.b(), class body, nested function.
# Before: the three method cases produced .X() -> .X() self-loops. After: they link to the module/imported function;
# recursion, self.b(), nested and class-body results are unchanged.

uv run --frozen pytest tests/test_python_bare_call_not_enclosing_method.py -q -p no:cacheprovider
  with fix:            6 passed
  fix reverted (git stash of engine.py): 4 failed, 2 passed
    (the classmethod, plain-method, same-file and nested-in-method tests fail; the recursion and self.b() guard tests pass both ways)

uv run --frozen pytest tests/test_python_*.py tests/test_issue_3405_python_resolution.py tests/test_walk_python_tree_iterative.py -q -p no:cacheprovider
  241 passed

uv run --frozen pytest tests/ -q -p no:cacheprovider -k "python and (call or resolv)"
  214 passed, 12 skipped

uv run --frozen python -m tools.skillgen --check
  check OK: 134 artifact(s) match committed output and expected/.

# Full suite on Linux (WSL kali-linux, clean clone at this commit):
uv sync --all-extras --frozen -q
HOME=$(mktemp -d) uv run --frozen pytest tests/ -q --tb=short -p no:cacheprovider
  9368 passed, 28 skipped, 1 xfailed in 1147s

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments.
  • 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.

A bare `f()` in a Python method body reads locals, enclosing functions,
module globals, then builtins; methods are only reachable through
self/cls/the class. The call walk's bare-call fallback used the file-wide
label_to_nid map, which also holds method names, so a method calling an
imported or module-level function of its own name (`AsyncTemplate.get_build_status`
calling the imported `get_build_status`) got a false EXTRACTED self-loop.

When that fallback lands on a method and the call is not in the body of
that method's class, look the name up in a method-free map instead; with no in-file hit the call
goes to raw_calls and the cross-file resolver binds it through the import.
Module-function recursion and self/cls calls are unchanged.
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

Thanks for the pull request, @nothariharan. 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.10 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 1, 2026

@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.

No issues found by static checks — no coupling regressions or blocking issues in the code graph. That is not the same as safe to merge: see what was not checked.

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 unsupported).

Not verified on this run: \_extract\_generic (unsupported).


Graphify review — findings

Stops bare calls inside Python method bodies from resolving to methods, so get_build_status(...) in a method of the same name now links to the imported or module-level function instead of falsely recursing into the method. The call walk builds a method-free name map for Python files and falls back to it when a bare call would otherwise bind to a method other than the caller. Class-body calls like x = f() next to def f can still resolve to the class's own method, and self.method() calls and genuine module-function recursion resolve as before. New tests cover classmethods, plain methods, same-file shadowing, nested functions in methods, and the unchanged recursion and self paths.

No blocking issues surfaced. 2 lower-confidence candidates did not survive cross-model review.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 3484 functions depend on the 395 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: extract() — 1091 callers, 64 callees

Verification — 3484 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: 1753 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

42 of 488 test file(s) selected (9%) via static blast radius.

  • tests/test_astro_extraction.py — impact
  • tests/test_build.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_csharp_partial_classes.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_ejs_extractor.py — impact
  • tests/test_ext_tagged_id_clash.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_php_closures.py — impact
  • tests/test_import_extension_resolution.py — impact
  • tests/test_indirect_call_block_scoped_shadow.py — impact
  • tests/test_indirect_dispatch.py — impact
  • tests/test_indirect_dispatch_assign_return.py — impact
  • tests/test_indirect_dispatch_getattr.py — impact
  • tests/test_java_member_calls.py — impact
  • tests/test_js_exported_scalar_bindings.py — impact
  • tests/test_js_member_collisions.py — impact
  • tests/test_languages.py — impact
  • tests/test_multilang.py — impact
  • tests/test_php_unparsed_casts.py — impact
  • tests/test_python_bare_call_not_enclosing_method.py — impact, changed-test
  • tests/test_python_inherited_method_calls.py — impact
  • tests/test_python_injected_factory_calls.py — impact
  • tests/test_python_property_reads.py — impact
  • tests/test_python_pytest_fixture_receivers.py — impact
  • tests/test_python_underscore_resolution.py — impact
  • tests/test_rationale.py — impact
  • tests/test_ruby_resolution.py — impact
  • tests/test_scala_context_bounds.py — impact
  • tests/test_scala_extension_methods.py — impact
  • tests/test_scala_given_instances.py — impact
  • tests/test_scala_self_type.py — impact
  • tests/test_scala_top_level_binding.py — impact
  • tests/test_scala_type_definition.py — impact
  • tests/test_svelte_extraction.py — impact
  • tests/test_swift_computed_properties.py — impact
  • tests/test_swift_protocol_requirements.py — impact
  • tests/test_trailing_newline_not_a_syntax_error.py — impact
  • tests/test_ts_new_expression_calls.py — impact
  • tests/test_typescript_module_extensions.py — impact
  • tests/test_vue_extraction.py — impact

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.

Docs that may be stale (advisory)

…and 10 more.

Formal verification

Could not verify: Could not verify \_extract\_generic.

The verifier did not have enough to check \_extract\_generic, 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: parameter `config` is annotated `LanguageConfig` — outside the synthesizable primitive/collection set

· 1 more finding(s) on lines outside this diff (see the check run).

@safishamsi
safishamsi merged commit 87fa551 into Graphify-Labs:v8 Nov 1, 2026
12 checks passed
safishamsi added a commit that referenced this pull request Nov 1, 2026
… fixes

#4933 (python bare-call), #4931 (go method-value via field) and #4932 (go
var-init type refs) were merged on GitHub but each changes AST-cached
output and deferred its schema bump; one combined 33->34 bump so cached
Python/Go extractions re-run. #4929 (namespace packages) is a post-cache
pass and needs none. Opens the unreleased 0.9.101 changelog.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
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.

2 participants