Skip to content

Commit 22f47fd

Browse files
JelleZijlstragvanrossum
authored andcommitted
add typing.AsyncContextManager and contextlib.asynccontextmanager (#1432)
Implements: - python/typing#438 - python/cpython#360 Note that python/cpython#1412, which adds contextlib.AbstractAsyncContextManager, has not yet been merged.
1 parent 31d7393 commit 22f47fd

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

stdlib/2and3/contextlib.pyi

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import sys
99
# Aliased here for backwards compatibility; TODO eventually remove this
1010
from typing import ContextManager as ContextManager
1111

12+
if sys.version_info >= (3, 5):
13+
from typing import AsyncContextManager, AsyncIterator
14+
1215
if sys.version_info >= (3, 6):
1316
from typing import ContextManager as AbstractContextManager
1417

@@ -26,6 +29,9 @@ if sys.version_info >= (3, 2):
2629
else:
2730
def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., ContextManager[_T]]: ...
2831

32+
if sys.version_info >= (3, 7):
33+
def asynccontextmanager(func: Callable[..., AsyncIterator[_T]]) -> Callable[..., AsyncContextManager[_T]]: ...
34+
2935
if sys.version_info < (3,):
3036
def nested(*mgr: ContextManager[Any]) -> ContextManager[Iterable[Any]]: ...
3137

stdlib/3/typing.pyi

+7
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ class ContextManager(Generic[_T_co]):
291291
exc_value: Optional[BaseException],
292292
traceback: Optional[TracebackType]) -> Optional[bool]: ...
293293

294+
if sys.version_info >= (3, 5):
295+
class AsyncContextManager(Generic[_T_co]):
296+
def __aenter__(self) -> Awaitable[_T_co]: ...
297+
def __aexit__(self, exc_type: Optional[Type[BaseException]],
298+
exc_value: Optional[BaseException],
299+
traceback: Optional[TracebackType]) -> Awaitable[Optional[bool]]: ...
300+
294301
class Mapping(_Collection[_KT], Generic[_KT, _VT_co]):
295302
# TODO: We wish the key type could also be covariant, but that doesn't work,
296303
# see discussion in https: //github.com/python/typing/pull/273.

0 commit comments

Comments
 (0)