From a45c6c6674268a6bbe57ac1361ef4a9aabf63d64 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 19 Apr 2020 14:17:10 +0300 Subject: [PATCH 1/5] bpo-40275: Avoid importing asyncio in test.support * Import IsolatedAsyncioTestCase lazily in unittest. * Import asyncio.events lazily in test.support. --- Lib/test/support/__init__.py | 2 +- Lib/unittest/__init__.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 9f43b4071c044d..9f9f04f995aae3 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -3,7 +3,6 @@ if __name__ != 'test.support': raise ImportError('support must be imported from the test package') -import asyncio.events import collections.abc import contextlib import errno @@ -3257,6 +3256,7 @@ def __gt__(self, other): def maybe_get_event_loop_policy(): """Return the global event loop policy if one is set, else return None.""" + import asyncio.events return asyncio.events._event_loop_policy # Helpers for testing hashing. diff --git a/Lib/unittest/__init__.py b/Lib/unittest/__init__.py index ace3a6fb1dd971..d72c94c7ff5a10 100644 --- a/Lib/unittest/__init__.py +++ b/Lib/unittest/__init__.py @@ -57,7 +57,6 @@ def testMultiply(self): __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 @@ -78,3 +77,9 @@ 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) + +def __getattr__(name): + if name == 'IsolatedAsyncioTestCase': + global IsolatedAsyncioTestCase + from .async_case import IsolatedAsyncioTestCase + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") From 212d7fdd551cd84455688339470ff2e7fa5a8e15 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 19 Apr 2020 15:57:04 +0300 Subject: [PATCH 2/5] Fix __getattr__ and add __dir__ in unittest. --- Lib/unittest/__init__.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/unittest/__init__.py b/Lib/unittest/__init__.py index d72c94c7ff5a10..ac2d2b7f42c5f7 100644 --- a/Lib/unittest/__init__.py +++ b/Lib/unittest/__init__.py @@ -78,8 +78,13 @@ def load_tests(loader, tests, pattern): this_dir = os.path.dirname(__file__) return loader.discover(start_dir=this_dir, pattern=pattern) + +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}") From ea6d6f270ae9e4dcea46b875b85740471a5c42ce Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 20 Apr 2020 18:46:41 +0300 Subject: [PATCH 3/5] Add comments. --- Lib/unittest/__init__.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/unittest/__init__.py b/Lib/unittest/__init__.py index ac2d2b7f42c5f7..348dc471f4c3d4 100644 --- a/Lib/unittest/__init__.py +++ b/Lib/unittest/__init__.py @@ -65,6 +65,7 @@ def testMultiply(self): 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 @@ -79,6 +80,10 @@ def load_tests(loader, tests, pattern): 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'} From edd3bc1a80d169292069d0a69e6de4424b0f767f Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 20 Apr 2020 18:50:30 +0300 Subject: [PATCH 4/5] Add blusrb. --- .../next/Library/2020-04-20-18-50-25.bpo-40275.Ofk6J8.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-04-20-18-50-25.bpo-40275.Ofk6J8.rst diff --git a/Misc/NEWS.d/next/Library/2020-04-20-18-50-25.bpo-40275.Ofk6J8.rst b/Misc/NEWS.d/next/Library/2020-04-20-18-50-25.bpo-40275.Ofk6J8.rst new file mode 100644 index 00000000000000..4f994d3bbadd93 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-04-20-18-50-25.bpo-40275.Ofk6J8.rst @@ -0,0 +1,2 @@ +The :class:`unittest.IsolatedAsyncioTestCase` class is now imported lazily +to avoid importing a large :mod:`asyncio` package when tests do not need it. From 41a52c8b1cbcfb7dad0f1069545c4b997bf1aed7 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 20 Apr 2020 19:12:40 +0300 Subject: [PATCH 5/5] Tweak wording of the NEWS entry. --- .../next/Library/2020-04-20-18-50-25.bpo-40275.Ofk6J8.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2020-04-20-18-50-25.bpo-40275.Ofk6J8.rst b/Misc/NEWS.d/next/Library/2020-04-20-18-50-25.bpo-40275.Ofk6J8.rst index 4f994d3bbadd93..2093589f528b05 100644 --- a/Misc/NEWS.d/next/Library/2020-04-20-18-50-25.bpo-40275.Ofk6J8.rst +++ b/Misc/NEWS.d/next/Library/2020-04-20-18-50-25.bpo-40275.Ofk6J8.rst @@ -1,2 +1,2 @@ -The :class:`unittest.IsolatedAsyncioTestCase` class is now imported lazily -to avoid importing a large :mod:`asyncio` package when tests do not need it. +The :mod:`asyncio` package is now imported lazily in :mod:`unittest` only +when the :class:`~unittest.IsolatedAsyncioTestCase` class is used.