Skip to content

fix(python): do not link calls made through a function parameter (#5418) - #5419

Closed
Deepjyoti-Sarmah wants to merge 5 commits into
Graphify-Labs:v8from
Deepjyoti-Sarmah:fix/5418-python-param-shadow-calls
Closed

Deepjyoti-Sarmah wants to merge 5 commits into
Graphify-Labs:v8from
Deepjyoti-Sarmah:fix/5418-python-param-shadow-calls

Conversation

@Deepjyoti-Sarmah

@Deepjyoti-Sarmah Deepjyoti-Sarmah commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #5418. A Python call made through a function parameter gets a calls
edge to an unrelated function that happens to share the name.

def handler():
    return "module"

def factory(handler):
    return lambda cb=handler(): cb   # calls whatever was passed in
v8 (5ce8fcef)  factory() handler() EXTRACTED
               inner()   handler() EXTRACTED
this branch    (neither)

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 nested
function or method calling a captured one, get the same false edge.

There are two producers, and both needed the check:

 engine.py, walk_calls
+  bare call whose name is a parameter of an enclosing function -> no edge, no raw call
   otherwise: same-file lookup, then raw_calls, as before
 resolution.py, import-guided calls
+  same check, before a call to an imported name becomes a use fact

The second one re-parses each file and walks function bodies itself. With only
the first fix, from lib import handler plus def outer(handler) still linked
to lib.handler.

The rule

The check walks outward from the call the way Python resolves a name:

case result
name is a parameter, own or captured no edge
call sits in a def's default, annotation or decorator that def's parameters don't count
a nearer def, class, import or global binds the name walk stops, existing lookup decides
class body binds the name, call is in a method class scope is skipped

Five commits

b6ff5ab8  refactor(5418): move _python_param_names into python_scope
9486036e  fix(5418): do not link a call made through a Python parameter
9de28c82  fix(5418): respect parameters in the import-guided call pass
e0c3757d  test(5418): pin Python parameter call shadowing
f95d86ae  fix(5418): bump the AST cache schema to 54

The first commit is a pure move. engine.py already imports resolution.py, so
the shared check lives in a new python_scope.py that 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.py and bump the AST cache schema, so
whichever lands second needs a small rebase that keeps both checks.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor (the first commit only)
  • Security fix

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.

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

Merge Danger

Door: two-way, with one persisted change. _AST_CACHE_SCHEMA goes from 53
to 54, because cached Python extractions hold the old edges.

Blast radius: Python bare calls only.

  • Member calls (x.handler()) and indirect_call edges are untouched.
  • The change only removes calls edges. It adds no nodes or edges.
  • Calls inside the arguments of a parameter call are still visited.

Limitations

  • Parameters only. After handler = make(), a call to handler() can still
    link to a same-file function.
  • A lambda's own parameters are left to fix(python): respect lambda parameter shadowing in call edges #5417.
  • The import pass also credits an outer function with calls made in its nested
    functions. v8 does the same, and this PR doesn't change it.

How was this tested?

tests/test_python_param_call_shadowing.py is new and has 14 tests.

  • 8 fail on v8: six same-file shapes (lambda default, captured lambda default,
    plain call, keyword-only, captured by a method, class scope hidden from a
    method), calls inside the arguments, and the cross-file import.
  • 6 guards pass on both: unshadowed call, local def, nearer def, global,
    a sibling function's parameter, and a local import.

All with PYTHONPATH set to the worktree:

.venv/bin/python -m pytest -q tests/test_python_param_call_shadowing.py
    v8 (5ce8fcef)  -> 8 failed, 6 passed
    this branch    -> 14 passed
.venv/bin/python -m pytest -q tests
    this branch    -> 11912 passed, 122 skipped, 1 xfailed
.venv/bin/python -m ruff check graphify tests/test_python_param_call_shadowing.py
    -> All checks passed
.venv/bin/pyright (the changed files)
    new files      -> 0 errors
    edited files   -> 72 errors, the same ones as v8

Not tested: Windows. Nothing here handles paths.

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (N/A: no skill fragments touched.)
  • 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. (No trailer in the commit messages.)

@github-actions

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Nov 23, 2026
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.

@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 review View in Graphify

Nothing blocking · 5 possible issues to check · Merge confidence 5/5

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

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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2  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

@Deepjyoti-Sarmah
Deepjyoti-Sarmah force-pushed the fix/5418-python-param-shadow-calls branch from cf353c0 to f95d86a Compare November 23, 2026 14:05
safishamsi added a commit that referenced this pull request Nov 24, 2026
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]>
@safishamsi

Copy link
Copy Markdown
Member

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!

@safishamsi safishamsi closed this Nov 24, 2026
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]: Python lambda defaults call unrelated same-file functions when enclosing parameters shadow them

2 participants