Uh oh!
There was an error while loading. Please reload this page.
gh-144957: Add test for lazy imports with __getattr__ - #145330
Closed
gourijain029-del wants to merge 3 commits into
Closed
gh-144957: Add test for lazy imports with __getattr__#145330gourijain029-del wants to merge 3 commits into
gourijain029-del wants to merge 3 commits into
Conversation
gourijain029-del
requested review from
DinoV, Yhg1s and pablogsal
as code ownersFebruary 27, 2026 20:34
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Adds regression test to verify lazy imports work correctly with modules that use __getattr__ for dynamic attributes (e.g. typing.Match). The issue appears to be already fixed in current main branch.
Member
Your test doesn't pass locally for me: ❯ ./python.exe-munittestLib.test.test_import.test_lazy_imports
....................................................F............BAR_MODULE_LOADED
...........................
======================================================================FAIL: test_lazy_import_with_getattr (Lib.test.test_import.test_lazy_imports.LazyImportTests.test_lazy_import_with_getattr)
Lazyimportsworkwithmodule__getattr__ (gh-144957).
----------------------------------------------------------------------Traceback (mostrecentcalllast):
File"/Users/bartosz.slawecki/Python/cpython/Lib/test/test_import/test_lazy_imports.py", line101, intest_lazy_import_with_getattrself.assertEqual(result.returncode, 0, result.stderr)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^AssertionError: 1!=0 : Traceback (mostrecentcalllast):
File"<string>", line4, in<module>ImportError: deferredimportof'typing.Match'raisedanexceptionduringresolutionTheaboveexceptionwasthedirectcauseofthefollowingexception:
Traceback (mostrecentcalllast):
File"<string>", line5, in<module>print(Match)
^^^^^ImportError: cannotimportname'Match'from'typing' (/Users/bartosz.slawecki/Python/cpython/Lib/typing.py)Presumably there's something with CI, needs investigation. |
gourijain029-delforce-pushed
the
gh-144957-lazy-import-test
branch
from
February 27, 2026 20:59
122ad9f to
3e63300CompareWhen resolving lazy imports, check if a lazy import object was found and the module has __getattr__. If so, try calling __getattr__ first before using the lazy import object.
Comment on lines
+90
to
+103
| code = textwrap.dedent(""" | ||
| import sys | ||
| sys.set_lazy_imports("normal") | ||
| lazy from test.test_import.data.lazy_imports.module_with_getattr import dynamic_attr | ||
| assert dynamic_attr == "from_getattr" | ||
| print("OK") | ||
| """) | ||
| result = subprocess.run( | ||
| [sys.executable, "-c", code], | ||
| capture_output=True, | ||
| text=True | ||
| ) | ||
| self.assertEqual(result.returncode, 0, result.stderr) | ||
| self.assertIn("OK", result.stdout) |
Member
There was a problem hiding this comment.
Less prone to false positives:
Suggested change
| code=textwrap.dedent(""" | |
| importsys | |
| sys.set_lazy_imports("normal") | |
| lazyfromtest.test_import.data.lazy_imports.module_with_getattrimportdynamic_attr | |
| assertdynamic_attr=="from_getattr" | |
| print("OK") | |
| """) | |
| result=subprocess.run( | |
| [sys.executable, "-c", code], | |
| capture_output=True, | |
| text=True | |
| ) | |
| self.assertEqual(result.returncode, 0, result.stderr) | |
| self.assertIn("OK", result.stdout) | |
| code=textwrap.dedent(""" | |
| importsys | |
| sys.set_lazy_imports("normal") | |
| lazyfromtest.test_lazy_import.data.module_with_getattrimportdynamic_attr | |
| print(repr(dynamic_attr)) | |
| """) | |
| result=subprocess.run( | |
| [sys.executable, "-c", code], | |
| capture_output=True, | |
| text=True | |
| ) | |
| self.assertEqual(result.returncode, 0, result.stderr) | |
| self.assertIn("'from_getattr'", result.stdout) |
pablogsal
commented
Mar 1, 2026
Member
This user is creating a lot of AI generated PRs see #145276 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a regression test for lazy imports working with modules that use
__getattr__.The issue reported that
lazy from typing import Matchwould fail sinceMatchis provided bytyping.__getattr__rather than being in the module dict. Testing shows this works correctly in current main - the existing code inregister_lazy_on_parent()already checks for__getattr__and skips adding lazy import objects to those modules.This test documents the expected behavior and prevents future regressions.
__getattr__when reifying #144957