Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Self in async context managers #5724

Merged
merged 4 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions stdlib/asyncio/events.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ssl
import sys
from _typeshed import FileDescriptorLike
from _typeshed import FileDescriptorLike, Self
from abc import ABCMeta, abstractmethod
from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
from typing import IO, Any, Awaitable, Callable, Dict, Generator, List, Optional, Sequence, Tuple, TypeVar, Union, overload
Expand Down Expand Up @@ -57,7 +57,7 @@ class TimerHandle(Handle):
class AbstractServer:
def close(self) -> None: ...
if sys.version_info >= (3, 7):
async def __aenter__(self: _T) -> _T: ...
async def __aenter__(self: Self) -> Self: ...
async def __aexit__(self, *exc: Any) -> None: ...
def get_loop(self) -> AbstractEventLoop: ...
def is_serving(self) -> bool: ...
Expand Down
10 changes: 3 additions & 7 deletions stdlib/contextlib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,12 @@ class redirect_stderr(ContextManager[_T_io]):
class ContextDecorator:
def __call__(self, func: _F) -> _F: ...

_U = TypeVar("_U", bound=ExitStack)

class ExitStack(ContextManager[ExitStack]):
def __init__(self) -> None: ...
def enter_context(self, cm: ContextManager[_T]) -> _T: ...
def push(self, exit: _CM_EF) -> _CM_EF: ...
def callback(self, callback: Callable[..., Any], *args: Any, **kwds: Any) -> Callable[..., Any]: ...
def pop_all(self: _U) -> _U: ...
def pop_all(self: Self) -> Self: ...
def close(self) -> None: ...
def __enter__(self: Self) -> Self: ...
def __exit__(
Expand All @@ -90,8 +88,6 @@ class ExitStack(ContextManager[ExitStack]):
) -> bool: ...

if sys.version_info >= (3, 7):
_S = TypeVar("_S", bound=AsyncExitStack)

_ExitCoroFunc = Callable[[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]], Awaitable[bool]]
_CallbackCoroFunc = Callable[..., Awaitable[Any]]
_ACM_EF = TypeVar("_ACM_EF", AsyncContextManager[Any], _ExitCoroFunc)
Expand All @@ -103,9 +99,9 @@ if sys.version_info >= (3, 7):
def push_async_exit(self, exit: _ACM_EF) -> _ACM_EF: ...
def callback(self, callback: Callable[..., Any], *args: Any, **kwds: Any) -> Callable[..., Any]: ...
def push_async_callback(self, callback: _CallbackCoroFunc, *args: Any, **kwds: Any) -> _CallbackCoroFunc: ...
def pop_all(self: _S) -> _S: ...
def pop_all(self: Self) -> Self: ...
Copy link
Contributor

@graingert graingert Jul 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is on _BaseExitStack

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of _BaseExitStack is to avoid copy/pasta in the implementation. On the one hand, we don't need it in typeshed because the stubs are short anyway, but on the other hand, it is generally good if stubs match the implementation. Either way, adding _BaseExitStack is beyond the scope of this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be fixed on both copies of pop_all instead

def aclose(self) -> Awaitable[None]: ...
def __aenter__(self: _S) -> Awaitable[_S]: ...
def __aenter__(self: Self) -> Awaitable[Self]: ...
def __aexit__(
self,
__exc_type: Optional[Type[BaseException]],
Expand Down
3 changes: 2 additions & 1 deletion stubs/aiofiles/aiofiles/base.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from _typeshed import Self
from types import CodeType, FrameType, TracebackType, coroutine
from typing import Any, Coroutine, Generator, Generic, Iterator, Optional, Type, TypeVar, Union

Expand All @@ -8,7 +9,7 @@ _T_contra = TypeVar("_T_contra", contravariant=True)

class AsyncBase(Generic[_T]):
def __init__(self, file: str, loop: Any, executor: Any) -> None: ...
async def __aiter__(self) -> Iterator[_T]: ...
async def __aiter__(self: Self) -> Self: ...
async def __anext__(self) -> _T: ...

class AiofilesContextManager(Generic[_T_co, _T_contra, _V_co]):
Expand Down