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 all commits
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
23 changes: 23 additions & 0 deletions Lib/test/support/import_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import shutil
import sys
import textwrap
import unittest
import warnings

Expand Down Expand Up @@ -309,3 +310,25 @@ def ready_to_import(name=None, source=""):
sys.modules[name] = old_module
else:
sys.modules.pop(name, None)


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}
if unexpected := modules_to_block & sys.modules.keys():
startup = ", ".join(unexpected)
raise AssertionError(f'unexpectedly imported at startup: {{startup}}')

import {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
assert_python_ok("-S", "-c", script)
7 changes: 7 additions & 0 deletions Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)

from test import support
from test.support import import_helper
from test.test_inspect import inspect_stock_annotations
from test.test_inspect import inspect_stringized_annotations
from test.test_inspect import inspect_stringized_annotations_2
Expand Down Expand Up @@ -1367,3 +1368,9 @@ def test_multiple_ways_to_create(self):
class TestAnnotationLib(unittest.TestCase):
def test__all__(self):
support.check__all__(self, annotationlib)

def test_lazy_imports(self):
import_helper.ensure_lazy_imports("annotationlib", {
"typing",
"warnings",
})
9 changes: 9 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6317,6 +6317,15 @@ def test_collect_parameters(self):
typing._collect_parameters
self.assertEqual(cm.filename, __file__)

def test_lazy_import(self):
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()
def cached_func(x, y):
Expand Down
Loading