diff options
author | 2020-04-25 10:04:10 +0300 | |
---|---|---|
committer | 2020-04-25 10:04:10 +0300 | |
commit | 3c8a5b459d68b4337776ada1e04f5b33f90a2275 (patch) | |
tree | f86166f552b0e9ff210dc81350b9b41178815b5d /Lib/unittest/__init__.py | |
parent | bpo-40279: Add some error-handling to the module initialisation docs example ... (diff) | |
download | cpython-3c8a5b459d68b4337776ada1e04f5b33f90a2275.tar.gz cpython-3c8a5b459d68b4337776ada1e04f5b33f90a2275.tar.bz2 cpython-3c8a5b459d68b4337776ada1e04f5b33f90a2275.zip |
bpo-40275: Avoid importing asyncio in test.support (GH-19600)
* Import asyncio lazily in unittest (only when IsolatedAsyncioTestCase is used).
* Import asyncio.events lazily in test.support.
Diffstat (limited to 'Lib/unittest/__init__.py')
-rw-r--r-- | Lib/unittest/__init__.py | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/Lib/unittest/__init__.py b/Lib/unittest/__init__.py index ace3a6fb1dd..348dc471f4c 100644 --- a/Lib/unittest/__init__.py +++ b/Lib/unittest/__init__.py @@ -57,7 +57,6 @@ __all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases']) __unittest = True from .result import TestResult -from .async_case import IsolatedAsyncioTestCase from .case import (addModuleCleanup, TestCase, FunctionTestCase, SkipTest, skip, skipIf, skipUnless, expectedFailure) from .suite import BaseTestSuite, TestSuite @@ -66,6 +65,7 @@ from .loader import (TestLoader, defaultTestLoader, makeSuite, getTestCaseNames, from .main import TestProgram, main from .runner import TextTestRunner, TextTestResult from .signals import installHandler, registerResult, removeResult, removeHandler +# IsolatedAsyncioTestCase will be imported lazily. # deprecated _TextTestResult = TextTestResult @@ -78,3 +78,18 @@ def load_tests(loader, tests, pattern): # top level directory cached on loader instance this_dir = os.path.dirname(__file__) return loader.discover(start_dir=this_dir, pattern=pattern) + + +# Lazy import of IsolatedAsyncioTestCase from .async_case +# It imports asyncio, which is relatively heavy, but most tests +# do not need it. + +def __dir__(): + return globals().keys() | {'IsolatedAsyncioTestCase'} + +def __getattr__(name): + if name == 'IsolatedAsyncioTestCase': + global IsolatedAsyncioTestCase + from .async_case import IsolatedAsyncioTestCase + return IsolatedAsyncioTestCase + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") |