Conversation
Move _run_subprocess_with_modules() and _assert_subprocess_ok() from FilterFunctionSignatureTests to LazyImportTestCase, so that any lazy import test can build a package tree in a temporary directory and run code against it in a subprocess. Signed-off-by: Georgios Alexopoulos <grgalex42@gmail.com>
…e module _PyEval_LazyImportFrom() ran its sys.modules fast path on lz_from, which for lazy import a.b as c is the whole dotted name a.b rather than the module a that b has to be taken from. Changes to _PyEval_LazyImportFrom(): 1. Compute the base module before the fast path instead of after it, and look the attribute up on the base. The base is lz_from up to the first dot when lz_attr is NULL, lz_from + "." + lz_attr when lz_attr is a str, and lz_from itself for a from-import. 2. Add last_step and skip the fast path when it is false. It is true for a from-import and for a placeholder without lz_attr whose name has a single dot, and false when lz_attr is a str, that is on the later steps of a name with three or more components. Resolving one of those eagerly would hand a real module to the next IMPORT_FROM, which then imports the rest of the name right away instead of deferring it. 3. Hold the base in one owned reference, from, released on every exit, so the three _PyLazyImport_New() call sites collapse into one. Add a test that imports a.b, where a/b.py defines an attribute b, and checks that lazy import a.b as x binds the module. Signed-off-by: Georgios Alexopoulos <grgalex42@gmail.com>
grgalex
requested review from
DinoV,
Yhg1s,
markshannon and
pablogsal
as code owners
September 16, 2026 14:31
lazy import a.b as c incorrectly binding a.b.b
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.
lazy import a.b as cbindsctoa.b.binstead of the modulea.bwhena.bis already insys.modulesand has an attribute namedb.Root cause
import a.b as ccompiles toIMPORT_NAME a.bfollowed byIMPORT_FROM b. For a lazy import,IMPORT_NAMEreturns a placeholder withlz_from = "a.b"._PyEval_LazyImportFrom()has a fast path for modules already insys.modules. It looks uplz_fromdirectly and getsnamefrom that module. In this case that means looking upbona.b, producinga.b.b, instead of looking upbon the base modulea.Fix
Compute the base module before the
sys.modulesfast path and perform the lookup against it.The fast path must also be limited to the last
IMPORT_FROMidentifiable from the placeholder. Longer dotted names compile to multipleIMPORT_FROMinstructions, and resolving an intermediate step would make the rest of the import eager. Eight existingtest_lazy_importtests cover this.Two-component imports and
fromimports remain eligible for the fast path.Test
Adds
MixedLazyEagerImportTests.test_eager_dotted_import_before_lazy_resolves_to_same_module()alongside the existing single-name test.It creates a package
awitha/b.pydefining an attributeb, then checks:The test fails on
mainbecauselazy_cresolves toa.b.band passes with the fix.test_lazy_import,test_import, andtest_importlibalso pass.Commits
LazyImportTestCaseso the regression test can reuse them. No behavior change._PyEval_LazyImportFrom(), with the regression test and NEWS entry.Future Work / Not Addressed
If package
aitself has an attributebshadowing submodulea.b, the lazy import can still reify to that attribute rather than the submodule. This is a separate mechanism, already tracked in gh-151208. The same mechanism makeslazy import math.pi as xbind the float where the eager statement raises; gh-155194 fixed the barelazy import math.piform only.For names with three or more components, an already-loaded module still resolves on first use rather than at the import statement because later
IMPORT_FROMsteps cannot tell whether they are the last one. Fixing that would require additional compiler or placeholder information and is only an optimization.Closes gh-157614.