Skip to content

gh-118761: Add test_lazy_import for more modules #133057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 5, 2025
10 changes: 10 additions & 0 deletions Lib/test/test_email/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
import time
import unittest

from test.support import cpython_only
from test.support.import_helper import ensure_lazy_imports


class TestImportTime(unittest.TestCase):

@cpython_only
def test_lazy_import(self):
ensure_lazy_imports("email.utils", {"random", "socket"})


class DateTimeTests(unittest.TestCase):

Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL
from test import support
from test.support import ALWAYS_EQ, REPO_ROOT
from test.support import threading_helper
from test.support import threading_helper, cpython_only
from test.support.import_helper import ensure_lazy_imports
from datetime import timedelta

python_version = sys.version_info[:2]
Expand Down Expand Up @@ -1274,6 +1275,10 @@ class Holiday(date, Enum):
IDES_OF_MARCH = 2013, 3, 15
self.Holiday = Holiday

@cpython_only
def test_lazy_import(self):
ensure_lazy_imports("enum", {"functools", "warnings", "inspect", "re"})

def test_bool(self):
# plain Enum members are always True
class Logic(Enum):
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from test.support import import_helper
from test.support import threading_helper
from test.support import cpython_only
from test.support import EqualToForwardRef

import functools
Expand Down Expand Up @@ -63,6 +64,14 @@ def __add__(self, other):
class MyDict(dict):
pass

class TestImportTime(unittest.TestCase):

@cpython_only
def test_lazy_import(self):
import_helper.ensure_lazy_imports(
"functools", {"os", "weakref", "typing", "annotationlib", "warnings"}
)


class TestPartial:

Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from urllib.request import pathname2url

from test.support import import_helper
from test.support import cpython_only
from test.support import is_emscripten, is_wasi
from test.support import infinite_recursion
from test.support import os_helper
Expand Down Expand Up @@ -129,6 +130,10 @@ class StrSubclass(str):
for part in p.parts:
self.assertIs(type(part), str)

@cpython_only
def test_lazy_import(self):
import_helper.ensure_lazy_imports("pathlib", {"shutil"})

def test_str_subclass_common(self):
self._check_str_subclass('')
self._check_str_subclass('.')
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import test.support
from test.support import threading_helper, requires_subprocess, requires_gil_enabled
from test.support import verbose, cpython_only, os_helper
from test.support.import_helper import import_module
from test.support.import_helper import ensure_lazy_imports, import_module
from test.support.script_helper import assert_python_ok, assert_python_failure
from test.support import force_not_colorized

Expand Down Expand Up @@ -120,6 +120,10 @@ def tearDown(self):
class ThreadTests(BaseTestCase):
maxDiff = 9999

@cpython_only
def test_lazy_import(self):
ensure_lazy_imports("threading", {"functools", "warnings"})

@cpython_only
def test_name(self):
def func(): pass
Expand Down
Loading