[Python-checkins] bpo-41543: contextlib.nullcontext can fill in for an async context manager (GH-21870)

asvetlov webhook-mailer at python.org
Mon Nov 9 07:34:16 EST 2020


https://github.com/python/cpython/commit/a117167d8dc8fa673a4646f509551c7950f824e5
commit: a117167d8dc8fa673a4646f509551c7950f824e5
branch: master
author: Tom Gringauz <tomgrin10 at gmail.com>
committer: asvetlov <andrew.svetlov at gmail.com>
date: 2020-11-09T14:34:07+02:00
summary:

bpo-41543: contextlib.nullcontext can fill in for an async context manager (GH-21870)

Co-authored-by: Andrew Svetlov <andrew.svetlov at gmail.com>

files:
A Misc/NEWS.d/next/Library/2020-08-14-00-39-04.bpo-41543.RpcRjb.rst
M Doc/library/contextlib.rst
M Lib/contextlib.py
M Lib/test/test_contextlib_async.py

diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst
index d5a1068a734fd..91edbba728347 100644
--- a/Doc/library/contextlib.rst
+++ b/Doc/library/contextlib.rst
@@ -243,8 +243,26 @@ Functions and classes provided:
           with cm as file:
               # Perform processing on the file
 
+   It can also be used as a stand-in for
+   :ref:`asynchronous context managers <async-context-managers>`::
+
+       async def send_http(session=None):
+          if not session:
+              # If no http session, create it with aiohttp
+              cm = aiohttp.ClientSession()
+          else:
+              # Caller is responsible for closing the session
+              cm = nullcontext(session)
+
+          async with cm as session:
+              # Send http requests with session
+
    .. versionadded:: 3.7
 
+   .. versionchanged:: 3.10
+      :term:`asynchronous context manager` support was added.
+
+
 
 .. function:: suppress(*exceptions)
 
diff --git a/Lib/contextlib.py b/Lib/contextlib.py
index 56b4968118bdb..a0b523c96fb28 100644
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -704,7 +704,7 @@ def _fix_exception_context(new_exc, old_exc):
         return received_exc and suppressed_exc
 
 
-class nullcontext(AbstractContextManager):
+class nullcontext(AbstractContextManager, AbstractAsyncContextManager):
     """Context manager that does no additional processing.
 
     Used as a stand-in for a normal context manager, when a particular
@@ -723,3 +723,9 @@ def __enter__(self):
 
     def __exit__(self, *excinfo):
         pass
+
+    async def __aenter__(self):
+        return self.enter_result
+
+    async def __aexit__(self, *excinfo):
+        pass
diff --git a/Lib/test/test_contextlib_async.py b/Lib/test/test_contextlib_async.py
index 109807d633d56..290ef05b82a80 100644
--- a/Lib/test/test_contextlib_async.py
+++ b/Lib/test/test_contextlib_async.py
@@ -1,5 +1,7 @@
 import asyncio
-from contextlib import aclosing, asynccontextmanager, AbstractAsyncContextManager, AsyncExitStack
+from contextlib import (
+    asynccontextmanager, AbstractAsyncContextManager,
+    AsyncExitStack, nullcontext, aclosing)
 import functools
 from test import support
 import unittest
@@ -537,5 +539,15 @@ async def suppress_exc(*exc_details):
         self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)
 
 
+class TestAsyncNullcontext(unittest.TestCase):
+    @_async_test
+    async def test_async_nullcontext(self):
+        class C:
+            pass
+        c = C()
+        async with nullcontext(c) as c_in:
+            self.assertIs(c_in, c)
+
+
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2020-08-14-00-39-04.bpo-41543.RpcRjb.rst b/Misc/NEWS.d/next/Library/2020-08-14-00-39-04.bpo-41543.RpcRjb.rst
new file mode 100644
index 0000000000000..753dc763f217c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-08-14-00-39-04.bpo-41543.RpcRjb.rst
@@ -0,0 +1 @@
+Add async context manager support for contextlib.nullcontext.



More information about the Python-checkins mailing list