Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Doc/library/contextlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,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)

Expand Down
8 changes: 7 additions & 1 deletion Lib/contextlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,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
Expand All @@ -700,3 +700,9 @@ def __enter__(self):

def __exit__(self, *excinfo):
pass

async def __aenter__(self):
return self.enter_result

async def __aexit__(self, *excinfo):
pass
14 changes: 13 additions & 1 deletion Lib/test/test_contextlib_async.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -510,5 +512,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()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add async context manager support for contextlib.nullcontext.