Skip to content

Module __getattr__ shadows a module's own lazy imports #158824

Description

@brittanyrey

Bug report

Bug
Accessing a lazily imported module via attribute access triggers the module's __getattr__, instead of resolving the lazy import.

Minimal Repro

# pkg/__init__.py
lazy from ._version import __version__

def __getattr__(name):
    import importlib
    return importlib.import_module(f"{__name__}.{name}")
>>> import pkg
>>> pkg.__version__
ModuleNotFoundError: No module named 'pkg.__version__'

How this Happened
The check in _Py_module_getattro_impl() (Objects/moduleobject.c) was the fix for
gh-144957, when lazy from put placeholders into the source module. gh-150052 removed
those placeholders, so now we override the lazy import reification with the __getattr__ check.

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Linked PRs

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions