8000 gh-118761: Add test_lazy_import for more modules by danielhollas · Pull Request #133057 · python/cpython · GitHub
[go: up one dir, main page]

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
Prev Previous commit
Next Next commit
Add test_lazy_import for pickle, mimetypes, base64, csv, pprint and s…
…ocket modules
  • Loading branch information
danielhollas committed May 5, 2025
commit 34e39abfdc1f6e8a825b69c5aac0550fe572d75c
8 changes: 8 additions & 0 deletions Lib/test/test_base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
import binascii
import os
from array import array
from test.support import cpython_only
from test.support import os_helper
from test.support import script_helper
from test.support.import_helper import ensure_lazy_imports


class LazyImportTest(unittest.TestCase):
@cpython_only
def test_lazy_import(self):
ensure_lazy_imports("base64", {"re"})


class LegacyBase64TestCase(unittest.TestCase):
Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import gc
import pickle
from test import support
from test.support import import_helper, check_disallow_instantiation
from test.support import cpython_only, import_helper, check_disallow_instantiation
from test.support.import_helper import ensure_lazy_imports
from itertools import permutations
from textwrap import dedent
from collections import OrderedDict
Expand Down Expand Up @@ -1565,6 +1566,10 @@ class MiscTestCase(unittest.TestCase):
def test__all__(self):
support.check__all__(self, csv, ('csv', '_csv'))

@cpython_only
def test_lazy_import(self):
ensure_lazy_imports("csv", {"re"})

def test_subclassable(self):
# issue 44089
class Foo(csv.Error): ...
Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import unittest.mock
from platform import win32_edition
from test import support
from test.support import os_helper
from test.support import cpython_only, os_helper
from test.support.import_helper import ensure_lazy_imports
from test.support.script_helper import run_python_until_end

try:
Expand Down Expand Up @@ -409,6 +410,10 @@ class MiscTestCase(unittest.TestCase):
def test__all__(self):
support.check__all__(self, mimetypes)

@cpython_only
def test_lazy_import(self):
ensure_lazy_imports("mimetypes", {"os", "posixpath", "urllib.parse"})


class CommandLineTest(unittest.TestCase):
def test_parse_args(self):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_is_notimplemented(self):
self.assertTrue(isinstance(pathlib.UnsupportedOperation(), NotImplementedError))


class TestImportTime(unittest.TestCase):
class LazyImportTest(unittest.TestCase):
@cpython_only
def test_lazy_import(self):
import_helper.ensure_lazy_imports("pathlib", {"shutil"})
Expand Down
9 changes: 8 additions & 1 deletion Lib/test/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import doctest
import unittest
from test import support
from test.support import import_helper, os_helper
from test.support import cpython_only, import_helper, os_helper
from test.support.import_helper import ensure_lazy_imports

from test.pickletester import AbstractHookTests
from test.pickletester import AbstractUnpickleTests
Expand All @@ -36,6 +37,12 @@
has_c_implementation = False


class LazyImportTest(unittest.TestCase):
@cpython_only
def test_lazy_import(self):
ensure_lazy_imports("pickle", {"re"})


class PyPickleTests(AbstractPickleModuleTests, unittest.TestCase):
dump = staticmethod(pickle._dump)
dumps = staticmethod(pickle._dumps)
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import types
import unittest

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

# list, tuple and dict subclasses that do or don't overwrite __repr__
class list2(list):
pass
Expand Down Expand Up @@ -129,6 +132,10 @@ def setUp(self):
self.b = list(range(200))
self.a[-12] = self.b

@cpython_only
def test_lazy_import(self):
ensure_lazy_imports("pprint", {"dataclasses", "re"})

def test_init(self):
pp = pprint.PrettyPrinter()
pp = pprint.PrettyPrinter(indent=4, width=40, depth=5,
Expand Down
9 changes: 8 additions & 1 deletion Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from unittest import mock
from test import support
from test.support import (
is_apple, os_helper, refleak_helper, socket_helper, threading_helper
cpython_only, is_apple, os_helper, refleak_helper, socket_helper, threading_helper
)
from test.support.import_helper import ensure_lazy_imports
import _thread as thread
import array
import contextlib
Expand Down Expand Up @@ -257,6 +258,12 @@ def downgrade_malformed_data_warning():
# Size in bytes of the int type
SIZEOF_INT = array.array("i").itemsize

class TestLazyImport(unittest.TestCase):
@cpython_only
def test_lazy_import(self):
ensure_lazy_imports("socket", {"array", "selectors"})


class SocketTCPTest(unittest.TestCase):

def setUp(self):
Expand Down
0