From a14d125ee03beb23ce10ba4c0ecc328ff8e450f6 Mon Sep 17 00:00:00 2001 From: Ilya Kulakov Date: Mon, 12 Feb 2018 13:54:32 -0800 Subject: [PATCH 1/5] Add stub for AsyncExitContext added by bpo-29302. --- stdlib/2and3/contextlib.pyi | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/stdlib/2and3/contextlib.pyi b/stdlib/2and3/contextlib.pyi index 21f640a17571..c3d1fb1f76e2 100644 --- a/stdlib/2and3/contextlib.pyi +++ b/stdlib/2and3/contextlib.pyi @@ -64,3 +64,29 @@ if sys.version_info >= (3,): def pop_all(self) -> ExitStack: ... def close(self) -> None: ... def __enter__(self: _U) -> _U: ... + +if sys.version_info >= (3, 7): + from asyncio.futures import Future + + _U = TypeVar('_U', bound='AsyncExitStack') + + _ExitCoroFunc = Callable[[Optional[Type[BaseException]], + Optional[BaseException], + Optional[TracebackType]], Future[bool]] + _CallbackCoroFunc = Callable[..., Future[None]] + _ACM_EF = TypeVar('_ACM_EF', AsyncContextManager, _ExitCoroFunc) + + class AsyncExitStack(AsyncContextManager[AsyncExitStack]): + def __init__(self) -> None: ... + def enter_context(self, cm: ContextManager[_T]) -> _T: ... + def enter_async_context(self, cm: AsyncContextManager[_T]) -> Future[_T]: ... + def push(self, exit: _CM_EF) -> _CM_EF: ... + def push_async_exit(self, exit: _ACM_EF) -> _ACM_EF: ... + def callback(self, callback: Callable[..., None], + *args: Any, **kwds: Any) -> Callable[..., None]: ... + def push_async_callback(self, callback: _CallbackCoroFunc, + *args: Any, **kwds: Any) -> _CallbackCoroFunc: ... + def pop_all(self) -> ExitStack: ... + def aclose(self) -> Future[None]: ... + def __enter__(self: _U) -> _U: ... + def __aenter__(self: _U) -> Future[_U]: ... From 7fe061dd1e55ce61537c8b383e3f0867fd0f80e5 Mon Sep 17 00:00:00 2001 From: Ilya Kulakov Date: Mon, 12 Feb 2018 16:29:13 -0800 Subject: [PATCH 2/5] Future -> Awaitable. --- stdlib/2and3/contextlib.pyi | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/stdlib/2and3/contextlib.pyi b/stdlib/2and3/contextlib.pyi index c3d1fb1f76e2..6a12bc959585 100644 --- a/stdlib/2and3/contextlib.pyi +++ b/stdlib/2and3/contextlib.pyi @@ -66,20 +66,20 @@ if sys.version_info >= (3,): def __enter__(self: _U) -> _U: ... if sys.version_info >= (3, 7): - from asyncio.futures import Future + from typing import Awaitable _U = TypeVar('_U', bound='AsyncExitStack') _ExitCoroFunc = Callable[[Optional[Type[BaseException]], Optional[BaseException], - Optional[TracebackType]], Future[bool]] - _CallbackCoroFunc = Callable[..., Future[None]] + Optional[TracebackType]], Awaitable[bool]] + _CallbackCoroFunc = Callable[..., Awaitable[None]] _ACM_EF = TypeVar('_ACM_EF', AsyncContextManager, _ExitCoroFunc) class AsyncExitStack(AsyncContextManager[AsyncExitStack]): def __init__(self) -> None: ... def enter_context(self, cm: ContextManager[_T]) -> _T: ... - def enter_async_context(self, cm: AsyncContextManager[_T]) -> Future[_T]: ... + def enter_async_context(self, cm: AsyncContextManager[_T]) -> Awaitable[_T]: ... def push(self, exit: _CM_EF) -> _CM_EF: ... def push_async_exit(self, exit: _ACM_EF) -> _ACM_EF: ... def callback(self, callback: Callable[..., None], @@ -87,6 +87,6 @@ if sys.version_info >= (3, 7): def push_async_callback(self, callback: _CallbackCoroFunc, *args: Any, **kwds: Any) -> _CallbackCoroFunc: ... def pop_all(self) -> ExitStack: ... - def aclose(self) -> Future[None]: ... + def aclose(self) -> Awaitable[None]: ... def __enter__(self: _U) -> _U: ... - def __aenter__(self: _U) -> Future[_U]: ... + def __aenter__(self: _U) -> Awaitable[_U]: ... From f207d24f0f5eb929a79e36e86a3a06511c0a898b Mon Sep 17 00:00:00 2001 From: Ilya Kulakov Date: Wed, 14 Feb 2018 14:28:23 -0800 Subject: [PATCH 3/5] Return type for pop_all should type(self). --- stdlib/2and3/contextlib.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/2and3/contextlib.pyi b/stdlib/2and3/contextlib.pyi index 6a12bc959585..7d90b6cefe38 100644 --- a/stdlib/2and3/contextlib.pyi +++ b/stdlib/2and3/contextlib.pyi @@ -86,7 +86,7 @@ if sys.version_info >= (3, 7): *args: Any, **kwds: Any) -> Callable[..., None]: ... def push_async_callback(self, callback: _CallbackCoroFunc, *args: Any, **kwds: Any) -> _CallbackCoroFunc: ... - def pop_all(self) -> ExitStack: ... + def pop_all(self: _U) -> _U: ... def aclose(self) -> Awaitable[None]: ... def __enter__(self: _U) -> _U: ... def __aenter__(self: _U) -> Awaitable[_U]: ... From 216b3b6c970e24c64f30cdd787c5e25df116e3fd Mon Sep 17 00:00:00 2001 From: Ilya Kulakov Date: Fri, 16 Feb 2018 17:00:02 -0800 Subject: [PATCH 4/5] AsyncExitStack is not a ContextManager and therefore has no __enter__. --- stdlib/2and3/contextlib.pyi | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/2and3/contextlib.pyi b/stdlib/2and3/contextlib.pyi index 7d90b6cefe38..c55c36479e19 100644 --- a/stdlib/2and3/contextlib.pyi +++ b/stdlib/2and3/contextlib.pyi @@ -88,5 +88,4 @@ if sys.version_info >= (3, 7): *args: Any, **kwds: Any) -> _CallbackCoroFunc: ... def pop_all(self: _U) -> _U: ... def aclose(self) -> Awaitable[None]: ... - def __enter__(self: _U) -> _U: ... def __aenter__(self: _U) -> Awaitable[_U]: ... From 598b57cddf4f7e9d8c50d56090c770a8708c1ba4 Mon Sep 17 00:00:00 2001 From: Ilya Kulakov Date: Fri, 16 Feb 2018 17:06:24 -0800 Subject: [PATCH 5/5] ExitStack's pop_all returns an instance of type(self). --- stdlib/2and3/contextlib.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/2and3/contextlib.pyi b/stdlib/2and3/contextlib.pyi index c55c36479e19..c7954ea37323 100644 --- a/stdlib/2and3/contextlib.pyi +++ b/stdlib/2and3/contextlib.pyi @@ -61,7 +61,7 @@ if sys.version_info >= (3,): def push(self, exit: _CM_EF) -> _CM_EF: ... def callback(self, callback: Callable[..., None], *args: Any, **kwds: Any) -> Callable[..., None]: ... - def pop_all(self) -> ExitStack: ... + def pop_all(self: _U) -> _U: ... def close(self) -> None: ... def __enter__(self: _U) -> _U: ...