Repository navigation
fix(python): a from-import inside a function applies to that function only - #3377
Merged
Merged
Conversation
colbymchenry
force-pushed
the
claude/issue-3229-python-local-from-import
branch
3 times, most recently
from
November 18, 2026 07:22
b7eb274 to
7eeeb32
Compare
This was referenced Nov 18, 2026
… 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
force-pushed
the
claude/issue-3229-python-local-from-import
branch
from
November 18, 2026 08:12
7eeeb32 to
afc936a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
In Python, a
from x import namewritten inside a function now applies to that function only. Calls ofnamein the rest of the file reach the file's owndef nameinstead 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 nameat any depth, so a function-local one was the whole file's, and every bare call ofnamein the file resolved through it. A function-localimport a.b as Xwas already scoped (#2600); the from-import form leaked.Fix
pythonImportsInScope(import-resolver.ts) already scoped the mappings a dotted receiver reads (#2632).pythonBareImportsInScopenow 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 (pythonFunctionImportKeyslists those), while the module's own imports (at the top, undertry:, or in a function that declares the nameglobal) 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
__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-leveltryand one a function makesglobal) pass on both.init:echo_via_pagercalls its own module'sget_pager_file, not_termui_impl.get_pager_filethat the wrapper imports inside its body;raw_terminal'sf()reaches theraw_terminalits own function imports asf, notgetchar, which another function imports asfdjango/CODEGRAPH_KERNEL_EXPECT=1, main's kernel, 4 workers): 929 files passed, 6 skipped; 11,501 tests passed, 2 expected failures, 93 skipped, 0 failed.Fixes #3229
No contributor PR targets this issue. Thanks to inth3shadows for the report.
🤖 Generated with Claude Code