FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-118761: Add helper to ensure that lazy imports are actually lazy by JelleZijlstra · Pull Request #132614 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
23 changes: 23 additions & 0 deletions Lib/test/support/import_helper.py
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
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(
Comment thread
JelleZijlstra marked this conversation as resolved.
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
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
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
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
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

#132596 will fix this

})


@lru_cache()
def cached_func(x, y):
Expand Down

Back | FazBrowse Home | New Git URL