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,4 +1,5 @@
import _ast
import _typeshed
import sys
import types
from _collections_abc import dict_items, dict_keys, dict_values
@@ -11,7 +12,6 @@ from _typeshed import (
OpenBinaryModeWriting,
OpenTextMode,
ReadableBuffer,
Self,
SupportsAdd,
SupportsAiter,
SupportsAnext,
@@ -54,7 +54,7 @@ from typing import ( # noqa: Y022
overload,
type_check_only,
)
from typing_extensions import Literal, LiteralString, SupportsIndex, TypeAlias, TypeGuard, final
from typing_extensions import Literal, LiteralString, Self, SupportsIndex, TypeAlias, TypeGuard, final
if sys.version_info >= (3, 9):
from types import GenericAlias
@@ -82,12 +82,12 @@ class object:
__module__: str
__annotations__: dict[str, Any]
@property
def __class__(self: Self) -> type[Self]: ...
def __class__(self) -> type[Self]: ...
# Ignore errors about type mismatch between property getter and setter
@__class__.setter
def __class__(self, __type: type[object]) -> None: ... # noqa: F811
def __init__(self) -> None: ...
def __new__(cls: type[Self]) -> Self: ...
def __new__(cls) -> Self: ...
# N.B. `object.__setattr__` and `object.__delattr__` are heavily special-cased by type checkers.
# Overriding them in subclasses has different semantics, even if the override has an identical signature.
def __setattr__(self, __name: str, __value: Any) -> None: ...
@@ -168,9 +168,11 @@ class type:
@overload
def __new__(cls, __o: object) -> type: ...
@overload
def __new__(cls: type[Self], __name: str, __bases: tuple[type, ...], __namespace: dict[str, Any], **kwds: Any) -> Self: ...
def __new__(
cls: type[_typeshed.Self], __name: str, __bases: tuple[type, ...], __namespace: dict[str, Any], **kwds: Any
) -> _typeshed.Self: ...
def __call__(self, *args: Any, **kwds: Any) -> Any: ...
def __subclasses__(self: Self) -> list[Self]: ...
def __subclasses__(self: _typeshed.Self) -> list[_typeshed.Self]: ...
# Note: the documentation doesn't specify what the return type is, the standard
# implementation seems to be returning a list.
def mro(self) -> list[type]: ...
@@ -196,9 +198,9 @@ _LiteralInteger = _PositiveInteger | _NegativeInteger | Literal[0] # noqa: Y026
class int:
@overload
def __new__(cls: type[Self], __x: str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> Self: ...
def __new__(cls, __x: str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> Self: ...
@overload
def __new__(cls: type[Self], __x: str | bytes | bytearray, base: SupportsIndex) -> Self: ...
def __new__(cls, __x: str | bytes | bytearray, base: SupportsIndex) -> Self: ...
if sys.version_info >= (3, 8):
def as_integer_ratio(self) -> tuple[int, Literal[1]]: ...
@@ -221,7 +223,7 @@ class int:
) -> bytes: ...
@classmethod
def from_bytes(
cls: type[Self],
cls,
bytes: Iterable[SupportsIndex] | SupportsBytes | ReadableBuffer,
byteorder: Literal["little", "big"] = "big",
*,
@@ -231,7 +233,7 @@ class int:
def to_bytes(self, length: SupportsIndex, byteorder: Literal["little", "big"], *, signed: bool = False) -> bytes: ...
@classmethod
def from_bytes(
cls: type[Self],
cls,
bytes: Iterable[SupportsIndex] | SupportsBytes | ReadableBuffer,
byteorder: Literal["little", "big"],
*,
@@ -298,12 +300,12 @@ class int:
def __index__(self) -> int: ...
class float:
def __new__(cls: type[Self], __x: SupportsFloat | SupportsIndex | str | ReadableBuffer = ...) -> Self: ...
def __new__(cls, __x: SupportsFloat | SupportsIndex | str | ReadableBuffer = ...) -> Self: ...
def as_integer_ratio(self) -> tuple[int, int]: ...
def hex(self) -> str: ...
def is_integer(self) -> bool: ...
@classmethod
def fromhex(cls: type[Self], __s: str) -> Self: ...
def fromhex(cls, __s: str) -> Self: ...
@property
def real(self) -> float: ...
@property
@@ -364,19 +366,17 @@ class complex:
# Python doesn't currently accept SupportsComplex for the second argument
@overload
def __new__(
cls: type[Self],
cls,
real: complex | SupportsComplex | SupportsFloat | SupportsIndex = ...,
imag: complex | SupportsFloat | SupportsIndex = ...,
) -> Self: ...
@overload
def __new__(cls: type[Self], real: str | SupportsComplex | SupportsFloat | SupportsIndex | complex) -> Self: ...
def __new__(cls, real: str | SupportsComplex | SupportsFloat | SupportsIndex | complex) -> Self: ...
else:
@overload
def __new__(
cls: type[Self], real: complex | SupportsComplex | SupportsFloat = ..., imag: complex | SupportsFloat = ...
) -> Self: ...
def __new__(cls, real: complex | SupportsComplex | SupportsFloat = ..., imag: complex | SupportsFloat = ...) -> Self: ...
@overload
def __new__(cls: type[Self], real: str | SupportsComplex | SupportsFloat | complex) -> Self: ...
def __new__(cls, real: str | SupportsComplex | SupportsFloat | complex) -> Self: ...
@property
def real(self) -> float: ...
@@ -410,9 +410,9 @@ class _TranslateTable(Protocol):
class str(Sequence[str]):
@overload
def __new__(cls: type[Self], object: object = ...) -> Self: ...
def __new__(cls, object: object = ...) -> Self: ...
@overload
def __new__(cls: type[Self], object: ReadableBuffer, encoding: str = ..., errors: str = ...) -> Self: ...
def __new__(cls, object: ReadableBuffer, encoding: str = ..., errors: str = ...) -> Self: ...
@overload
def capitalize(self: LiteralString) -> LiteralString: ...
@overload
@@ -589,11 +589,11 @@ class str(Sequence[str]):
class bytes(ByteString):
@overload
def __new__(cls: type[Self], __o: Iterable[SupportsIndex] | SupportsIndex | SupportsBytes | ReadableBuffer) -> Self: ...
def __new__(cls, __o: Iterable[SupportsIndex] | SupportsIndex | SupportsBytes | ReadableBuffer) -> Self: ...
@overload
def __new__(cls: type[Self], __string: str, encoding: str, errors: str = ...) -> Self: ...
def __new__(cls, __string: str, encoding: str, errors: str = ...) -> Self: ...
@overload
def __new__(cls: type[Self]) -> Self: ...
def __new__(cls) -> Self: ...
def capitalize(self) -> bytes: ...
def center(self, __width: SupportsIndex, __fillchar: bytes = b" ") -> bytes: ...
def count(
@@ -665,7 +665,7 @@ class bytes(ByteString):
def upper(self) -> bytes: ...
def zfill(self, __width: SupportsIndex) -> bytes: ...
@classmethod
def fromhex(cls: type[Self], __s: str) -> Self: ...
def fromhex(cls, __s: str) -> Self: ...
@staticmethod
def maketrans(__frm: ReadableBuffer, __to: ReadableBuffer) -> bytes: ...
def __len__(self) -> int: ...
@@ -774,7 +774,7 @@ class bytearray(MutableSequence[int], ByteString):
def upper(self) -> bytearray: ...
def zfill(self, __width: SupportsIndex) -> bytearray: ...
@classmethod
def fromhex(cls: type[Self], __string: str) -> Self: ...
def fromhex(cls, __string: str) -> Self: ...
@staticmethod
def maketrans(__frm: ReadableBuffer, __to: ReadableBuffer) -> bytes: ...
def __len__(self) -> int: ...
@@ -791,10 +791,10 @@ class bytearray(MutableSequence[int], ByteString):
def __delitem__(self, __i: SupportsIndex | slice) -> None: ...
def __add__(self, __s: ReadableBuffer) -> bytearray: ...
# The superclass wants us to accept Iterable[int], but that fails at runtime.
def __iadd__(self: Self, __s: ReadableBuffer) -> Self: ... # type: ignore[override]
def __iadd__(self, __s: ReadableBuffer) -> Self: ... # type: ignore[override]
def __mul__(self, __n: SupportsIndex) -> bytearray: ...
def __rmul__(self, __n: SupportsIndex) -> bytearray: ...
def __imul__(self: Self, __n: SupportsIndex) -> Self: ...
def __imul__(self, __n: SupportsIndex) -> Self: ...
def __mod__(self, __value: Any) -> bytes: ...
# Incompatible with Sequence.__contains__
def __contains__(self, __o: SupportsIndex | ReadableBuffer) -> bool: ... # type: ignore[override]
@@ -833,7 +833,7 @@ class memoryview(Sequence[int]):
@property
def nbytes(self) -> int: ...
def __init__(self, obj: ReadableBuffer) -> None: ...
def __enter__(self: Self) -> Self: ...
def __enter__(self) -> Self: ...
def __exit__(
self, __exc_type: type[BaseException] | None, __exc_val: BaseException | None, __exc_tb: TracebackType | None
) -> None: ...
@@ -868,7 +868,7 @@ class memoryview(Sequence[int]):
@final
class bool(int):
def __new__(cls: type[Self], __o: object = ...) -> Self: ...
def __new__(cls, __o: object = ...) -> Self: ...
# The following overloads could be represented more elegantly with a TypeVar("_B", bool, int),
# however mypy has a bug regarding TypeVar constraints (https://github.com/python/mypy/issues/11880).
@overload
@@ -913,7 +913,7 @@ class slice:
def indices(self, __len: SupportsIndex) -> tuple[int, int, int]: ...
class tuple(Sequence[_T_co], Generic[_T_co]):
def __new__(cls: type[Self], __iterable: Iterable[_T_co] = ...) -> Self: ...
def __new__(cls, __iterable: Iterable[_T_co] = ...) -> Self: ...
def __len__(self) -> int: ...
def __contains__(self, __x: object) -> bool: ...
@overload
@@ -1001,10 +1001,10 @@ class list(MutableSequence[_T], Generic[_T]):
def __add__(self, __x: list[_T]) -> list[_T]: ...
@overload
def __add__(self, __x: list[_S]) -> list[_S | _T]: ...
def __iadd__(self: Self, __x: Iterable[_T]) -> Self: ... # type: ignore[misc]
def __iadd__(self, __x: Iterable[_T]) -> Self: ... # type: ignore[misc]
def __mul__(self, __n: SupportsIndex) -> list[_T]: ...
def __rmul__(self, __n: SupportsIndex) -> list[_T]: ...
def __imul__(self: Self, __n: SupportsIndex) -> Self: ...
def __imul__(self, __n: SupportsIndex) -> Self: ...
def __contains__(self, __o: object) -> bool: ...
def __reversed__(self) -> Iterator[_T]: ...
def __gt__(self, __x: list[_T]) -> bool: ...
@@ -1033,7 +1033,7 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
# Cannot be Iterable[Sequence[_T]] or otherwise dict(["foo", "bar", "baz"]) is not an error
@overload
def __init__(self: dict[str, str], __iterable: Iterable[list[str]]) -> None: ...
def __new__(cls: type[Self], *args: Any, **kwargs: Any) -> Self: ...
def __new__(cls, *args: Any, **kwargs: Any) -> Self: ...
def copy(self) -> dict[_KT, _VT]: ...
def keys(self) -> dict_keys[_KT, _VT]: ...
def values(self) -> dict_values[_KT, _VT]: ...
@@ -1070,9 +1070,9 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def __ror__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT | _T2]: ...
# dict.__ior__ should be kept roughly in line with MutableMapping.update()
@overload # type: ignore[misc]
def __ior__(self: Self, __value: SupportsKeysAndGetItem[_KT, _VT]) -> Self: ...
def __ior__(self, __value: SupportsKeysAndGetItem[_KT, _VT]) -> Self: ...
@overload
def __ior__(self: Self, __value: Iterable[tuple[_KT, _VT]]) -> Self: ...
def __ior__(self, __value: Iterable[tuple[_KT, _VT]]) -> Self: ...
class set(MutableSet[_T], Generic[_T]):
@overload
@@ -1098,13 +1098,13 @@ 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: Self, __s: AbstractSet[object]) -> Self: ...
def __iand__(self, __s: AbstractSet[object]) -> Self: ...
def __or__(self, __s: AbstractSet[_S]) -> set[_T | _S]: ...
def __ior__(self: Self, __s: AbstractSet[_T]) -> Self: ... # type: ignore[override,misc]
def __ior__(self, __s: AbstractSet[_T]) -> Self: ... # type: ignore[override,misc]
def __sub__(self, __s: AbstractSet[_T | None]) -> set[_T]: ...
def __isub__(self: Self, __s: AbstractSet[object]) -> Self: ...
def __isub__(self, __s: AbstractSet[object]) -> Self: ...
def __xor__(self, __s: AbstractSet[_S]) -> set[_T | _S]: ...
def __ixor__(self: Self, __s: AbstractSet[_T]) -> Self: ... # type: ignore[override,misc]
def __ixor__(self, __s: AbstractSet[_T]) -> Self: ... # type: ignore[override,misc]
def __le__(self, __s: AbstractSet[object]) -> bool: ...
def __lt__(self, __s: AbstractSet[object]) -> bool: ...
def __ge__(self, __s: AbstractSet[object]) -> bool: ...
@@ -1115,9 +1115,9 @@ class set(MutableSet[_T], Generic[_T]):
class frozenset(AbstractSet[_T_co], Generic[_T_co]):
@overload
def __new__(cls: type[Self]) -> Self: ...
def __new__(cls) -> Self: ...
@overload
def __new__(cls: type[Self], __iterable: Iterable[_T_co]) -> Self: ...
def __new__(cls, __iterable: Iterable[_T_co]) -> Self: ...
def copy(self) -> frozenset[_T_co]: ...
def difference(self, *s: Iterable[object]) -> frozenset[_T_co]: ...
def intersection(self, *s: Iterable[object]) -> frozenset[_T_co]: ...
@@ -1142,7 +1142,7 @@ class frozenset(AbstractSet[_T_co], Generic[_T_co]):
class enumerate(Iterator[tuple[int, _T]], Generic[_T]):
def __init__(self, iterable: Iterable[_T], start: int = ...) -> None: ...
def __iter__(self: Self) -> Self: ...
def __iter__(self) -> Self: ...
def __next__(self) -> tuple[int, _T]: ...
if sys.version_info >= (3, 9):
def __class_getitem__(cls, __item: Any) -> GenericAlias: ...
@@ -1354,7 +1354,7 @@ class filter(Iterator[_T], Generic[_T]):
def __init__(self, __function: Callable[[_S], TypeGuard[_T]], __iterable: Iterable[_S]) -> None: ...
@overload
def __init__(self, __function: Callable[[_T], Any], __iterable: Iterable[_T]) -> None: ...
def __iter__(self: Self) -> Self: ...
def __iter__(self) -> Self: ...
def __next__(self) -> _T: ...
def format(__value: object, __format_spec: str = "") -> str: ...
@@ -1445,7 +1445,7 @@ class map(Iterator[_S], Generic[_S]):
__iter6: Iterable[Any],
*iterables: Iterable[Any],
) -> None: ...
def __iter__(self: Self) -> Self: ...
def __iter__(self) -> Self: ...
def __next__(self) -> _S: ...
@overload
@@ -1682,7 +1682,7 @@ class reversed(Iterator[_T], Generic[_T]):
def __init__(self, __sequence: Reversible[_T]) -> None: ...
@overload
def __init__(self, __sequence: SupportsLenAndGetItem[_T]) -> None: ...
def __iter__(self: Self) -> Self: ...
def __iter__(self) -> Self: ...
def __next__(self) -> _T: ...
def __length_hint__(self) -> int: ...
@@ -1826,7 +1826,7 @@ class zip(Iterator[_T_co], Generic[_T_co]):
*iterables: Iterable[Any],
) -> zip[tuple[Any, ...]]: ...
def __iter__(self: Self) -> Self: ...
def __iter__(self) -> Self: ...
def __next__(self) -> _T_co: ...
# Signature of `builtins.__import__` should be kept identical to `importlib.__import__`
@@ -1856,7 +1856,7 @@ class BaseException:
__traceback__: TracebackType | None
def __init__(self, *args: object) -> None: ...
def __setstate__(self, __state: dict[str, Any] | None) -> None: ...
def with_traceback(self: Self, __tb: TracebackType | None) -> Self: ...
def with_traceback(self, __tb: TracebackType | None) -> Self: ...
if sys.version_info >= (3, 11):
# only present after add_note() is called
__notes__: list[str]
@@ -2009,7 +2009,7 @@ if sys.version_info >= (3, 11):
# See `check_exception_group.py` for use-cases and comments.
class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]):
def __new__(cls: type[Self], __message: str, __exceptions: Sequence[_BaseExceptionT_co]) -> Self: ...
def __new__(cls, __message: str, __exceptions: Sequence[_BaseExceptionT_co]) -> Self: ...
def __init__(self, __message: str, __exceptions: Sequence[_BaseExceptionT_co]) -> None: ...
@property
def message(self) -> str: ...
@@ -2025,7 +2025,7 @@ if sys.version_info >= (3, 11):
) -> BaseExceptionGroup[_BaseExceptionT] | None: ...
@overload
def subgroup(
self: Self, __condition: Callable[[_BaseExceptionT_co | Self], bool]
self, __condition: Callable[[_BaseExceptionT_co | Self], bool]
) -> BaseExceptionGroup[_BaseExceptionT_co] | None: ...
@overload
def split(
@@ -2037,7 +2037,7 @@ if sys.version_info >= (3, 11):
) -> tuple[BaseExceptionGroup[_BaseExceptionT] | None, BaseExceptionGroup[_BaseExceptionT_co] | None]: ...
@overload
def split(
self: Self, __condition: Callable[[_BaseExceptionT_co | Self], bool]
self, __condition: Callable[[_BaseExceptionT_co | Self], bool]
) -> tuple[BaseExceptionGroup[_BaseExceptionT_co] | None, BaseExceptionGroup[_BaseExceptionT_co] | None]: ...
# In reality it is `NonEmptySequence`:
@overload
@@ -2047,7 +2047,7 @@ if sys.version_info >= (3, 11):
def __class_getitem__(cls, __item: Any) -> GenericAlias: ...
class ExceptionGroup(BaseExceptionGroup[_ExceptionT_co], Exception):
def __new__(cls: type[Self], __message: str, __exceptions: Sequence[_ExceptionT_co]) -> Self: ...
def __new__(cls, __message: str, __exceptions: Sequence[_ExceptionT_co]) -> Self: ...
def __init__(self, __message: str, __exceptions: Sequence[_ExceptionT_co]) -> None: ...
@property
def exceptions(self) -> tuple[_ExceptionT_co | ExceptionGroup[_ExceptionT_co], ...]: ...
@@ -2057,14 +2057,12 @@ if sys.version_info >= (3, 11):
self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...]
) -> ExceptionGroup[_ExceptionT] | None: ...
@overload
def subgroup(
self: Self, __condition: Callable[[_ExceptionT_co | Self], bool]
) -> ExceptionGroup[_ExceptionT_co] | None: ...
def subgroup(self, __condition: Callable[[_ExceptionT_co | Self], bool]) -> ExceptionGroup[_ExceptionT_co] | None: ...
@overload # type: ignore[override]
def split(
self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...]
) -> tuple[ExceptionGroup[_ExceptionT] | None, ExceptionGroup[_ExceptionT_co] | None]: ...
@overload
def split(
self: Self, __condition: Callable[[_ExceptionT_co | Self], bool]
self, __condition: Callable[[_ExceptionT_co | Self], bool]
) -> tuple[ExceptionGroup[_ExceptionT_co] | None, ExceptionGroup[_ExceptionT_co] | None]: ...