Skip to content
Merged
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
Next Next commit
Add test helper logic to allow for additional commands in lazy import…
… tests
  • Loading branch information
DavidCEllis committed Apr 21, 2026
commit a2c939581de35e312aa1ed479be13abc64837eb4
12 changes: 11 additions & 1 deletion Lib/test/support/import_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def ready_to_import(name=None, source=""):
sys.modules.pop(name, None)


def ensure_lazy_imports(imported_module, modules_to_block):
def ensure_lazy_imports(imported_module, modules_to_block, *, additional_code=None):
"""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)
Expand All @@ -343,6 +343,16 @@ def ensure_lazy_imports(imported_module, modules_to_block):
raise AssertionError(f'unexpectedly imported after importing {imported_module}: {{after}}')
"""
)
if additional_code:
script += additional_code
script += textwrap.dedent(
f"""
if unexpected := modules_to_block & sys.modules.keys():
after = ", ".join(unexpected)
raise AssertionError(f'unexpectedly imported after additional code: {{after}}')
"""
)

from .script_helper import assert_python_ok
assert_python_ok("-S", "-c", script)

Expand Down