Skip to content

Commit cae660d

Browse files
gh-118761: Add test_lazy_import for more modules (#133057)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent b1aa515 commit cae660d

20 files changed

+139
-11
lines changed

Lib/test/test_ast/test_ast.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from test.support import os_helper
2727
from test.support import skip_emscripten_stack_overflow, skip_wasi_stack_overflow
2828
from test.support.ast_helper import ASTTestMixin
29+
from test.support.import_helper import ensure_lazy_imports
2930
from test.test_ast.utils import to_tuple
3031
from test.test_ast.snippets import (
3132
eval_tests, eval_results, exec_tests, exec_results, single_tests, single_results
@@ -47,6 +48,12 @@ def ast_repr_update_snapshots() -> None:
4748
AST_REPR_DATA_FILE.write_text("\n".join(data))
4849

4950

51+
class LazyImportTest(unittest.TestCase):
52+
@support.cpython_only
53+
def test_lazy_import(self):
54+
ensure_lazy_imports("ast", {"contextlib", "enum", "inspect", "re", "collections", "argparse"})
55+
56+
5057
class AST_Tests(unittest.TestCase):
5158
maxDiff = None
5259

Lib/test/test_base64.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
import binascii
44
import os
55
from array import array
6+
from test.support import cpython_only
67
from test.support import os_helper
78
from test.support import script_helper
9+
from test.support.import_helper import ensure_lazy_imports
10+
11+
12+
class LazyImportTest(unittest.TestCase):
13+
@cpython_only
14+
def test_lazy_import(self):
15+
ensure_lazy_imports("base64", {"re", "getopt"})
816

917

1018
class LegacyBase64TestCase(unittest.TestCase):

Lib/test/test_cmd.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@
1111
import io
1212
import textwrap
1313
from test import support
14-
from test.support.import_helper import import_module
14+
from test.support.import_helper import ensure_lazy_imports, import_module
1515
from test.support.pty_helper import run_pty
1616

17+
class LazyImportTest(unittest.TestCase):
18+
@support.cpython_only
19+
def test_lazy_import(self):
20+
ensure_lazy_imports("cmd", {"inspect", "string"})
21+
22+
1723
class samplecmdclass(cmd.Cmd):
1824
"""
1925
Instance the sampleclass:

Lib/test/test_csv.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
import gc
1111
import pickle
1212
from test import support
13-
from test.support import import_helper, check_disallow_instantiation
13+
from test.support import cpython_only, import_helper, check_disallow_instantiation
14+
from test.support.import_helper import ensure_lazy_imports
1415
from itertools import permutations
1516
from textwrap import dedent
1617
from collections import OrderedDict
@@ -1565,6 +1566,10 @@ class MiscTestCase(unittest.TestCase):
15651566
def test__all__(self):
15661567
support.check__all__(self, csv, ('csv', '_csv'))
15671568

1569+
@cpython_only
1570+
def test_lazy_import(self):
1571+
ensure_lazy_imports("csv", {"re"})
1572+
15681573
def test_subclassable(self):
15691574
# issue 44089
15701575
class Foo(csv.Error): ...

Lib/test/test_email/test_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
import time
55
import unittest
66

7+
from test.support import cpython_only
8+
from test.support.import_helper import ensure_lazy_imports
9+
10+
11+
class TestImportTime(unittest.TestCase):
12+
13+
@cpython_only
14+
def test_lazy_import(self):
15+
ensure_lazy_imports("email.utils", {"random", "socket"})
16+
717

818
class DateTimeTests(unittest.TestCase):
919

Lib/test/test_enum.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL
2020
from test import support
2121
from test.support import ALWAYS_EQ, REPO_ROOT
22-
from test.support import threading_helper
22+
from test.support import threading_helper, cpython_only
23+
from test.support.import_helper import ensure_lazy_imports
2324
from datetime import timedelta
2425

2526
python_version = sys.version_info[:2]
@@ -5288,6 +5289,10 @@ class MiscTestCase(unittest.TestCase):
52885289
def test__all__(self):
52895290
support.check__all__(self, enum, not_exported={'bin', 'show_flag_values'})
52905291

5292+
@cpython_only
5293+
def test_lazy_import(self):
5294+
ensure_lazy_imports("enum", {"functools", "warnings", "inspect", "re"})
5295+
52915296
def test_doc_1(self):
52925297
class Single(Enum):
52935298
ONE = 1

Lib/test/test_functools.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from test.support import import_helper
2525
from test.support import threading_helper
26+
from test.support import cpython_only
2627
from test.support import EqualToForwardRef
2728

2829
import functools
@@ -63,6 +64,14 @@ def __add__(self, other):
6364
class MyDict(dict):
6465
pass
6566

67+
class TestImportTime(unittest.TestCase):
68+
69+
@cpython_only
70+
def test_lazy_import(self):
71+
import_helper.ensure_lazy_imports(
72+
"functools", {"os", "weakref", "typing", "annotationlib", "warnings"}
73+
)
74+
6675

6776
class TestPartial:
6877

Lib/test/test_gettext.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from functools import partial
77

88
from test import support
9-
from test.support import os_helper
9+
from test.support import cpython_only, os_helper
10+
from test.support.import_helper import ensure_lazy_imports
1011

1112

1213
# TODO:
@@ -931,6 +932,10 @@ def test__all__(self):
931932
support.check__all__(self, gettext,
932933
not_exported={'c2py', 'ENOENT'})
933934

935+
@cpython_only
936+
def test_lazy_import(self):
937+
ensure_lazy_imports("gettext", {"re", "warnings", "locale"})
938+
934939

935940
if __name__ == '__main__':
936941
unittest.main()

Lib/test/test_locale.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
from decimal import Decimal
2-
from test.support import verbose, is_android, linked_to_musl, os_helper
2+
from test.support import cpython_only, verbose, is_android, linked_to_musl, os_helper
33
from test.support.warnings_helper import check_warnings
4-
from test.support.import_helper import import_fresh_module
4+
from test.support.import_helper import ensure_lazy_imports, import_fresh_module
55
from unittest import mock
66
import unittest
77
import locale
88
import sys
99
import codecs
1010

11+
class LazyImportTest(unittest.TestCase):
12+
@cpython_only
13+
def test_lazy_import(self):
14+
ensure_lazy_imports("locale", {"re", "warnings"})
15+
1116

1217
class BaseLocalizedTest(unittest.TestCase):
1318
#

Lib/test/test_mimetypes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import unittest.mock
77
from platform import win32_edition
88
from test import support
9-
from test.support import force_not_colorized, os_helper
9+
from test.support import cpython_only, force_not_colorized, os_helper
10+
from test.support.import_helper import ensure_lazy_imports
1011

1112
try:
1213
import _winapi
@@ -435,6 +436,10 @@ class MiscTestCase(unittest.TestCase):
435436
def test__all__(self):
436437
support.check__all__(self, mimetypes)
437438

439+
@cpython_only
440+
def test_lazy_import(self):
441+
ensure_lazy_imports("mimetypes", {"os", "posixpath", "urllib.parse", "argparse"})
442+
438443

439444
class CommandLineTest(unittest.TestCase):
440445
@force_not_colorized

0 commit comments

Comments
 (0)