Skip to content

fix(python): a from-import inside a function applies to that function only - #3377

Merged
colbymchenry merged 1 commit into
mainfrom
claude/issue-3229-python-local-from-import
Nov 18, 2026
Merged

colbymchenry merged 1 commit into
mainfrom
claude/issue-3229-python-local-from-import

Conversation

@colbymchenry

@colbymchenry colbymchenry commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Summary

In Python, a from x import name written inside a function now applies to that function only. Calls of name in the rest of the file reach the file's own def name instead of the imported function, which is how a wrapper that imports the original it wraps works.

Root cause

A Python file's import mappings read from x import name at any depth, so a function-local one was the whole file's, and every bare call of name in the file resolved through it. A function-local import a.b as X was already scoped (#2600); the from-import form leaked.

Fix

pythonImportsInScope (import-resolver.ts) already scoped the mappings a dotted receiver reads (#2632). pythonBareImportsInScope now does the same for a bare name. Where no function around the reference binds the name, a mapping that only function-local from-imports make is dropped (pythonFunctionImportKeys lists those), while the module's own imports (at the top, under try:, or in a function that declares the name global) still bind every call. Inside a function that imports the name, that import's mapping is the one read. A local of the name that isn't an import leaves the mappings as they were.

Validation

  • New __tests__/python-function-local-from-import.test.ts, 3 cases: the issue's repro fails on main and passes with the fix; 2 guards (a module-level import beside a function-local one; an import under a module-level try and one a function makes global) pass on both.
  • The 35 Python test files pass.
  • A/B against main, both arms with the 1.6.3 kernel, fresh init:
Repo Files Changed edges Triage
click (2247b35) 100 2 moved both right: termui's echo_via_pager calls its own module's get_pager_file, not _termui_impl.get_pager_file that the wrapper imports inside its body; raw_terminal's f() reaches the raw_terminal its own function imports as f, not getchar, which another function imports as f
flask, httpx, django django/ 91, 66, 930 0
express, gin (controls) 149, 110 0 byte-identical

Fixes #3229

No contributor PR targets this issue. Thanks to inth3shadows for the report.

🤖 Generated with Claude Code

… only

A Python file's import mappings read `from x import name` at any depth, so
one written inside a function was the whole file's. Every call of `name`
elsewhere in the file went to the import, even where the file defines its
own `def name`: a test module wrapping a library function in a same-named
helper that imports the original inside its body had all its test calls
land on the library function, and the helper got no callers. Function-local
`import a.b as X` was already scoped (Issue 2600).

The mappings a bare name reads are now scoped the way a dotted receiver's
already are (pythonImportsInScope). Where no function around the
reference binds the name, a mapping that only function-local from-imports
make is dropped, and the module's own imports, at the top, under `try:`, or
in a function that declares the name `global`, still bind every call;
inside a function that imports the name, that import's mapping is the one
read. A local of the name that isn't an import changes nothing here.

Validation, against main (both arms with the 1.6.3 kernel, fresh init):
- New test file, 3 cases: the issue's repro fails on main and passes now;
  2 guards (a module-level import beside a function-local one, an import
  under a module-level `try` and one a function makes `global`) pass on
  both.
- click: 2 calls move, both right: termui's `echo_via_pager` calling its
  own module's `get_pager_file` linked to `_termui_impl.get_pager_file`,
  imported inside that wrapper; `raw_terminal`'s `f()` linked to
  `_termui_impl.getchar`, imported as `f` inside `getchar`, and now
  reaches the `raw_terminal` its own function imports as `f`. flask,
  httpx, django's django/ package, express and gin: byte-identical.
- Index CPU on django (median of 5, interleaved): 35.0 s -> 35.1 s, noise;
  the function-local import lines are found once per file.
- The 35 Python test files pass.

Fixes #3229

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@colbymchenry
colbymchenry force-pushed the claude/issue-3229-python-local-from-import branch from 7eeeb32 to afc936a Compare November 18, 2026 08:12
@colbymchenry
colbymchenry merged commit d2f3f07 into main Nov 18, 2026
@colbymchenry
colbymchenry deleted the claude/issue-3229-python-local-from-import branch November 18, 2026 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: a from x import name inside a function makes every call to name in the file link to the import

1 participant