add typing.AsyncContextManager and contextlib.asynccontextmanager (#1432)

Implements:
- https://github.com/python/typing/pull/438
- https://github.com/python/cpython/pull/360

Note that https://github.com/python/cpython/pull/1412, which adds
contextlib.AbstractAsyncContextManager, has not yet been merged.
This commit is contained in:
Jelle Zijlstra
2017-06-27 10:39:40 -07:00
committed by Guido van Rossum
parent 31d7393cae
commit 22f47fd478
2 changed files with 13 additions and 0 deletions

View File

@@ -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]]: ...

View File

@@ -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.