From 5f830d023f5241cf5c34abe1eab9bba76ebdc548 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 8 Feb 2022 17:52:43 +0000 Subject: [PATCH] Backport more `Self`-related changes to Python 2 (#7166) --- stdlib/@python2/UserString.pyi | 6 ++---- stdlib/@python2/__builtin__.pyi | 6 +++--- stdlib/@python2/_collections.pyi | 3 +-- stdlib/@python2/_weakrefset.pyi | 18 +++++++++--------- stdlib/@python2/array.pyi | 3 ++- stdlib/@python2/builtins.pyi | 6 +++--- stdlib/@python2/collections.pyi | 8 ++++---- stdlib/@python2/typing.pyi | 11 +++++------ 8 files changed, 29 insertions(+), 32 deletions(-) diff --git a/stdlib/@python2/UserString.pyi b/stdlib/@python2/UserString.pyi index be802a9e2..7598fcaee 100644 --- a/stdlib/@python2/UserString.pyi +++ b/stdlib/@python2/UserString.pyi @@ -1,7 +1,5 @@ from _typeshed import Self -from typing import Any, Iterable, MutableSequence, Sequence, Text, TypeVar, overload - -_MST = TypeVar("_MST", bound=MutableString) +from typing import Any, Iterable, MutableSequence, Sequence, Text, overload class UserString(Sequence[UserString]): data: unicode @@ -70,5 +68,5 @@ class MutableString(UserString, MutableSequence[MutableString]): def __delitem__(self, index: int | slice) -> None: ... def immutable(self) -> UserString: ... def __iadd__(self: Self, other: Any) -> Self: ... - def __imul__(self, n: int) -> _MST: ... + def __imul__(self: Self, n: int) -> Self: ... def insert(self, index: int, value: Any) -> None: ... diff --git a/stdlib/@python2/__builtin__.pyi b/stdlib/@python2/__builtin__.pyi index 0039adaaa..f675020b8 100644 --- a/stdlib/@python2/__builtin__.pyi +++ b/stdlib/@python2/__builtin__.pyi @@ -708,9 +708,9 @@ class set(MutableSet[_T], Generic[_T]): def __contains__(self, o: object) -> bool: ... def __iter__(self) -> Iterator[_T]: ... def __and__(self, s: AbstractSet[object]) -> set[_T]: ... - def __iand__(self, s: AbstractSet[object]) -> set[_T]: ... + def __iand__(self: Self, s: AbstractSet[object]) -> Self: ... def __or__(self, s: AbstractSet[_S]) -> set[_T | _S]: ... - def __ior__(self, s: AbstractSet[_S]) -> set[_T | _S]: ... + def __ior__(self: Self, s: AbstractSet[_T]) -> Self: ... @overload def __sub__(self: set[str], s: AbstractSet[Text | None]) -> set[_T]: ... @overload @@ -720,7 +720,7 @@ class set(MutableSet[_T], Generic[_T]): @overload def __isub__(self, s: AbstractSet[_T | None]) -> set[_T]: ... def __xor__(self, s: AbstractSet[_S]) -> set[_T | _S]: ... - def __ixor__(self, s: AbstractSet[_S]) -> set[_T | _S]: ... + def __ixor__(self: Self, s: AbstractSet[_S]) -> Self: ... def __le__(self, s: AbstractSet[object]) -> bool: ... def __lt__(self, s: AbstractSet[object]) -> bool: ... def __ge__(self, s: AbstractSet[object]) -> bool: ... diff --git a/stdlib/@python2/_collections.pyi b/stdlib/@python2/_collections.pyi index 98509176d..6f0ee3f87 100644 --- a/stdlib/@python2/_collections.pyi +++ b/stdlib/@python2/_collections.pyi @@ -4,7 +4,6 @@ from typing import Any, Callable, Generic, Iterator, TypeVar _K = TypeVar("_K") _V = TypeVar("_V") _T = TypeVar("_T") -_T2 = TypeVar("_T2") class defaultdict(dict[_K, _V]): default_factory: None @@ -30,7 +29,7 @@ class deque(Generic[_T]): def __contains__(self, o: Any) -> bool: ... def __copy__(self) -> deque[_T]: ... def __getitem__(self, i: int) -> _T: ... - def __iadd__(self, other: deque[_T2]) -> deque[_T | _T2]: ... + def __iadd__(self: Self, other: deque[_T]) -> Self: ... def __iter__(self) -> Iterator[_T]: ... def __len__(self) -> int: ... def __reversed__(self) -> Iterator[_T]: ... diff --git a/stdlib/@python2/_weakrefset.pyi b/stdlib/@python2/_weakrefset.pyi index a6ca20d57..0d2c7fc42 100644 --- a/stdlib/@python2/_weakrefset.pyi +++ b/stdlib/@python2/_weakrefset.pyi @@ -16,15 +16,15 @@ class WeakSet(MutableSet[_T], Generic[_T]): def __contains__(self, item: object) -> bool: ... def __len__(self) -> int: ... def __iter__(self) -> Iterator[_T]: ... - def __ior__(self, other: Iterable[_S]) -> WeakSet[_S | _T]: ... + def __ior__(self: Self, other: Iterable[_T]) -> Self: ... # type: ignore[override,misc] def difference(self: Self, other: Iterable[_T]) -> Self: ... - def __sub__(self: Self, other: Iterable[_T]) -> Self: ... - def difference_update(self, other: Iterable[_T]) -> None: ... - def __isub__(self: Self, other: Iterable[_T]) -> Self: ... + def __sub__(self: Self, other: Iterable[Any]) -> Self: ... + def difference_update(self, other: Iterable[Any]) -> None: ... + def __isub__(self: Self, other: Iterable[Any]) -> Self: ... def intersection(self: Self, other: Iterable[_T]) -> Self: ... - def __and__(self: Self, other: Iterable[_T]) -> Self: ... - def intersection_update(self, other: Iterable[_T]) -> None: ... - def __iand__(self: Self, other: Iterable[_T]) -> Self: ... + def __and__(self: Self, other: Iterable[Any]) -> Self: ... + def intersection_update(self, other: Iterable[Any]) -> None: ... + def __iand__(self: Self, other: Iterable[Any]) -> Self: ... def issubset(self, other: Iterable[_T]) -> bool: ... def __le__(self, other: Iterable[_T]) -> bool: ... def __lt__(self, other: Iterable[_T]) -> bool: ... @@ -34,8 +34,8 @@ class WeakSet(MutableSet[_T], Generic[_T]): def __eq__(self, other: object) -> bool: ... def symmetric_difference(self, other: Iterable[_S]) -> WeakSet[_S | _T]: ... def __xor__(self, other: Iterable[_S]) -> WeakSet[_S | _T]: ... - def symmetric_difference_update(self, other: Iterable[Any]) -> None: ... - def __ixor__(self, other: Iterable[_S]) -> WeakSet[_S | _T]: ... + def symmetric_difference_update(self, other: Iterable[_T]) -> None: ... + def __ixor__(self: Self, other: Iterable[_T]) -> Self: ... # type: ignore[override,misc] def union(self, other: Iterable[_S]) -> WeakSet[_S | _T]: ... def __or__(self, other: Iterable[_S]) -> WeakSet[_S | _T]: ... def isdisjoint(self, other: Iterable[_T]) -> bool: ... diff --git a/stdlib/@python2/array.pyi b/stdlib/@python2/array.pyi index ea9b21a8e..81fee7972 100644 --- a/stdlib/@python2/array.pyi +++ b/stdlib/@python2/array.pyi @@ -1,3 +1,4 @@ +from _typeshed import Self from typing import Any, BinaryIO, Generic, Iterable, MutableSequence, Text, TypeVar, Union, overload from typing_extensions import Literal @@ -52,7 +53,7 @@ class array(MutableSequence[_T], Generic[_T]): def __add__(self, x: array[_T]) -> array[_T]: ... def __ge__(self, other: array[_T]) -> bool: ... def __gt__(self, other: array[_T]) -> bool: ... - def __iadd__(self, x: array[_T]) -> array[_T]: ... # type: ignore # Overrides MutableSequence + def __iadd__(self: Self, x: array[_T]) -> Self: ... # type: ignore[override] def __imul__(self, n: int) -> array[_T]: ... def __le__(self, other: array[_T]) -> bool: ... def __lt__(self, other: array[_T]) -> bool: ... diff --git a/stdlib/@python2/builtins.pyi b/stdlib/@python2/builtins.pyi index 0039adaaa..b6ac2b78a 100644 --- a/stdlib/@python2/builtins.pyi +++ b/stdlib/@python2/builtins.pyi @@ -708,9 +708,9 @@ class set(MutableSet[_T], Generic[_T]): def __contains__(self, o: object) -> bool: ... def __iter__(self) -> Iterator[_T]: ... def __and__(self, s: AbstractSet[object]) -> set[_T]: ... - def __iand__(self, s: AbstractSet[object]) -> set[_T]: ... + def __iand__(self: Self, s: AbstractSet[object]) -> Self: ... def __or__(self, s: AbstractSet[_S]) -> set[_T | _S]: ... - def __ior__(self, s: AbstractSet[_S]) -> set[_T | _S]: ... + def __ior__(self: Self, s: AbstractSet[_T]) -> Self: ... @overload def __sub__(self: set[str], s: AbstractSet[Text | None]) -> set[_T]: ... @overload @@ -720,7 +720,7 @@ class set(MutableSet[_T], Generic[_T]): @overload def __isub__(self, s: AbstractSet[_T | None]) -> set[_T]: ... def __xor__(self, s: AbstractSet[_S]) -> set[_T | _S]: ... - def __ixor__(self, s: AbstractSet[_S]) -> set[_T | _S]: ... + def __ixor__(self: Self, s: AbstractSet[_T]) -> Self: ... def __le__(self, s: AbstractSet[object]) -> bool: ... def __lt__(self, s: AbstractSet[object]) -> bool: ... def __ge__(self, s: AbstractSet[object]) -> bool: ... diff --git a/stdlib/@python2/collections.pyi b/stdlib/@python2/collections.pyi index ca2d78be5..e7a2e7d17 100644 --- a/stdlib/@python2/collections.pyi +++ b/stdlib/@python2/collections.pyi @@ -87,10 +87,10 @@ class Counter(dict[_T, int], Generic[_T]): def __sub__(self, other: Counter[_T]) -> Counter[_T]: ... def __and__(self, other: Counter[_T]) -> Counter[_T]: ... def __or__(self, other: Counter[_T]) -> Counter[_T]: ... - def __iadd__(self, other: Counter[_T]) -> Counter[_T]: ... - def __isub__(self, other: Counter[_T]) -> Counter[_T]: ... - def __iand__(self, other: Counter[_T]) -> Counter[_T]: ... - def __ior__(self, other: Counter[_T]) -> Counter[_T]: ... + def __iadd__(self: Self, other: Counter[_T]) -> Self: ... + def __isub__(self: Self, other: Counter[_T]) -> Self: ... + def __iand__(self: Self, other: Counter[_T]) -> Self: ... + def __ior__(self: Self, other: Counter[_T]) -> Self: ... class OrderedDict(dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]): def popitem(self, last: bool = ...) -> tuple[_KT, _VT]: ... diff --git a/stdlib/@python2/typing.pyi b/stdlib/@python2/typing.pyi index 34f5d9d46..affa82c82 100644 --- a/stdlib/@python2/typing.pyi +++ b/stdlib/@python2/typing.pyi @@ -58,7 +58,6 @@ NoReturn = Union[None] # These type variables are used by the container types. _T = TypeVar("_T") -_S = TypeVar("_S") _KT = TypeVar("_KT") # Key type. _VT = TypeVar("_VT") # Value type. _T_co = TypeVar("_T_co", covariant=True) # Any type covariant containers. @@ -211,7 +210,7 @@ class MutableSequence(Sequence[_T], Generic[_T]): def reverse(self) -> None: ... def pop(self, index: int = ...) -> _T: ... def remove(self, object: _T) -> None: ... - def __iadd__(self, x: Iterable[_T]) -> MutableSequence[_T]: ... + def __iadd__(self: Self, x: Iterable[_T]) -> Self: ... class AbstractSet(Iterable[_T_co], Container[_T_co], Generic[_T_co]): @abstractmethod @@ -240,10 +239,10 @@ class MutableSet(AbstractSet[_T], Generic[_T]): def clear(self) -> None: ... def pop(self) -> _T: ... def remove(self, element: _T) -> None: ... - def __ior__(self, s: AbstractSet[_S]) -> MutableSet[_T | _S]: ... - def __iand__(self, s: AbstractSet[Any]) -> MutableSet[_T]: ... - def __ixor__(self, s: AbstractSet[_S]) -> MutableSet[_T | _S]: ... - def __isub__(self, s: AbstractSet[Any]) -> MutableSet[_T]: ... + def __ior__(self: Self, s: AbstractSet[_T]) -> Self: ... + def __iand__(self: Self, s: AbstractSet[Any]) -> Self: ... + def __ixor__(self: Self, s: AbstractSet[_T]) -> Self: ... + def __isub__(self: Self, s: AbstractSet[Any]) -> Self: ... class MappingView(object): def __len__(self) -> int: ...