Skip to content

fix(python): scope function-local imports to their function (#5421) - #5428

Open
debojyoti10CC wants to merge 2 commits into
Graphify-Labs:v8from
debojyoti10CC:fix/python-local-import-scope
Open

debojyoti10CC wants to merge 2 commits into
Graphify-Labs:v8from
debojyoti10CC:fix/python-local-import-scope

Conversation

@debojyoti10CC

Copy link
Copy Markdown
Contributor

What does this PR do?nnFixes #5421. A from X import name inside a function binds name only inside that function. Before this change, two things leaked:nn- A bare call to name in 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 own name gained 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 upstream v8: 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_definition and test_function_local_relative_import_does_not_leak_to_sibling_function. Both fail on upstream v8 and pass with this change.n- Limitations: this covers bare-name calls bound by function-local from ... import. Function-local import x and 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 upstream v8 on 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.

…-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>
@github-actions

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Nov 23, 2026

@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

Review before merging · Merge confidence 3/5

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.

P2  Nested functions miss imports made before they are called · resolution.py:2044

Bug  Calls before a local import bind to the module import · resolution.py:2034

3 possible issues we couldn't confirm

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

Comment thread graphify/extractors/engine.py Resolved
Comment thread graphify/extractors/resolution.py Outdated
Comment on lines +2044 to +2047
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:

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

Comment thread graphify/extractors/engine.py Resolved
…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>
@debojyoti10CC

Copy link
Copy Markdown
Contributor Author

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: test_nested_function_call_resolves_through_enclosing_function_import.n- Duplicate imports: a call binds to the latest import of that name before it in the same function. Regression test: test_later_function_local_import_wins_over_earlier_one.n- __init__.py: a function-local import is no longer published as a package re-export. Regression test: test_function_local_import_is_not_a_package_reexport.nnNot changed: the per-call re-scan of the enclosing function, which the bot flagged as a possible slowdown. I can cache it if you want it.nnLocal checks on Windows: tests/test_python_external_import_calls.py and tests/test_symbol_resolution.py pass (60). pyright and ruff match upstream v8. The 3.13 CI failures are Linux-only watch tests that use /dev/shm. I could not reproduce them here, and one watch test already fails on upstream v8 on Windows.

This branch has not been deployed

No deployments
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.

1 participant