Skip to content
Prev Previous commit
Next Next commit
Add test_lazy_import for ast, cmd, gettext, locale, optparse, pstats,…
… string and zipfile modules
  • Loading branch information
danielhollas committed May 5, 2025
commit eabf651be6553ed6ea5f9f110096e4a47e9103a6
7 changes: 7 additions & 0 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from test.support import os_helper
from test.support import skip_emscripten_stack_overflow, skip_wasi_stack_overflow
from test.support.ast_helper import ASTTestMixin
from test.support.import_helper import ensure_lazy_imports
from test.test_ast.utils import to_tuple
from test.test_ast.snippets import (
eval_tests, eval_results, exec_tests, exec_results, single_tests, single_results
Expand All @@ -47,6 +48,12 @@ def ast_repr_update_snapshots() -> None:
AST_REPR_DATA_FILE.write_text("\n".join(data))


class LazyImportTest(unittest.TestCase):
@support.cpython_only
def test_lazy_import(self):
ensure_lazy_imports("ast", {"contextlib", "enum"})
Comment thread
danielhollas marked this conversation as resolved.
Outdated


class AST_Tests(unittest.TestCase):
maxDiff = None

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class LazyImportTest(unittest.TestCase):
@cpython_only
def test_lazy_import(self):
ensure_lazy_imports("base64", {"re"})
ensure_lazy_imports("base64", {"re", "getopt"})


class LegacyBase64TestCase(unittest.TestCase):
Expand Down
8 changes: 7 additions & 1 deletion Lib/test/test_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@
import io
import textwrap
from test import support
from test.support.import_helper import import_module
from test.support.import_helper import ensure_lazy_imports, import_module
from test.support.pty_helper import run_pty

class LazyImportTest(unittest.TestCase):
@support.cpython_only
def test_lazy_import(self):
ensure_lazy_imports("cmd", {"inspect", "string"})


class samplecmdclass(cmd.Cmd):
"""
Instance the sampleclass:
Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from functools import partial

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


# TODO:
Expand Down Expand Up @@ -931,6 +932,10 @@ def test__all__(self):
support.check__all__(self, gettext,
not_exported={'c2py', 'ENOENT'})

@cpython_only
def test_lazy_import(self):
ensure_lazy_imports("gettext", {"re"})
Comment thread
danielhollas marked this conversation as resolved.
Outdated


if __name__ == '__main__':
unittest.main()
Expand Down
9 changes: 7 additions & 2 deletions Lib/test/test_locale.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from decimal import Decimal
from test.support import verbose, is_android, linked_to_musl, os_helper
from test.support import cpython_only, verbose, is_android, linked_to_musl, os_helper
from test.support.warnings_helper import check_warnings
from test.support.import_helper import import_fresh_module
from test.support.import_helper import ensure_lazy_imports, import_fresh_module
from unittest import mock
import unittest
import locale
import sys
import codecs

class LazyImportTest(unittest.TestCase):
@cpython_only
def test_lazy_import(self):
ensure_lazy_imports("locale", {"re"})
Comment thread
danielhollas marked this conversation as resolved.
Outdated


class BaseLocalizedTest(unittest.TestCase):
#
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def test__all__(self):

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


class CommandLineTest(unittest.TestCase):
Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_optparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

from io import StringIO
from test import support
from test.support import os_helper
from test.support import cpython_only, os_helper
from test.support.i18n_helper import TestTranslationsBase, update_translation_snapshots
from test.support.import_helper import ensure_lazy_imports

import optparse
from optparse import make_option, Option, \
Expand Down Expand Up @@ -1655,6 +1656,10 @@ def test__all__(self):
not_exported = {'check_builtin', 'AmbiguousOptionError', 'NO_DEFAULT'}
support.check__all__(self, optparse, not_exported=not_exported)

@cpython_only
def test_lazy_import(self):
ensure_lazy_imports("optparse", {"textwrap"})


class TestTranslations(TestTranslationsBase):
def test_translations(self):
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_pstats.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest

from test import support
from test.support.import_helper import ensure_lazy_imports
from io import StringIO
from pstats import SortKey
from enum import StrEnum, _test_simple_enum
Expand All @@ -10,6 +11,12 @@
import tempfile
import cProfile

class LazyImportTest(unittest.TestCase):
@support.cpython_only
def test_lazy_import(self):
ensure_lazy_imports("pstats", {"typing"})


class AddCallersTestCase(unittest.TestCase):
"""Tests for pstats.add_callers helper."""

Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_shlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import shlex
import string
import unittest
from test.support import cpython_only
from test.support import import_helper


Expand Down Expand Up @@ -364,6 +365,7 @@ def testPunctuationCharsReadOnly(self):
with self.assertRaises(AttributeError):
shlex_instance.punctuation_chars = False

@cpython_only
def test_lazy_imports(self):
import_helper.ensure_lazy_imports('shlex', {'collections', 're', 'os'})

Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_string/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
import string
from string import Template
import types
from test.support import cpython_only
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", "collections"})


class ModuleTest(unittest.TestCase):
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_zipfile/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
from test.support import (
findfile, requires_zlib, requires_bz2, requires_lzma,
captured_stdout, captured_stderr, requires_subprocess,
cpython_only
)
from test.support.os_helper import (
TESTFN, unlink, rmtree, temp_dir, temp_cwd, fd_count, FakePath
)
from test.support.import_helper import ensure_lazy_imports


TESTFN2 = TESTFN + "2"
Expand All @@ -49,6 +51,13 @@ def get_files(test):
yield f
test.assertFalse(f.closed)


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


class AbstractTestsWithSourceFile:
@classmethod
def setUpClass(cls):
Expand Down
Loading