import os
import shutil
import sys
+import textwrap
import unittest
import warnings
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)
)
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
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",
+ })
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
+ })
+
@lru_cache()
def cached_func(x, y):