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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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
Copy link
Member

Choose a reason for hiding this comment

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

I think this fits better in MiscTestCase (currently at around line 5286). In general I'd put this test close to test__all__ if present; it's sort of similar in testing the behavior of the whole module.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that's a very helpful comment as I am not familiar with cpython's test suite. Done.

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
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't fit well in this class, can you add a new test class instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

def test_lazy_import(self):
Copy link
Member

Choose a reason for hiding this comment

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

Would you mind adding the @cpython_only decorator, since these tests don't necessarily make sense on other Python implementations?

I should have done that on my PRs too but didn't think about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

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