You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PEP 810 lazy from imports ignore module __getattr__ when reifying #144957
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 resolution
The 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 a lazy_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 failed
Failed again
{'ohno': <lazy_import 'demo_module.ohno'>}
ohno
changed the title [-]PEP810 lazy from imports ignore module `__getattr__` when reifying[/-][+]PEP 810 lazy from imports ignore module `__getattr__` when reifying[/+]on Mar 13, 2026
I can confirm this is still broken on 3.15a8 (running on linuxmint 22.3, python3.15 and python3.15t installed from packages provided by the deadsnakes ppa, both "3.15.0~a8-1+noble1"). The demo module + script posted by OP show the exact same output.
I'm running into the same erroneous import in local testing with PYTHON_LAZY_IMPORTS=all, where rich references typing.Match from rich.markup, crashing in a similar way:
File ".../python3.15t/site-packages/rich/markup.py", line 43, in <module>
_ReStringMatch = Match[str] # regex match object
^^^^^
ImportError: cannot import name 'Match' from 'typing' (/usr/lib/python3.15/typing.py)
Without PYTHON_LAZY_IMPORTS set, there is no issue.
Activity
pythongh-144957: Add test for lazy imports with __getattr__
pythongh-144957: Add test for lazy imports with __getattr__
pythongh-144957: Add test for lazy imports with __getattr__
pythongh-144957: Fix lazy imports to respect module __getattr__
Merge branch 'main' into pythongh-144957-lazy-import-test and fix tes…
[-]PEP810 lazy from imports ignore module `__getattr__` when reifying[/-][+]PEP 810 lazy from imports ignore module `__getattr__` when reifying[/+]DavidCEllis commentedon Apr 2, 2026
I'd like to note that despite what that closed, linked, AI generated PR claimed this does not in fact work on Main (as of this morning).
akaIDIOT commentedon Apr 8, 2026
I can confirm this is still broken on 3.15a8 (running on linuxmint 22.3, python3.15 and python3.15t installed from packages provided by the deadsnakes ppa, both "3.15.0~a8-1+noble1"). The demo module + script posted by OP show the exact same output.
I'm running into the same erroneous import in local testing with
PYTHON_LAZY_IMPORTS=all, whererichreferencestyping.Matchfromrich.markup, crashing in a similar way:Without
PYTHON_LAZY_IMPORTSset, there is no issue.DavidCEllis commentedon Apr 8, 2026
richusingtyping.Matchis also how I found this, although via testing onpip list.JelleZijlstra commentedon May 9, 2026
Added the blocker label as I don't think we should release 3.15 without fixing this.
gh-144957: Fix lazy imports + module __getattr__ (GH-149624)
[3.15] gh-144957: Fix lazy imports + module __getattr__ (GH-149624) (#…