From 6882e513c2ca50319d4e7f318b9ccd4d72fd372e Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Sun, 13 Feb 2022 14:21:01 +0300 Subject: [PATCH] Improve `Self` type usage accross `stdlib` (#7183) --- stdlib/asyncio/futures.pyi | 12 ++++++------ stdlib/collections/__init__.pyi | 6 ++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/stdlib/asyncio/futures.pyi b/stdlib/asyncio/futures.pyi index a3caa670e..ec27e6b6d 100644 --- a/stdlib/asyncio/futures.pyi +++ b/stdlib/asyncio/futures.pyi @@ -1,4 +1,5 @@ import sys +from _typeshed import Self from concurrent.futures._base import Error, Future as _ConcurrentFuture from typing import Any, Awaitable, Callable, Generator, Iterable, TypeVar @@ -16,7 +17,6 @@ if sys.version_info >= (3, 9): from types import GenericAlias _T = TypeVar("_T") -_S = TypeVar("_S") if sys.version_info < (3, 7): class _TracebackLogger: @@ -39,12 +39,12 @@ class Future(Awaitable[_T], Iterable[_T]): def __del__(self) -> None: ... if sys.version_info >= (3, 7): def get_loop(self) -> AbstractEventLoop: ... - def _callbacks(self: _S) -> list[tuple[Callable[[_S], Any], Context]]: ... - def add_done_callback(self: _S, __fn: Callable[[_S], Any], *, context: Context | None = ...) -> None: ... + def _callbacks(self: Self) -> list[tuple[Callable[[Self], Any], Context]]: ... + def add_done_callback(self: Self, __fn: Callable[[Self], Any], *, context: Context | None = ...) -> None: ... else: @property - def _callbacks(self: _S) -> list[Callable[[_S], Any]]: ... - def add_done_callback(self: _S, __fn: Callable[[_S], Any]) -> None: ... + def _callbacks(self: Self) -> list[Callable[[Self], Any]]: ... + def add_done_callback(self: Self, __fn: Callable[[Self], Any]) -> None: ... if sys.version_info >= (3, 9): def cancel(self, msg: Any | None = ...) -> bool: ... else: @@ -54,7 +54,7 @@ class Future(Awaitable[_T], Iterable[_T]): def done(self) -> bool: ... def result(self) -> _T: ... def exception(self) -> BaseException | None: ... - def remove_done_callback(self: _S, __fn: Callable[[_S], Any]) -> int: ... + def remove_done_callback(self: Self, __fn: Callable[[Self], Any]) -> int: ... def set_result(self, __result: _T) -> None: ... def set_exception(self, __exception: type | BaseException) -> None: ... def __iter__(self) -> Generator[Any, None, _T]: ... diff --git a/stdlib/collections/__init__.pyi b/stdlib/collections/__init__.pyi index c6c40f608..5bc282bf3 100644 --- a/stdlib/collections/__init__.pyi +++ b/stdlib/collections/__init__.pyi @@ -112,8 +112,6 @@ class UserList(MutableSequence[_T]): def sort(self, *, key: Callable[[_T], SupportsRichComparison], reverse: bool = ...) -> None: ... def extend(self, other: Iterable[_T]) -> None: ... -_UserStringT = TypeVar("_UserStringT", bound=UserString) - class UserString(Sequence[str]): data: str def __init__(self, seq: object) -> None: ... @@ -129,8 +127,8 @@ class UserString(Sequence[str]): def __len__(self) -> int: ... # It should return a str to implement Sequence correctly, but it doesn't. def __getitem__(self: Self, i: SupportsIndex | slice) -> Self: ... # type: ignore[override] - def __iter__(self: _UserStringT) -> Iterator[_UserStringT]: ... # type: ignore[override] - def __reversed__(self: _UserStringT) -> Iterator[_UserStringT]: ... # type: ignore[override] + def __iter__(self: Self) -> Iterator[Self]: ... # type: ignore[override] + def __reversed__(self: Self) -> Iterator[Self]: ... # type: ignore[override] def __add__(self: Self, other: object) -> Self: ... def __mul__(self: Self, n: int) -> Self: ... def __mod__(self: Self, args: Any) -> Self: ...