Skip to content

gh-157757: Fix to make lazy import a.b as c import the module a.b - #158092

Merged
pablogsal merged 7 commits into
python:mainfrom
brittanyrey:b-gh157757-lazy-dotted-as
Sep 26, 2026
Merged

pablogsal merged 7 commits into
python:mainfrom
brittanyrey:b-gh157757-lazy-dotted-as

Conversation

@brittanyrey

@brittanyrey brittanyrey commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Bug: import a.b as c compiles to IMPORT_NAME a.b followed by IMPORT_FROM b. Lazily, IMPORT_NAME leaves a placeholder holding "a.b", and IMPORT_FROM rewrote it into the placeholder lazy from a import b produces. Reification then imported a alone and read b off it, so the module a.b was never imported under its own name: an attribute of the package shadowing it answered instead, and math.pi, which no module backs, bound the float where the eager statement raises ModuleNotFoundError.

Fix: Follow @pablogsal's suggestion to have each deferred IMPORT_FROM keep the previous placeholder and the attribute name. Reification runs the original import and then applies the recorded lookups in order with _PyEval_ImportFrom, as the eager statement does.

For lazy from a import b, c, the import gets only the name being resolved as the fromlist, so accessing b doesn't import a.c.

@hugovk hugovk added interpreter-core (Objects, Python, Grammar, and Parser dirs) release-blocker topic-lazy-imports labels Sep 24, 2026
@brittanyrey brittanyrey changed the title gh-157757: [RFC] Import the module a lazy import a.b as c names gh-157757: Import the module a lazy import a.b as c names Sep 24, 2026
@brittanyrey brittanyrey changed the title gh-157757: Import the module a lazy import a.b as c names gh-157757: Fix to make lazy import a.b as c import the module a.b Sep 24, 2026
@pablogsal

Copy link
Copy Markdown
Member

Hmm, maybe we can have each deferred IMPORT_FROM hold onto the previous placeholder and the attribute name?

For import a.b.c as d, we’d keep the original import of "a.b.c", then record the lookup of b, then c. When resolving it, we could walk back to the original import and apply those lookups in order with _PyEval_ImportFrom. Each step would just add one node, without copying the previous names.

Perhaps we can use the existing fields for this: lz_from could hold either the module name or the previous placeholder, and lz_attr could hold the fromlist or the attribute name. That would also handle a custom __lazy_import__ returning a placeholder for a different module name, since we’d keep the actual lookups instead of reconstructing them from that name.

I prototyped this and it seems to work.

We would keep the intermediate placeholders around until resolution, so it uses more memory. I think it’s worth considering, though. It follows what the bytecode does and avoids needing the extra flag.

@brittanyrey

Copy link
Copy Markdown
Contributor Author

Thanks @pablogsal! That was very useful context + direction.
I had initially held off from publishing this one since the new lz_dotted_as felt like overkill for a single corner case

@brettcannon
brettcannon removed their request for review September 24, 2026 20:00
Comment thread Python/import.c Outdated
@pablogsal

Copy link
Copy Markdown
Member

Yep, I think the chaining is much nicer than the flag: we just replay the same IMPORT_FROM lookups the eager bytecode does, so there is no special case for import a.b as c anymore and custom __lazy_import__ placeholders work too. I left a small comment about empty fromlist tuples, otherwise this looks good to me. Thanks a lot for working on this!

@brittanyrey

Copy link
Copy Markdown
Contributor Author

Moved the change into the constructor + added two tests

`import a.b as c` compiles to `IMPORT_NAME a.b` followed by `IMPORT_FROM b`.
Lazily, IMPORT_NAME leaves a placeholder holding "a.b", and IMPORT_FROM
rewrote it into the placeholder `lazy from a import b` produces.  Reification
then imported `a` alone and read `b` off it, so the module `a.b` was never
imported under its own name: an attribute of the package shadowing it answered
instead, and `math.pi`, which no module backs, bound the float where the eager
statement raises ModuleNotFoundError.

Mark the dotted import on the placeholder and keep the whole name on it.
Reification imports that name and then walks its components with IMPORT_FROM,
which is what the eager statement does.

The test pinning `lazy import math.pi as pi` as working is inverted, since the
eager statement raises.
It passes now that a lazy `import a.b as c` imports the module: the
KeyError on 'test.tracedmodules.testmod' came from the submodule never
being imported under its own name.
The flag means "bind the whole dotted name, not the root", which the old
name did not say, and import.c already has unrelated lazy_pending_submodules
machinery to be confused with.
brittanyrey and others added 4 commits September 26, 2026 20:43
Each deferred IMPORT_FROM off a placeholder without a fromlist now keeps
the previous placeholder in lz_from and the attribute name in lz_attr.
Reification walks back to the placeholder IMPORT_NAME left, runs that
import, and replays the lookups in order with _PyEval_ImportFrom, which
is what the eager bytecode does.  This drops the lz_dotted_as flag and
also follows a custom __lazy_import__ that returns a placeholder for a
different module name.
`lazy from a import b` now records its lookup the same way as `import
a.b as c`, so a placeholder holds either the module name and fromlist or
the previous placeholder and an attribute name, and reification has a
single path.  The import passes only the name being resolved as the
fromlist, so accessing b still does not import the other names'
submodules.
__import__("a.b", fromlist=()) returns the top-level package `a`, the same
as fromlist=None, but the placeholder kept the empty tuple.  Every consumer
of lz_attr then read it as a real fromlist: reification narrowed it to the
chained attribute and replayed the lookups on `a.b`, _PyEval_LazyImportFrom
took the attribute off sys.modules["a.b"], and the repr named `a.b.attr`.

_PyLazyImport_New already collapses None to NULL for exactly this reason,
so collapse an empty tuple there too and every site follows.
@pablogsal
pablogsal force-pushed the b-gh157757-lazy-dotted-as branch from c10339c to 48244f1 Compare September 26, 2026 19:46
@pablogsal
pablogsal enabled auto-merge (squash) September 26, 2026 19:46
@hugovk hugovk added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 26, 2026
@pablogsal
pablogsal merged commit 1e8ff18 into python:main Sep 26, 2026
66 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @brittanyrey for the PR, and @pablogsal for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington-app

Copy link
Copy Markdown

Sorry, @brittanyrey and @pablogsal, I could not cleanly backport this to 3.15 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 1e8ff18a1215b711136b4377282e392583847d62 3.15

@pablogsal

Copy link
Copy Markdown
Member

Great work on the lazy import fix, @brittanyrey! Thanks a lot! ❤️

@bedevere-app

bedevere-app Bot commented Sep 26, 2026

Copy link
Copy Markdown

GH-158257 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 26, 2026
pablogsal added a commit that referenced this pull request Sep 26, 2026
…e `a.b` (GH-158092) (#158257)

gh-157757: Fix to make `lazy import a.b as c` import the module `a.b` (#158092)

* Import the module a lazy `import a.b as c` names

`import a.b as c` compiles to `IMPORT_NAME a.b` followed by `IMPORT_FROM b`.
Lazily, IMPORT_NAME leaves a placeholder holding "a.b", and IMPORT_FROM
rewrote it into the placeholder `lazy from a import b` produces.  Reification
then imported `a` alone and read `b` off it, so the module `a.b` was never
imported under its own name: an attribute of the package shadowing it answered
instead, and `math.pi`, which no module backs, bound the float where the eager
statement raises ModuleNotFoundError.

Mark the dotted import on the placeholder and keep the whole name on it.
Reification imports that name and then walks its components with IMPORT_FROM,
which is what the eager statement does.

The test pinning `lazy import math.pi as pi` as working is inverted, since the
eager statement raises.

* Stop excluding test_trace from the lazy-imports-all run

It passes now that a lazy `import a.b as c` imports the module: the
KeyError on 'test.tracedmodules.testmod' came from the submodule never
being imported under its own name.

* Rename lz_submodule to lz_dotted_as and trim the comments

The flag means "bind the whole dotted name, not the root", which the old
name did not say, and import.c already has unrelated lazy_pending_submodules
machinery to be confused with.

* Chain lazy IMPORT_FROM placeholders instead of flagging dotted imports

Each deferred IMPORT_FROM off a placeholder without a fromlist now keeps
the previous placeholder in lz_from and the attribute name in lz_attr.
Reification walks back to the placeholder IMPORT_NAME left, runs that
import, and replays the lookups in order with _PyEval_ImportFrom, which
is what the eager bytecode does.  This drops the lz_dotted_as flag and
also follows a custom __lazy_import__ that returns a placeholder for a
different module name.

* Chain every deferred IMPORT_FROM onto the previous placeholder

`lazy from a import b` now records its lookup the same way as `import
a.b as c`, so a placeholder holds either the module name and fromlist or
the previous placeholder and an attribute name, and reification has a
single path.  The import passes only the name being resolved as the
fromlist, so accessing b still does not import the other names'
submodules.

* Treat an empty fromlist on a lazy import placeholder as no fromlist

__import__("a.b", fromlist=()) returns the top-level package `a`, the same
as fromlist=None, but the placeholder kept the empty tuple.  Every consumer
of lz_attr then read it as a real fromlist: reification narrowed it to the
chained attribute and replayed the lookups on `a.b`, _PyEval_LazyImportFrom
took the attribute off sys.modules["a.b"], and the repr named `a.b.attr`.

_PyLazyImport_New already collapses None to NULL for exactly this reason,
so collapse an empty tuple there too and every site follows.

* gh-157757: Preserve empty fromlists for custom import hooks

---------


(cherry picked from commit 1e8ff18)

Co-authored-by: Brittany Reynoso <breynoso@meta.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

interpreter-core (Objects, Python, Grammar, and Parser dirs) release-blocker topic-lazy-imports

Projects

Development

Successfully merging this pull request may close these issues.

3 participants