Repository navigation
fix(python): scope function-local imports to their function (#5421) - #5428
debojyoti10CC wants to merge 2 commits into
Conversation
…-Labs#5421) A `from X import name` inside a function binds `name` only in that function. Two leaks let it escape: - A bare call to `name` in the function still bound to a same-named definition elsewhere in the same file, even when the import named a module outside the scanned corpus. - The import was recorded as a file-wide symbol alias, so other functions calling their own `name` picked up the imported target. Now a call inside the importing function resolves through that import only. Function-local symbol imports are kept in the alias table with their line range and consulted per use, so sibling functions no longer see them. Ambiguous modules still resolve to nothing. Regression tests fail before this change and pass after it. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
|
Thanks for the pull request, @debojyoti10CC. 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. |
There was a problem hiding this comment.
Graphify review 
2 bugs · 3 possible issues to check · High impact
The change records function scope on Python import facts and uses scope-aware bindings to resolve calls.
Nested functions miss imports made before they are called ·
resolution.py:2044
Calls before a local import bind to the module import ·
resolution.py:2034
3 possible issues we couldn't confirm
- Several 'no call edge' tests would still pass if call extraction produced nothing, so they do not prove the import is respected ·
tests/test_python_external_import_calls.py:77 - Function-local import tests assert an empty call set, which passes even if the resolver never emits edges ·
tests/test_python_external_import_calls.py:170 - Test helper raises KeyError instead of a clear failure when a call edge points to a node missing from the result ·
tests/test_python_external_import_calls.py:22
They don't change the score.
What this review covered
- A changed file's diff was too long to show in full, so the review may not have seen every changed line in it.
- One review pass used all of its turns for reading code, so it reported from what it had already read.
- Some lookups asked for files or lines that do not exist; the review was told and carried on.
- 4 of 4 changed files inspected. This is scope, not a correctness guarantee.
Code graph · 4971 functions depend on this change
- 3 functions became more coupled.
extract()(1353 callers),_collect_python_symbol_resolution_facts()(2 callers),walk_calls()(1 callers). - 80 of 620 test files are affected by this change.
- Gate, test list and every flagged function: in the Graphify check.
Reviewed b5472cb · 1 of 1 earlier findings resolved · 6 earlier findings not re-checked · Full report in the Graphify check
| for first, last, local, origin in scoped_aliases_by_file.get(file_path, ()) | ||
| if local == use_fact.local_name and first <= use_fact.line <= last | ||
| ] | ||
| if scoped_origins: |
There was a problem hiding this comment.
The first same-function import can win over the later one
If a function imports the same name more than once, calls after the later import can point to the earlier module instead.
Resolve function-local imports by their statement position in _apply_symbol_resolution_facts in graphify/extractors/resolution.py.
Evidence · checked against the code
Expected: The call edge targets the symbol from the later import, which is the active Python binding.
Actually: Equal function-scope starts tie, so the first import can supply the resolved target.
- Independent source review: Checked the raw-call consumer: it filters candidates using the selected import binding, and the symbol-use resolver independently selects the first tied scoped origin. The existing local-import tests checked scope leakage, not repeated same-name imports; the proposed test directly exercises the duplicate-import case.
Prompt to fix with AI
In graphify/extractors/resolution.py, _apply_symbol_resolution_facts (lines 2044-2047):
Problem: The first same-function import can win over the later one
Expected: The call edge targets the symbol from the later import, which is the active Python binding.
Actually: Equal function-scope starts tie, so the first import can supply the resolved target.
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
…y-Labs#5421) - A call inside a nested function now sees the names imported by any enclosing function, not only its own body. - When a function imports the same name twice, each use binds to the latest import before it. - A function-local import in a package `__init__.py` is no longer published as a package re-export. Adds regression tests for each case. They fail before this change. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
|
Addressed the review points in b5472cb:nn- Nested functions: a call inside a nested function now resolves through an import in any enclosing function. Regression test: |
What does this PR do?nnFixes #5421. A
from X import nameinside a function bindsnameonly inside that function. Before this change, two things leaked:nn- A bare call tonamein that function still resolved to a same-named definition in the same file, even when the import pointed at a module outside the scanned corpus.n- The import was recorded as a file-wide alias, so other functions calling their ownnamegained an edge to the imported target.nnNow a call inside the importing function resolves through that import only, and sibling functions no longer see it. Calls through an ambiguous module resolve to nothing. Module-level imports are unchanged.nn## Type of changenn- [x] Bug fixn- [ ] New featuren- [ ] Documentationn- [x] Tests or CIn- [ ] Refactorn- [ ] Security fixnn## Verification & Invariantsnn- Reproduced #5421 on upstreamv8:caller() -> main()was emitted as EXTRACTED for a function-local import of an absent package.n- The invariant is that a name bound by a function-local import resolves only inside that function, and never to a guessed same-named target. Extraction stays deterministic.n- Regression tests:test_function_local_import_does_not_bind_same_file_definitionandtest_function_local_relative_import_does_not_leak_to_sibling_function. Both fail on upstreamv8and pass with this change.n- Limitations: this covers bare-name calls bound by function-localfrom ... import. Function-localimport xand member calls are unchanged.nn## How was this tested?nntextnuv run pytest tests/test_python_external_import_calls.py tests/test_symbol_resolution.pyn57 passed (includes the 2 new regression tests)nnntextnruff check (changed files): all checks passednpyright (changed files): 52 errors, same as upstream v8nnnFull suite on my Windows machine: 3201 passed, 136 skipped. The run ended with a pytest internal error while formatting a failure report, so I could not identify that test. The failures I checked (Lua, Fortran, CL, R symbol-only, VB.NET) also fail on upstreamv8on this machine. CI on Linux is the authoritative check.nn## Graphify-specific checklistnn- [ ] I updated generated skill artifacts. Not applicable: no skill fragments changed.n- [x] I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).n- [x] I reviewed changes for security implications (no unsafe interpolation into shell/Python).n- [x] I confirmed no API keys or local-only graph data are included.n- [x] (If applicable) I disclosed AI authorship in my commit messages.