Use typing_extensions.Self in the stdlib (#9694)

This commit is contained in:
Alex Waygood
2023-02-09 09:12:13 +00:00
committed by GitHub
parent 10086c06a1
commit 9ed39d8796
98 changed files with 627 additions and 654 deletions

View File

@@ -1,8 +1,8 @@
import sys
from _collections_abc import dict_items, dict_keys, dict_values
from _typeshed import Self, SupportsKeysAndGetItem, SupportsRichComparison, SupportsRichComparisonT
from _typeshed import SupportsKeysAndGetItem, SupportsRichComparison, SupportsRichComparisonT
from typing import Any, Generic, NoReturn, TypeVar, overload
from typing_extensions import SupportsIndex, final
from typing_extensions import Self, SupportsIndex, final
if sys.version_info >= (3, 9):
from types import GenericAlias
@@ -68,8 +68,8 @@ class UserDict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def __delitem__(self, key: _KT) -> None: ...
def __iter__(self) -> Iterator[_KT]: ...
def __contains__(self, key: object) -> bool: ...
def copy(self: Self) -> Self: ...
def __copy__(self: Self) -> Self: ...
def copy(self) -> Self: ...
def __copy__(self) -> Self: ...
# `UserDict.fromkeys` has the same semantics as `dict.fromkeys`, so should be kept in line with `dict.fromkeys`.
# TODO: Much like `dict.fromkeys`, the true signature of `UserDict.fromkeys` is inexpressible in the current type system.
@@ -85,9 +85,9 @@ class UserDict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def __ror__(self, other: UserDict[_T1, _T2] | dict[_T1, _T2]) -> UserDict[_KT | _T1, _VT | _T2]: ... # type: ignore[misc]
# UserDict.__ior__ should be kept roughly in line with MutableMapping.update()
@overload # type: ignore[misc]
def __ior__(self: Self, other: SupportsKeysAndGetItem[_KT, _VT]) -> Self: ...
def __ior__(self, other: SupportsKeysAndGetItem[_KT, _VT]) -> Self: ...
@overload
def __ior__(self: Self, other: Iterable[tuple[_KT, _VT]]) -> Self: ...
def __ior__(self, other: Iterable[tuple[_KT, _VT]]) -> Self: ...
class UserList(MutableSequence[_T]):
data: list[_T]
@@ -105,24 +105,24 @@ class UserList(MutableSequence[_T]):
@overload
def __getitem__(self, i: SupportsIndex) -> _T: ...
@overload
def __getitem__(self: Self, i: slice) -> Self: ...
def __getitem__(self, i: slice) -> Self: ...
@overload
def __setitem__(self, i: SupportsIndex, item: _T) -> None: ...
@overload
def __setitem__(self, i: slice, item: Iterable[_T]) -> None: ...
def __delitem__(self, i: SupportsIndex | slice) -> None: ...
def __add__(self: Self, other: Iterable[_T]) -> Self: ...
def __radd__(self: Self, other: Iterable[_T]) -> Self: ...
def __iadd__(self: Self, other: Iterable[_T]) -> Self: ...
def __mul__(self: Self, n: int) -> Self: ...
def __rmul__(self: Self, n: int) -> Self: ...
def __imul__(self: Self, n: int) -> Self: ...
def __add__(self, other: Iterable[_T]) -> Self: ...
def __radd__(self, other: Iterable[_T]) -> Self: ...
def __iadd__(self, other: Iterable[_T]) -> Self: ...
def __mul__(self, n: int) -> Self: ...
def __rmul__(self, n: int) -> Self: ...
def __imul__(self, n: int) -> Self: ...
def append(self, item: _T) -> None: ...
def insert(self, i: int, item: _T) -> None: ...
def pop(self, i: int = -1) -> _T: ...
def remove(self, item: _T) -> None: ...
def copy(self: Self) -> Self: ...
def __copy__(self: Self) -> Self: ...
def copy(self) -> Self: ...
def __copy__(self) -> Self: ...
def count(self, item: _T) -> int: ...
# All arguments are passed to `list.index` at runtime, so the signature should be kept in line with `list.index`.
def index(self, item: _T, __start: SupportsIndex = 0, __stop: SupportsIndex = sys.maxsize) -> int: ...
@@ -147,30 +147,30 @@ class UserString(Sequence[UserString]):
def __eq__(self, string: object) -> bool: ...
def __contains__(self, char: object) -> bool: ...
def __len__(self) -> int: ...
def __getitem__(self: Self, index: SupportsIndex | slice) -> Self: ...
def __iter__(self: Self) -> Iterator[Self]: ...
def __reversed__(self: Self) -> Iterator[Self]: ...
def __add__(self: Self, other: object) -> Self: ...
def __radd__(self: Self, other: object) -> Self: ...
def __mul__(self: Self, n: int) -> Self: ...
def __rmul__(self: Self, n: int) -> Self: ...
def __mod__(self: Self, args: Any) -> Self: ...
def __getitem__(self, index: SupportsIndex | slice) -> Self: ...
def __iter__(self) -> Iterator[Self]: ...
def __reversed__(self) -> Iterator[Self]: ...
def __add__(self, other: object) -> Self: ...
def __radd__(self, other: object) -> Self: ...
def __mul__(self, n: int) -> Self: ...
def __rmul__(self, n: int) -> Self: ...
def __mod__(self, args: Any) -> Self: ...
if sys.version_info >= (3, 8):
def __rmod__(self: Self, template: object) -> Self: ...
def __rmod__(self, template: object) -> Self: ...
else:
def __rmod__(self: Self, format: Any) -> Self: ...
def __rmod__(self, format: Any) -> Self: ...
def capitalize(self: Self) -> Self: ...
def casefold(self: Self) -> Self: ...
def center(self: Self, width: int, *args: Any) -> Self: ...
def capitalize(self) -> Self: ...
def casefold(self) -> Self: ...
def center(self, width: int, *args: Any) -> Self: ...
def count(self, sub: str | UserString, start: int = 0, end: int = sys.maxsize) -> int: ...
if sys.version_info >= (3, 8):
def encode(self: UserString, encoding: str | None = "utf-8", errors: str | None = "strict") -> bytes: ...
else:
def encode(self: Self, encoding: str | None = None, errors: str | None = None) -> Self: ...
def encode(self, encoding: str | None = None, errors: str | None = None) -> Self: ...
def endswith(self, suffix: str | tuple[str, ...], start: int | None = 0, end: int | None = sys.maxsize) -> bool: ...
def expandtabs(self: Self, tabsize: int = 8) -> Self: ...
def expandtabs(self, tabsize: int = 8) -> Self: ...
def find(self, sub: str | UserString, start: int = 0, end: int = sys.maxsize) -> int: ...
def format(self, *args: Any, **kwds: Any) -> str: ...
def format_map(self, mapping: Mapping[str, Any]) -> str: ...
@@ -188,31 +188,31 @@ class UserString(Sequence[UserString]):
def isupper(self) -> bool: ...
def isascii(self) -> bool: ...
def join(self, seq: Iterable[str]) -> str: ...
def ljust(self: Self, width: int, *args: Any) -> Self: ...
def lower(self: Self) -> Self: ...
def lstrip(self: Self, chars: str | None = None) -> Self: ...
def ljust(self, width: int, *args: Any) -> Self: ...
def lower(self) -> Self: ...
def lstrip(self, chars: str | None = None) -> Self: ...
maketrans = str.maketrans
def partition(self, sep: str) -> tuple[str, str, str]: ...
if sys.version_info >= (3, 9):
def removeprefix(self: Self, __prefix: str | UserString) -> Self: ...
def removesuffix(self: Self, __suffix: str | UserString) -> Self: ...
def removeprefix(self, __prefix: str | UserString) -> Self: ...
def removesuffix(self, __suffix: str | UserString) -> Self: ...
def replace(self: Self, old: str | UserString, new: str | UserString, maxsplit: int = -1) -> Self: ...
def replace(self, old: str | UserString, new: str | UserString, maxsplit: int = -1) -> Self: ...
def rfind(self, sub: str | UserString, start: int = 0, end: int = sys.maxsize) -> int: ...
def rindex(self, sub: str | UserString, start: int = 0, end: int = sys.maxsize) -> int: ...
def rjust(self: Self, width: int, *args: Any) -> Self: ...
def rjust(self, width: int, *args: Any) -> Self: ...
def rpartition(self, sep: str) -> tuple[str, str, str]: ...
def rstrip(self: Self, chars: str | None = None) -> Self: ...
def rstrip(self, chars: str | None = None) -> Self: ...
def split(self, sep: str | None = None, maxsplit: int = -1) -> list[str]: ...
def rsplit(self, sep: str | None = None, maxsplit: int = -1) -> list[str]: ...
def splitlines(self, keepends: bool = False) -> list[str]: ...
def startswith(self, prefix: str | tuple[str, ...], start: int | None = 0, end: int | None = sys.maxsize) -> bool: ...
def strip(self: Self, chars: str | None = None) -> Self: ...
def swapcase(self: Self) -> Self: ...
def title(self: Self) -> Self: ...
def translate(self: Self, *args: Any) -> Self: ...
def upper(self: Self) -> Self: ...
def zfill(self: Self, width: int) -> Self: ...
def strip(self, chars: str | None = None) -> Self: ...
def swapcase(self) -> Self: ...
def title(self) -> Self: ...
def translate(self, *args: Any) -> Self: ...
def upper(self) -> Self: ...
def zfill(self, width: int) -> Self: ...
class deque(MutableSequence[_T], Generic[_T]):
@property
@@ -223,7 +223,7 @@ class deque(MutableSequence[_T], Generic[_T]):
def __init__(self, iterable: Iterable[_T], maxlen: int | None = None) -> None: ...
def append(self, __x: _T) -> None: ...
def appendleft(self, __x: _T) -> None: ...
def copy(self: Self) -> Self: ...
def copy(self) -> Self: ...
def count(self, __x: _T) -> int: ...
def extend(self, __iterable: Iterable[_T]) -> None: ...
def extendleft(self, __iterable: Iterable[_T]) -> None: ...
@@ -233,18 +233,18 @@ class deque(MutableSequence[_T], Generic[_T]):
def popleft(self) -> _T: ...
def remove(self, __value: _T) -> None: ...
def rotate(self, __n: int = 1) -> None: ...
def __copy__(self: Self) -> Self: ...
def __copy__(self) -> Self: ...
def __len__(self) -> int: ...
# These methods of deque don't take slices, unlike MutableSequence, hence the type: ignores
def __getitem__(self, __index: SupportsIndex) -> _T: ... # type: ignore[override]
def __setitem__(self, __i: SupportsIndex, __x: _T) -> None: ... # type: ignore[override]
def __delitem__(self, __i: SupportsIndex) -> None: ... # type: ignore[override]
def __contains__(self, __o: object) -> bool: ...
def __reduce__(self: Self) -> tuple[type[Self], tuple[()], None, Iterator[_T]]: ...
def __iadd__(self: Self, __iterable: Iterable[_T]) -> Self: ...
def __add__(self: Self, __other: Self) -> Self: ...
def __mul__(self: Self, __other: int) -> Self: ...
def __imul__(self: Self, __other: int) -> Self: ...
def __reduce__(self) -> tuple[type[Self], tuple[()], None, Iterator[_T]]: ...
def __iadd__(self, __iterable: Iterable[_T]) -> Self: ...
def __add__(self, __other: Self) -> Self: ...
def __mul__(self, __other: int) -> Self: ...
def __imul__(self, __other: int) -> Self: ...
def __lt__(self, __other: deque[_T]) -> bool: ...
def __le__(self, __other: deque[_T]) -> bool: ...
def __gt__(self, __other: deque[_T]) -> bool: ...
@@ -261,7 +261,7 @@ class Counter(dict[_T, int], Generic[_T]):
def __init__(self, __mapping: SupportsKeysAndGetItem[_T, int]) -> None: ...
@overload
def __init__(self, __iterable: Iterable[_T]) -> None: ...
def copy(self: Self) -> Self: ...
def copy(self) -> Self: ...
def elements(self) -> Iterator[_T]: ...
def most_common(self, n: int | None = None) -> list[tuple[_T, int]]: ...
@classmethod
@@ -297,10 +297,10 @@ class Counter(dict[_T, int], Generic[_T]):
def __pos__(self) -> Counter[_T]: ...
def __neg__(self) -> Counter[_T]: ...
# several type: ignores because __iadd__ is supposedly incompatible with __add__, etc.
def __iadd__(self: Self, other: Counter[_T]) -> Self: ... # type: ignore[misc]
def __isub__(self: Self, other: Counter[_T]) -> Self: ...
def __iand__(self: Self, other: Counter[_T]) -> Self: ...
def __ior__(self: Self, other: Counter[_T]) -> Self: ... # type: ignore[override,misc]
def __iadd__(self, other: Counter[_T]) -> Self: ... # type: ignore[misc]
def __isub__(self, other: Counter[_T]) -> Self: ...
def __iand__(self, other: Counter[_T]) -> Self: ...
def __ior__(self, other: Counter[_T]) -> Self: ... # type: ignore[override,misc]
if sys.version_info >= (3, 10):
def total(self) -> int: ...
def __le__(self, other: Counter[Any]) -> bool: ...
@@ -338,7 +338,7 @@ class _odict_values(dict_values[_KT_co, _VT_co], Reversible[_VT_co], Generic[_KT
class OrderedDict(dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]):
def popitem(self, last: bool = True) -> tuple[_KT, _VT]: ...
def move_to_end(self, key: _KT, last: bool = True) -> None: ...
def copy(self: Self) -> Self: ...
def copy(self) -> Self: ...
def __reversed__(self) -> Iterator[_KT]: ...
def keys(self) -> _odict_keys[_KT, _VT]: ...
def items(self) -> _odict_items[_KT, _VT]: ...
@@ -387,15 +387,15 @@ class defaultdict(dict[_KT, _VT], Generic[_KT, _VT]):
**kwargs: _VT,
) -> None: ...
def __missing__(self, __key: _KT) -> _VT: ...
def __copy__(self: Self) -> Self: ...
def copy(self: Self) -> Self: ...
def __copy__(self) -> Self: ...
def copy(self) -> Self: ...
class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
maps: list[MutableMapping[_KT, _VT]]
def __init__(self, *maps: MutableMapping[_KT, _VT]) -> None: ...
def new_child(self: Self, m: MutableMapping[_KT, _VT] | None = None) -> Self: ...
def new_child(self, m: MutableMapping[_KT, _VT] | None = None) -> Self: ...
@property
def parents(self: Self) -> Self: ...
def parents(self) -> Self: ...
def __setitem__(self, key: _KT, value: _VT) -> None: ...
def __delitem__(self, key: _KT) -> None: ...
def __getitem__(self, key: _KT) -> _VT: ...
@@ -413,7 +413,7 @@ class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def pop(self, key: _KT) -> _VT: ...
@overload
def pop(self, key: _KT, default: _VT | _T) -> _VT | _T: ...
def copy(self: Self) -> Self: ...
def copy(self) -> Self: ...
__copy__ = copy
# All arguments to `fromkeys` are passed to `dict.fromkeys` at runtime, so the signature should be kept in line with `dict.fromkeys`.
@classmethod
@@ -427,6 +427,6 @@ class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def __ror__(self, other: Mapping[_T1, _T2]) -> ChainMap[_KT | _T1, _VT | _T2]: ...
# ChainMap.__ior__ should be kept roughly in line with MutableMapping.update()
@overload # type: ignore[misc]
def __ior__(self: Self, other: SupportsKeysAndGetItem[_KT, _VT]) -> Self: ...
def __ior__(self, other: SupportsKeysAndGetItem[_KT, _VT]) -> Self: ...
@overload
def __ior__(self: Self, other: Iterable[tuple[_KT, _VT]]) -> Self: ...
def __ior__(self, other: Iterable[tuple[_KT, _VT]]) -> Self: ...