8000 gh-118761: Add helper to ensure that lazy imports are actually lazy by JelleZijlstra · Pull Request #132614 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-118761: Add helper to ensure that lazy imports are actually lazy #132614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Apply suggestions from code review
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
  • Loading branch information
JelleZijlstra and AA-Turner authored Apr 17, 2025
commit a77577e3747bc91f4ca530b4cee2cb6b054a3630
11 changes: 7 additions & 4 deletions Lib/test/support/import_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,16 +315,19 @@ def ready_to_import(name=None, source=""):
def ensure_lazy_imports(imported_module, modules_to_block):
"""Test that when imported_module is imported, none of the modules in
modules_to_block are imported as a side effect."""
modules_to_block = frozenset(modules_to_block)
script = textwrap.dedent(
f"""
import sys
modules_to_block = {modules_to_block}
for mod in modules_to_block:
assert mod not in sys.modules, f"{{mod}} was imported at startup"
if unexpected := modules_to_block & sys.modules.keys():
startup = ", ".join(unexpected)
raise AssertionError(f'unexpectedly imported at startup: {{startup}}')

import {imported_module}
for mod in modules_to_block:
assert mod not in sys.modules, f"{{mod}} was imported after importing {imported_module}"
if unexpected := modules_to_block & sys.modules.keys():
after = ", ".join(unexpected)
raise AssertionError(f'unexpectedly imported after importing {imported_module}: {{after}}')
"""
)
from .script_helper import assert_python_ok
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ def test__all__(self):
support.check__all__(self, annotationlib)

def test_lazy_imports(self):
import_helper.ensure_lazy_imports("annotationlib", [
import_helper.ensure_lazy_imports("annotationlib", {
"typing",
"warnings",
])
})
4 changes: 2 additions & 2 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6318,13 +6318,13 @@ def test_collect_parameters(self):
self.assertEqual(cm.filename, __file__)

def test_lazy_import(self):
import_helper.ensure_lazy_imports("typing", [
import_helper.ensure_lazy_imports("typing", {
"warnings",
"inspect",
"re",
"contextlib",
# "annotationlib", # TODO
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#132596 will fix this

])
})


@lru_cache()
Expand Down
Loading
0