bpo-41543: contextlib.nullcontext can fill in for an async context manager (GH-21870)
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst
index d5a1068..91edbba 100644
--- a/Doc/library/contextlib.rst
+++ b/Doc/library/contextlib.rst
@@ -243,8 +243,26 @@
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)