Skip to content

Commit ecd8634

Browse files
update docs
1 parent 7bafa12 commit ecd8634

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

asyncstdlib/contextlib.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
def contextmanager(
3434
func: Callable[..., AsyncGenerator[T, None]]
3535
) -> Callable[..., AsyncContextManager[T]]:
36-
"""
36+
r"""
3737
Create an asynchronous context manager out of an asynchronous generator function
3838
3939
This is intended as a decorator for an asynchronous generator function.
@@ -44,14 +44,17 @@ def contextmanager(
4444
.. code-block:: python3
4545
4646
@contextmanager
47-
async def Context(*args, **kwargs):
47+
async def context(*args, **kwargs):
4848
# __aenter__
4949
yield # context value
5050
# __aexit__
5151
5252
Note that if an exception ends the context block, it gets re-raised at the ``yield``
5353
inside the asynchronous generator (via :py:meth:`~agen.athrow`). In order to handle
5454
this exception, the ``yield`` should be wrapped in a ``try`` statement.
55+
56+
The created context manager is a :py:class:`~.ContextDecorator` and can also be used
57+
as a decorator. It is automatically entered when a decorated function is ``await``\ ed.
5558
"""
5659

5760
@wraps(func)
@@ -70,7 +73,7 @@ class ContextDecorator(AsyncContextManager[T]):
7073
7174
.. code:: python3
7275
73-
class DecoratorAndContext(AsyncContextDecorator):
76+
class DecoratorAndContext(ContextDecorator):
7477
async def __aenter__(self) -> Any:
7578
print("entering", self)
7679
@@ -85,8 +88,8 @@ async def func():
8588
8689
Since functions are decorated with an existing context manager instance,
8790
the same instance is entered and exited on every call. If the context is
88-
not safe to be entered multiple times or even concurrently the subclass
89-
should implement the method `_recreate_cm(:Self) -> Self` to create a copy.
91+
not safe to be entered multiple times or even concurrently it should implement
92+
the method ``_recreate_cm(:Self) -> Self`` to create a copy of itself.
9093
"""
9194

9295
__slots__ = ()

docs/source/api/contextlib.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ Context Managers
2323

2424
.. versionadded:: 1.1.0
2525

26+
.. autoclass:: ContextDecorator[T]
27+
2628
.. py:function:: contextmanager(func: (...) → async iter T) (...)
2729
:async-with: :T
2830
:noindex:
2931

3032
.. autofunction:: contextmanager(func: (...) → async iter T) -> (...) → async with T
3133
:decorator:
3234

35+
.. versionadded:: 3.12.2
36+
37+
The created context manager is a :py:class:`~.ContextDecorator`.
38+
3339
.. autofunction:: closing(thing: AC)
3440
:async-with: :AC
3541

0 commit comments

Comments
 (0)