Repository navigation
fix(python): do not link calls made through a function parameter (#5418) - #5419
Deepjyoti-Sarmah wants to merge 5 commits into
Conversation
|
Thanks for the pull request, @Deepjyoti-Sarmah. 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. |
The import resolver needs it too, and the engine already imports the resolver.
A bare call whose name is a parameter of an enclosing function, own or captured, has an unknown target. Check that before the same-file lookup and before saving a raw call.
This pass walks function bodies itself and linked calls to an imported name even when a parameter hid it.
Cover own and captured parameters, rebinding scopes that keep their edge, and the cross-file path.
Cached Python extractions still hold the false edges.
There was a problem hiding this comment.
Graphify review 
5 possible issues to check · High impact
The change suppresses bare Python call links when the name refers to an enclosing function parameter.
5 possible issues we couldn't confirm
- Typed
*argsand**kwargsparameters are not treated as shadowing names, so calls through them can still link to module functions ·graphify/extractors/python_scope.py:31 - Lambda parameters and locally assigned variables do not block linking calls to module functions ·
graphify/extractors/python_scope.py:68 - Parameter-shadow checker receives only
sourceand may re-parse the file for every Python extraction ·graphify/extractors/engine.py:8001 - +2 more in Graphify
They don't change the score.
What this review covered
- Some lookups asked for files or lines that do not exist; the review was told and carried on.
- 5 of 5 changed files inspected. This is scope, not a correctness guarantee.
Code graph · 5448 functions depend on this change
- 3 functions became more coupled.
extract()(1351 callers),_extract_generic()(21 callers),_collect_python_symbol_resolution_facts()(2 callers). - 621 of 621 test files are affected by this change.
- Gate, test list and every flagged function: in the Graphify check.
Reviewed f95d86a · 9 earlier findings not re-checked · Full report in the Graphify check
| continue # A nearer import shadows the module binding. | ||
| call["_python_import_binding"] = imported_names[call["callee"]] | ||
|
|
||
| param_shadowed = _python_param_shadow_checker(source) |
There was a problem hiding this comment.
Captured parameter calls can still link to an imported function
A nested function that calls a parameter captured from its outer function can still get a cross-file call edge to a same-named module import.
Exclude calls shadowed by captured parameters from import binding in _collect_python_symbol_resolution_facts.
Evidence · checked against the code
Expected: The call should remain unresolved because the captured parameter shadows the import.
Actually: The raw call may receive the import binding and resolve to a same-named function in the imported file, even though the use-fact path skips it.
- Independent source review: Read the new test for a captured parameter with a same-named import; it asserts that only the unshadowed call links across files. The test reaches the claimed scenario, but no test outcome was available to establish whether it passes or fails. The inspected raw-call binding and resolution path still presents the claimed route.
Prompt to fix with AI
In graphify/extractors/resolution.py, _collect_python_symbol_resolution_facts (line 3500):
Problem: Captured parameter calls can still link to an imported function
Expected: The call should remain unresolved because the captured parameter shadows the import.
Actually: The raw call may receive the import binding and resolve to a same-named function in the imported file, even though the use-fact path skips it.
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
cf353c0 to
f95d86a
Compare
Bump _AST_CACHE_SCHEMA 54 -> 55 for the Python function-parameter call change (#5418), so cached Python extractions re-run; its walk_calls guard was combined with the 0.9.119 lambda-parameter guard (both suppress a bare Python call under a shadowing binding). Harden the #5414 go-fold test with a node-count assert so it fails without the fix. The wiki/affected/build changes need no schema bump. 0.9.120 changelog and version bump. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Landed in v0.9.119 via an authorship-preserving cherry-pick. A bare call through a function parameter (own or captured from an outer function) no longer binds to a module-level definition. Maintainer tweak on merge: your walk_calls guard was combined with the lambda-parameter guard from #5417 (both suppress a shadowed bare Python call), and the schema was reconciled to the next free number. Complements #5417. Thanks @Deepjyoti-Sarmah! |
What does this PR do?
Fixes #5418. A Python call made through a function parameter gets a
callsedge to an unrelated function that happens to share the name.
The lookup never asks whether a parameter owns the name. So the bug is wider
than lambda defaults: a plain
return handler()on a parameter, and a nestedfunction or method calling a captured one, get the same false edge.
There are two producers, and both needed the check:
The second one re-parses each file and walks function bodies itself. With only
the first fix,
from lib import handlerplusdef outer(handler)still linkedto
lib.handler.The rule
The check walks outward from the call the way Python resolves a name:
def's default, annotation or decoratordef's parameters don't countdef,class, import orglobalbinds the nameFive commits
The first commit is a pure move.
engine.pyalready importsresolution.py, sothe shared check lives in a new
python_scope.pythat both can import.Related
#5417 handles a lambda's own parameters and leaves this case to #5418. Both PRs
edit the same condition in
engine.pyand bump the AST cache schema, sowhichever lands second needs a small rebase that keeps both checks.
Type of change
Verification & Invariants
The invariant: a bare call doesn't get an edge to a function its name can't
refer to. A call through a parameter has no target the extractor can know.
Merge Danger
Door: two-way, with one persisted change.
_AST_CACHE_SCHEMAgoes from 53to 54, because cached Python extractions hold the old edges.
Blast radius: Python bare calls only.
x.handler()) andindirect_calledges are untouched.callsedges. It adds no nodes or edges.Limitations
handler = make(), a call tohandler()can stilllink to a same-file function.
functions. v8 does the same, and this PR doesn't change it.
How was this tested?
tests/test_python_param_call_shadowing.pyis new and has 14 tests.plain call, keyword-only, captured by a method, class scope hidden from a
method), calls inside the arguments, and the cross-file import.
def, nearerdef,global,a sibling function's parameter, and a local import.
All with
PYTHONPATHset to the worktree:Not tested: Windows. Nothing here handles paths.
Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (N/A: no skill fragments touched.)