[Python-checkins] bpo-40275: Avoid importing asyncio in test.support (GH-19600)

Serhiy Storchaka webhook-mailer at python.org
Sat Apr 25 03:04:15 EDT 2020


https://github.com/python/cpython/commit/3c8a5b459d68b4337776ada1e04f5b33f90a2275
commit: 3c8a5b459d68b4337776ada1e04f5b33f90a2275
branch: master
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-04-25T10:04:10+03:00
summary:

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.

files:
A Misc/NEWS.d/next/Library/2020-04-20-18-50-25.bpo-40275.Ofk6J8.rst
M Lib/test/support/__init__.py
M Lib/unittest/__init__.py

diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index f3868c1041542..2b3a4147246ee 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
@@ -3260,6 +3259,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 ace3a6fb1dd97..348dc471f4c3d 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
@@ -66,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
@@ -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}")
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 0000000000000..2093589f528b0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-04-20-18-50-25.bpo-40275.Ofk6J8.rst
@@ -0,0 +1,2 @@
+The :mod:`asyncio` package is now imported lazily in :mod:`unittest` only
+when the :class:`~unittest.IsolatedAsyncioTestCase` class is used.



More information about the Python-checkins mailing list