Repository navigation
fix(python): don't resolve a bare call in a method to a method - #4933
safishamsi merged 1 commit into
Conversation
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.
|
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. |
There was a problem hiding this comment.
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— impacttests/test_build.py— impacttests/test_cjs_module_extension.py— impacttests/test_cpp_nested_and_cli.py— impacttests/test_csharp_partial_classes.py— impacttests/test_dotnet.py— impacttests/test_ejs_extractor.py— impacttests/test_ext_tagged_id_clash.py— impacttests/test_extract.py— impacttests/test_extract_php_closures.py— impacttests/test_import_extension_resolution.py— impacttests/test_indirect_call_block_scoped_shadow.py— impacttests/test_indirect_dispatch.py— impacttests/test_indirect_dispatch_assign_return.py— impacttests/test_indirect_dispatch_getattr.py— impacttests/test_java_member_calls.py— impacttests/test_js_exported_scalar_bindings.py— impacttests/test_js_member_collisions.py— impacttests/test_languages.py— impacttests/test_multilang.py— impacttests/test_php_unparsed_casts.py— impacttests/test_python_bare_call_not_enclosing_method.py— impact, changed-testtests/test_python_inherited_method_calls.py— impacttests/test_python_injected_factory_calls.py— impacttests/test_python_property_reads.py— impacttests/test_python_pytest_fixture_receivers.py— impacttests/test_python_underscore_resolution.py— impacttests/test_rationale.py— impacttests/test_ruby_resolution.py— impacttests/test_scala_context_bounds.py— impacttests/test_scala_extension_methods.py— impacttests/test_scala_given_instances.py— impacttests/test_scala_self_type.py— impacttests/test_scala_top_level_binding.py— impacttests/test_scala_type_definition.py— impacttests/test_svelte_extraction.py— impacttests/test_swift_computed_properties.py— impacttests/test_swift_protocol_requirements.py— impacttests/test_trailing_newline_not_a_syntax_error.py— impacttests/test_ts_new_expression_calls.py— impacttests/test_typescript_module_extensions.py— impacttests/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)
ARCHITECTURE.md§ Extraction output schema (lines 56-81): references changed symbolsnameCHANGELOG.md§ 0.9.97 (2026-10-27) (lines 61-86): references changed symbolsnameCHANGELOG.md§ 0.9.95 (2026-10-23) (lines 118-143): references changed symbolsnameCHANGELOG.md§ 0.9.93 (2026-10-20) (lines 166-192): references changed symbolsnameCHANGELOG.md§ 0.9.88 (2026-10-12) (lines 265-289): references changed symbolsnameCHANGELOG.md§ 0.9.87 (2026-10-12) (lines 290-312): references changed symbolsnameCHANGELOG.md§ 0.9.85 (2026-10-10) (lines 335-357): references changed symbolsnameCHANGELOG.md§ 0.9.83 (2026-10-09) (lines 372-386): references changed symbolsnameCHANGELOG.md§ 0.9.81 (2026-10-08) (lines 393-411): references changed symbolsnameCHANGELOG.md§ 0.9.76 (2026-10-04) (lines 472-490): references changed symbolsname
…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).
… 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]>
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):
v8 emits a false self-loop:
Expected (and emitted with this PR):
The same thing happens with a plain (non-classmethod) method. It also happens with a same-file module function:
def helper(): ...at module level plusclass C: def helper(self): return helper()linked.helper()to itself. The real-world case where this shows up is the E2B SDK'sAsyncTemplate.get_build_status(template_async/main.py), which calls the importedget_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-widelabel_to_nidmap. That map is built from every node label, methods included (.get_build_status()normalises toget_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 reachedraw_calls, and the cross-file import resolution never saw it.Change. While building
label_to_nid, also buildpython_free_label_to_nid: the same map with Python method nodes (targets ofmethodedges) 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 toraw_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_targetbranch, 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
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:
def fact(n): ... fact(n - 1)) still links to itself.self.b()still links to the sibling method.Persisted state: this changes Python extraction output (it removes false self-loop
callsedges 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.Limitation: a bare call written directly in a class body (
x = f()next todef 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?
Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments.