Repository navigation
Conversation
…raphify-Labs#5658) The indirect-call guard only knew the caller's own parameters and locals. A nested function that passed on a variable captured from its enclosing function (`source` bound in `extract_file`, read in `walk`) had that name treated as a free reference, and the cross-file resolver bound it to any same-named function in the corpus. On graphify's own package this gave a nested `source()` in cli.py 126 fabricated `indirect_call` edges. For Python, each nested body now walks with the parameters and locals of its enclosing functions folded into `extra_locals` (the same channel Graphify-Labs#5217 uses for JS closures). Class scopes are skipped as in Python. A name that a nearer scope imports or defines with `def` hides the same name further out, so a local import and a nested def still resolve. Tests: tests/test_python_enclosing_scope_shadow.py; the two bug cases fail without this change, the five guards pass both ways. Co-Authored-By: Claude Opus 5.5
There was a problem hiding this comment.
Graphify review 
1 bug · High impact
The extractor now adds enclosing Python function bindings to the names that block indirect-call matching.
A middle function’s
global declaration changes nested name resolution · engine.py:14934
What this review covered
- We didn't review 1 file that isn't source code, such as docs, config, images, or generated or third-party code.
- One review pass used all of its turns for reading code, so it reported from what it had already read.
- 2 of 2 changed files inspected. This is scope, not a correctness guarantee.
Code graph · 5061 functions depend on this change
- 1 function became more coupled.
_extract_generic()(23 callers). - 693 of 693 test files are affected by this change.
- Gate, test list and every flagged function: in the Graphify check.
Reviewed e6e9fad · 2 earlier findings not re-checked
| scope = scope_parents.get(caller_nid) | ||
| while scope and scope not in seen_scopes: | ||
| seen_scopes.add(scope) | ||
| scope_locals = local_bound_names.get(scope, frozenset()) |
There was a problem hiding this comment.
A
global callback can be hidden by an enclosing local
A nested function that declares a name global can lose its indirect-call edge when an enclosing function has a local with the same name.
Exclude names declared global in the nested function from python_enclosing_locals.
Evidence · checked against the code
Expected: The callback name resolves to the module-level binding, and the extractor retains the indirect-call edge to that callable.
Actually: The enclosing local is included in the nested caller’s shadow set, so the indirect-reference guard can suppress the edge.
- Independent source review: Checked the existing
globaltest and the indirect-argument handler. The test exerciseshandler()as a direct call and does not passhandleras a callback, so it does not counter this claim. - The added tests do not cover
globaldeclarations. - The omitted edge occurs when the nested function's global name is also locally bound in an enclosing function.
Prompt to fix with AI
In graphify/extractors/engine.py, python_enclosing_locals (lines 14875-14878):
Problem: A `global` callback can be hidden by an enclosing local
Expected: The callback name resolves to the module-level binding, and the extractor retains the indirect-call edge to that callable.
Actually: The enclosing local is included in the nested caller’s shadow set, so the indirect-reference guard can suppress the edge.
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
There was a problem hiding this comment.
Fixed in e6e9fad. A name declared global in the nested function (or in a function between it and the outer local) now resolves to the module binding, so the callback edge stays. Covered by test_global_declaration_in_the_nested_function_reaches_the_module_5658 and test_global_declaration_in_a_middle_function_hides_an_outer_local_5658, which both fail without the change.
| scope = scope_parents.get(caller_nid) | ||
| while scope and scope not in seen_scopes: | ||
| seen_scopes.add(scope) | ||
| scope_locals = local_bound_names.get(scope, frozenset()) |
There was a problem hiding this comment.
Comprehension targets hide names in nested functions
A comprehension target in an enclosing function can make the extractor drop a real indirect-call edge from a nested function.
Do not propagate comprehension targets as enclosing function locals in python_enclosing_locals.
Evidence · checked against the code
Expected: The nested function’s reference can resolve to the module-level callable because the comprehension target does not bind in the enclosing function.
Actually: The extractor propagates the target name to the nested function and suppresses the inferred indirect-call edge.
- Independent source review: The new scope logic accounts for nearer lexical definitions and imports, but the inspected path does not treat comprehension targets as a separate scope or otherwise exclude them. The existing nested-closure test covers JavaScript, not this Python case, so it does not mitigate the reported path.
Prompt to fix with AI
In graphify/extractors/engine.py, python_enclosing_locals (lines 14875-14878):
Problem: Comprehension targets hide names in nested functions
Expected: The nested function’s reference can resolve to the module-level callable because the comprehension target does not bind in the enclosing function.
Actually: The extractor propagates the target name to the nested function and suppresses the inferred indirect-call edge.
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
There was a problem hiding this comment.
Fixed in e6e9fad. Comprehension targets of an enclosing function are no longer passed down to nested functions, since they are bound in the comprehension. Covered by test_comprehension_target_does_not_shadow_in_a_nested_function_5658, which fails without the change.
…ows (Graphify-Labs#5658) The enclosing-locals set added for Graphify-Labs#5658 took each outer function's full local set, so two cases dropped a real indirect-call edge: - `global handler` in the nested function (or in a function between it and the outer local) names the module binding, but the outer local still hid it. - A comprehension target (`[h for h in xs]`) is bound in the comprehension's own scope, yet it was propagated to nested functions. Enclosing scopes now contribute their locals without comprehension targets and without names they declare `global`; a `global` name also hides the same name further out, as in CPython's symtable. Three regression tests fail without this change. Co-Authored-By: Claude Opus 5.5
| scope_locals = _python_visible_locals(scope) | ||
| bound |= scope_locals - claimed | ||
| claimed |= scope_locals | _python_scope_claims(scope) | ||
| scope = scope_parents.get(scope) |
There was a problem hiding this comment.
A middle function’s
global declaration changes nested name resolution
A nested function can be linked to a module callback even when it captures a same-named local from a farther enclosing function.
Keep middle’s global handler scoped to middle in _python_scope_claims.
Evidence · checked against the code
Expected: The nested function captures the farther function’s local binding; the extractor should suppress an indirect edge to a same-named module callback.
Actually: The extractor treats the intervening global declaration as hiding the farther local, allowing a false indirect-call edge to the module callback.
- Independent source review: Inspected the indirect-reference caller and its shadowing guard: it skips names present in
enclosing_locals, and the nested call passespython_enclosing_localsinto that guard. In this case the middle function’s global claim wrongly keeps the outer binding out of that set, so the guard does not stop the false edge. - Some lookups asked for files or lines that do not exist; the review was told and carried on.
Prompt to fix with AI
In graphify/extractors/engine.py, _python_scope_claims (lines 14931-14934):
Problem: A middle function’s `global` declaration changes nested name resolution
Expected: The nested function captures the farther function’s local binding; the extractor should suppress an indirect edge to a same-named module callback.
Actually: The extractor treats the intervening global declaration as hiding the farther local, allowing a false indirect-call edge to the module callback.
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
What does this PR do?
Fixes #5658.
The Python indirect-call guard only knew the caller's own parameters and locals. A nested function that passes on a variable captured from its enclosing function had that name treated as a free reference, and the cross-file resolver bound it to any same-named function in the corpus. The issue's reproduction, on current
v8:gives
indirect_call walk -> pkg_cli_placeholder_source(INFERRED 0.85). On graphify's owngraphify/package that nestedsource()collects 126 such edges and tops the god-node list.How:
extractors/engine.py: before the body walk, each Python body gets the parameters and locals of its enclosing functions (viascope_parents) folded intoextra_locals, the same channel JS: a variable captured from an enclosing function is not shadowed, so passing it as an argument fabricates an indirect_call to any same-named function in the corpus #5217 uses for JS closures. That set already feeds the indirect-reference shadow check and the bare-call local-data check._python_local_import_names) or defines withdefhides the same name further out, sodef outer(h): def inner(): from lib import hstill resolves tolib.h._python_local_bound_namesstops at them), so a def from an enclosing function still resolves through the lexical scope map.ARCHITECTURE.md: a short paragraph next to the lambda-parameter shadowing rule.This is the first of the two checks the issue suggests (enclosing-scope bindings). The second (a nested def is never reachable by bare name from another module) is not needed for these cases and is left out to keep the change narrow. #3607 (open) covers a different case, a nested def in an enclosing scope; this PR is about enclosing variables.
Type of change
Verification & Invariants
Invariant: a name bound as a variable in any enclosing function scope is a value for a nested function, never a reference to a same-named function elsewhere, unless a nearer scope rebinds it with an import or a
def.Bug cases (fail before, pass after):
sourcepassed as an argument);Guards (pass before and after):
defpassed by name still resolves;defwins over a farther variable.The existing
test_local_import_over_parameter_keeps_its_edge(direct calls) also covers the import precedence and passes.Limitations:
global/nonlocaldeclarations are not modelled: a name an enclosing function declaresglobalis still treated as its local, so a callback through it gets no edge (fails closed).scope_parentsare walked. A method of a class defined inside a function is already extracted as that function's nested function, so it does see the function's locals (checked:def outer(handler): class K: def m(self): run(handler)no longer binds to another module'shandler).Cache: the Python extractor output changes (fewer
indirect_calledges), so it needs an AST cache schema bump. I've left that to the maintainers' release step, as with other extractor changes.How was this tested?
Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (n/a)