Skip to content

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

Description

@grgalex

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

Activity

  1. added 14 commits that reference this issue on Sep 16, 2026
  2. grgalex commented on Sep 16, 2026

    @grgalex
    ContributorAuthor

    @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!

  3. StanFromIreland commented on Sep 16, 2026

    @StanFromIreland
    Member

    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.

  4. 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
  5. grgalex commented on Sep 18, 2026

    @grgalex
    ContributorAuthor

    Another bug found while investigating the present issue: #157757

  6. hugovk commented on Sep 28, 2026

    @hugovk
    Member

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

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 fixes3.16new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-lazy-importstype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions