GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `lazy import a.b as c` incorrectly binds to `a.b.b` · Issue #157614 · python/cpython

lazy import a.b as c incorrectly binds to a.b.b #157614

New issue
Copy link
New issue
Copy link
Open
#157626
Open
lazy import a.b as c incorrectly binds to a.b.b#157614
#157626
Copy link
Labels
3.15bugs and security fixesbugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-lazy-importstype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@grgalex
grgalex
opened on Sep 16, 2026
Issue body actions

Bug report

(Edited by @johnslavik)

To reproduce:

import importlib.metadata
import importlib.metadata as x
lazy import importlib.metadata as lazy_x

print("type(x):     ", type(x).__name__)  # reifies to importlib.metadata
print("type(lazy_x):", type(lazy_x).__name__)  # reifies to importlib.metadata.metadata

x.distributions()  # OK
lazy_x.distributions()  # AttributeError: 'function' object has no attribute 'distributions'

x is the importlib.metadata module, as expected. lazy_x is instead reified to importlib.metadata.metadata, the function.

The bug is in cached name resolution.

For lazy import a.b as c, the issue reproduces when:

  1. a.b is already in sys.modules.
  2. a.b has an attribute named b.
  3. c is reified.

Possible root cause

The problem may be in _PyEval_LazyImportFrom: [Python/ceval.c:3305-3361](https://github.com/python/cpython/blob/cf312cfa64dcaa1700531ec8bc76d56ddd65fb15/Python/ceval.c#L3305-L3361) (3.15 at cf312cfa64dcaa1700531ec8bc76d56ddd65fb15, v3.15.0rc2-22-gcf312cf).

The sys.modules fast path at lines 3313-3331 runs before the base-module computation at 3333-3360 and looks up name directly in d->lz_from.

In the IMPORT_FROM chain generated for import a.b.c as x, however, d->lz_from does not appear to be the module the attribute should be taken from: on the first step it is the full dotted name, and on later steps it is one component short of the base.

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Linked PRs

  • gh-157614: Fix lazy import a.b as c incorrectly binding a.b.b #157626

Activity

Next
  1. grgalex
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Sep 16, 2026
  2. grgalex
    added 2 commits that reference this issue on Sep 16, 2026

    pythongh-157614: Fix lazy import a.b as c binding a.b.b instead of th…

    d1de807

    pythongh-157614: Fix lazy import a.b as c binding a.b.b instead of th…

    9858f12
  3. johnslavik
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    3.15bugs and security fixesbugs and security fixes
    topic-lazy-imports
    3.16new features, bugs and security fixesnew features, bugs and security fixes
    on Sep 16, 2026
  4. johnslavik commented on Sep 16, 2026

    @johnslavik
    johnslavik
    on Sep 16, 2026
    Member
    More actions

    Thanks for reporting the issue, I've reproduced it.

    You've opened a PR in your fork repo, not in CPython.

  5. grgalex commented on Sep 16, 2026

    @grgalex
    grgalex
    on Sep 16, 2026
    ContributorAuthor
    More actions

    @johnslavik Thanks for reproducing!

    Ahhh the PR on the fork references the issue so it appears here. Was testing some stuff!

    I'll submit the associated PR once it's ready (soon!) :)

    Reacted by Bartosz Sławecki
  6. johnslavik commented on Sep 16, 2026

    @johnslavik
    johnslavik
    on Sep 16, 2026
    Member
    More actions

    @grgalex In the test please create a package structure to be imported, don't rely on importlib.metadata because that API can change. We should test the pattern, not something that happens to accidentally be an instance of it today.

    Reacted by George Alexopoulos
  7. grgalex
    added 5 commits that reference this issue on Sep 16, 2026

    pythongh-157614: Fix lazy import a.b as c binding a.b.b instead of th…

    fc21e6c

    pythongh-157614: Fix lazy import a.b as c binding a.b.b instead of th…

    1e7835b

    pythongh-157614: Fix lazy import a.b as c binding a.b.b instead of th…

    88b63e1

    pythongh-157614: Make the lazy import subprocess test helper reusable

    733be05

    pythongh-157614: Fix lazy import a.b as c binding a.b.b instead of th…

    58c080f
  8. 11 remaining items

  9. grgalex
    added 3 commits that reference this issue on Sep 16, 2026

    pythongh-157614: Fix lazy import a.b as c binding a.b.b instead of th…

    6ec504a

    pythongh-157614: Make the lazy import subprocess test helper reusable

    cfba96e

    pythongh-157614: Fix lazy import a.b as c binding a.b.b instead of th…

    3398871
  10. grgalex commented on Sep 16, 2026

    @grgalex
    grgalex
    on Sep 16, 2026
    ContributorAuthor
    More actions

    @johnslavik I've opened the PR.

    Next time I'll make sure not to link to the main issue during experimentation in the private fork to avoid polluting the issue with references!

  11. StanFromIreland
    added
    release-blocker
    on Sep 16, 2026
  12. github-project-automation
    added this to Release and Deferred blockers 🚫on Sep 16, 2026
  13. github-project-automation
    moved this to Todo in Release and Deferred blockers 🚫on Sep 16, 2026
  14. StanFromIreland commented on Sep 16, 2026

    @StanFromIreland
    StanFromIreland
    on Sep 16, 2026
    Member
    More actions

    Marking as a release-blocker to get Hugo's eyes on this. This seems pretty bad, and seems like a realistic scenario to hit in practice.

    Reacted by Bartosz Sławecki
  15. StanFromIreland
    changed the title [-]lazy import a.b as c incorrectly binds a.b.b[/-] [+]`lazy import a.b as c` incorrectly binds to `a.b.b`[/+] on Sep 16, 2026
  16. grgalex commented on Sep 18, 2026

    @grgalex
    grgalex
    on Sep 18, 2026
    ContributorAuthor
    More actions

    Another bug found while investigating the present issue: #157757

  17. hugovk commented on Sep 28, 2026

    @hugovk
    hugovk
    on Sep 28, 2026
    Member
    More actions

    Fixed by #158092, removing release-blocker label. Keeping this issue open for now to decide if we want the test from #157626.

    Reacted by Stan Ulbrych and Marcelo Trylesinski
  18. hugovk
    removed
    release-blocker
    on Sep 28, 2026
  19. kevinprimeraquecreo-stack
    added this to Project_VKon Oct 18, 2026
  20. github-project-automation
    moved this to In progress in Project_VKon Oct 18, 2026
  21. kevinprimeraquecreo-stack
    added this to Project_VKon Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.15bugs and security fixesbugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-lazy-importstype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    • Release and Deferred blockers 🚫
      Status
      Todo
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.