Skip to content

Commit cae660d

Browse files
gh-118761: Add test_lazy_import for more modules (#133057)
Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent b1aa515 commit cae660d

20 files changed

+139
-11
lines changed

Lib/test/test_ast/test_ast.py

+7
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

+8
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

+7-1
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

+6-1
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

+10
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

+6-1
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

+9
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

+6-1
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

+7-2
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

+6-1
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

Lib/test/test_optparse.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
from io import StringIO
1616
from test import support
17-
from test.support import os_helper
17+
from test.support import cpython_only, os_helper
1818
from test.support.i18n_helper import TestTranslationsBase, update_translation_snapshots
19+
from test.support.import_helper import ensure_lazy_imports
1920

2021
import optparse
2122
from optparse import make_option, Option, \
@@ -1655,6 +1656,10 @@ def test__all__(self):
16551656
not_exported = {'check_builtin', 'AmbiguousOptionError', 'NO_DEFAULT'}
16561657
support.check__all__(self, optparse, not_exported=not_exported)
16571658

1659+
@cpython_only
1660+
def test_lazy_import(self):
1661+
ensure_lazy_imports("optparse", {"textwrap"})
1662+
16581663

16591664
class TestTranslations(TestTranslationsBase):
16601665
def test_translations(self):

Lib/test/test_pathlib/test_pathlib.py

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from urllib.request import pathname2url
1717

1818
from test.support import import_helper
19+
from test.support import cpython_only
1920
from test.support import is_emscripten, is_wasi
2021
from test.support import infinite_recursion
2122
from test.support import os_helper
@@ -80,6 +81,12 @@ def test_is_notimplemented(self):
8081
self.assertTrue(isinstance(pathlib.UnsupportedOperation(), NotImplementedError))
8182

8283

84+
class LazyImportTest(unittest.TestCase):
85+
@cpython_only
86+
def test_lazy_import(self):
87+
import_helper.ensure_lazy_imports("pathlib", {"shutil"})
88+
89+
8390
#
8491
# Tests for the pure classes.
8592
#

Lib/test/test_pickle.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
import doctest
1616
import unittest
1717
from test import support
18-
from test.support import import_helper, os_helper
18+
from test.support import cpython_only, import_helper, os_helper
19+
from test.support.import_helper import ensure_lazy_imports
1920

2021
from test.pickletester import AbstractHookTests
2122
from test.pickletester import AbstractUnpickleTests
@@ -36,6 +37,12 @@
3637
has_c_implementation = False
3738

3839

40+
class LazyImportTest(unittest.TestCase):
41+
@cpython_only
42+
def test_lazy_import(self):
43+
ensure_lazy_imports("pickle", {"re"})
44+
45+
3946
class PyPickleTests(AbstractPickleModuleTests, unittest.TestCase):
4047
dump = staticmethod(pickle._dump)
4148
dumps = staticmethod(pickle._dumps)

Lib/test/test_pprint.py

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import types
1212
import unittest
1313

14+
from test.support import cpython_only
15+
from test.support.import_helper import ensure_lazy_imports
16+
1417
# list, tuple and dict subclasses that do or don't overwrite __repr__
1518
class list2(list):
1619
pass
@@ -129,6 +132,10 @@ def setUp(self):
129132
self.b = list(range(200))
130133
self.a[-12] = self.b
131134

135+
@cpython_only
136+
def test_lazy_import(self):
137+
ensure_lazy_imports("pprint", {"dataclasses", "re"})
138+
132139
def test_init(self):
133140
pp = pprint.PrettyPrinter()
134141
pp = pprint.PrettyPrinter(indent=4, width=40, depth=5,

Lib/test/test_pstats.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22

33
from test import support
4+
from test.support.import_helper import ensure_lazy_imports
45
from io import StringIO
56
from pstats import SortKey
67
from enum import StrEnum, _test_simple_enum
@@ -10,6 +11,12 @@
1011
import tempfile
1112
import cProfile
1213

14+
class LazyImportTest(unittest.TestCase):
15+
@support.cpython_only
16+
def test_lazy_import(self):
17+
ensure_lazy_imports("pstats", {"typing"})
18+
19+
1320
class AddCallersTestCase(unittest.TestCase):
1421
"""Tests for pstats.add_callers helper."""
1522

Lib/test/test_shlex.py

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import shlex
44
import string
55
import unittest
6+
from test.support import cpython_only
67
from test.support import import_helper
78

89

@@ -364,6 +365,7 @@ def testPunctuationCharsReadOnly(self):
364365
with self.assertRaises(AttributeError):
365366
shlex_instance.punctuation_chars = False
366367

368+
@cpython_only
367369
def test_lazy_imports(self):
368370
import_helper.ensure_lazy_imports('shlex', {'collections', 're', 'os'})
369371

Lib/test/test_socket.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
from unittest import mock
33
from test import support
44
from test.support import (
5-
is_apple, os_helper, refleak_helper, socket_helper, threading_helper
5+
cpython_only, is_apple, os_helper, refleak_helper, socket_helper, threading_helper
66
)
7+
from test.support.import_helper import ensure_lazy_imports
78
import _thread as thread
89
import array
910
import contextlib
@@ -257,6 +258,12 @@ def downgrade_malformed_data_warning():
257258
# Size in bytes of the int type
258259
SIZEOF_INT = array.array("i").itemsize
259260

261+
class TestLazyImport(unittest.TestCase):
262+
@cpython_only
263+
def test_lazy_import(self):
264+
ensure_lazy_imports("socket", {"array", "selectors"})
265+
266+
260267
class SocketTCPTest(unittest.TestCase):
261268

262269
def setUp(self):

Lib/test/test_string/test_string.py

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
import string
33
from string import Template
44
import types
5+
from test.support import cpython_only
6+
from test.support.import_helper import ensure_lazy_imports
7+
8+
9+
class LazyImportTest(unittest.TestCase):
10+
@cpython_only
11+
def test_lazy_import(self):
12+
ensure_lazy_imports("base64", {"re", "collections"})
513

614

715
class ModuleTest(unittest.TestCase):

Lib/test/test_threading.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import test.support
66
from test.support import threading_helper, requires_subprocess, requires_gil_enabled
77
from test.support import verbose, cpython_only, os_helper
8-
from test.support.import_helper import import_module
8+
from test.support.import_helper import ensure_lazy_imports, import_module
99
from test.support.script_helper import assert_python_ok, assert_python_failure
1010
from test.support import force_not_colorized
1111

@@ -120,6 +120,10 @@ def tearDown(self):
120120
class ThreadTests(BaseTestCase):
121121
maxDiff = 9999
122122

123+
@cpython_only
124+
def test_lazy_import(self):
125+
ensure_lazy_imports("threading", {"functools", "warnings"})
126+
123127
@cpython_only
124128
def test_name(self):
125129
def func(): pass

0 commit comments

Comments
 (0)