From 32977e0018acb4e44a7daef9792a9a3a528ff6d6 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 23 Jun 2017 21:29:13 -0700 Subject: [PATCH] add typing.AsyncContextManager and contextlib.asynccontextmanager Implements: - https://github.com/python/typing/pull/438 - https://github.com/python/cpython/pull/360 https://github.com/python/cpython/pull/1412, which adds contextlib.AbstractAsyncContextManager, has not yet been merged. --- stdlib/2and3/contextlib.pyi | 6 ++++++ stdlib/3/typing.pyi | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/stdlib/2and3/contextlib.pyi b/stdlib/2and3/contextlib.pyi index b4fa27cdc861..480546fb95ac 100644 --- a/stdlib/2and3/contextlib.pyi +++ b/stdlib/2and3/contextlib.pyi @@ -9,6 +9,9 @@ import sys # Aliased here for backwards compatibility; TODO eventually remove this from typing import ContextManager as ContextManager +if sys.version_info >= (3, 5): + from typing import AsyncContextManager, AsyncIterator + if sys.version_info >= (3, 6): from typing import ContextManager as AbstractContextManager @@ -26,6 +29,9 @@ if sys.version_info >= (3, 2): else: def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., ContextManager[_T]]: ... +if sys.version_info >= (3, 7): + def asynccontextmanager(func: Callable[..., AsyncIterator[_T]]) -> Callable[..., AsyncContextManager[_T]]: ... + if sys.version_info < (3,): def nested(*mgr: ContextManager[Any]) -> ContextManager[Iterable[Any]]: ... diff --git a/stdlib/3/typing.pyi b/stdlib/3/typing.pyi index c211591aa94c..7ec86a1ac389 100644 --- a/stdlib/3/typing.pyi +++ b/stdlib/3/typing.pyi @@ -291,6 +291,13 @@ class ContextManager(Generic[_T_co]): exc_value: Optional[BaseException], traceback: Optional[TracebackType]) -> Optional[bool]: ... +if sys.version_info >= (3, 5): + class AsyncContextManager(Generic[_T_co]): + def __aenter__(self) -> Awaitable[_T_co]: ... + def __aexit__(self, exc_type: Optional[Type[BaseException]], + exc_value: Optional[BaseException], + traceback: Optional[TracebackType]) -> Awaitable[Optional[bool]]: ... + class Mapping(_Collection[_KT], Generic[_KT, _VT_co]): # TODO: We wish the key type could also be covariant, but that doesn't work, # see discussion in https: //github.com/python/typing/pull/273.