Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PEP810 lazy from imports ignore module__getattr__ when reifying #144957

Open
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error
@DavidCEllis

Description

@DavidCEllis

Bug report

Bug description:

Without lazy imports:
python -c "from typing import Match; print(Match)"

typing.Match

With lazy imports:
python -c "lazy from typing import Match; print(Match)"

Traceback (most recent call last):  File "<string>", line 1, in <module>    lazy from typing import Match; print(Match)ImportError: deferred import of 'typing.Match' raised an exception during resolutionThe above exception was the direct cause of the following exception:Traceback (most recent call last):  File "<string>", line 1, in <module>    lazy from typing import Match; print(Match)                                         ^^^^^ImportError: cannot import name 'Match' from 'typing' (.../src/cpython/Lib/typing.py)

I believe the underlying issue is that the lazy import statement creates alazy_import object in the module__dict__ which then prevents the module__getattr__ from being called. This occurs even if the module has already been imported.

demo_module.py

def__getattr__(name):returnname

demo_script.py

importdemo_modulelazyfromdemo_moduleimportohnotry:print(ohno)exceptImportError:print("Import failed")# The lazy from import has placed the ohno object in demo_module's globalstry:print(demo_module.ohno)exceptImportError:print("Failed again")print({k:vfork,vindemo_module.__dict__.items()ifnotk.startswith("_")})demo_module.__dict__.pop("ohno",None)print(demo_module.ohno)

python demo_script.py

Import failedFailed again{'ohno': <lazy_import 'demo_module.ohno'>}ohno

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2026 Movatter.jp