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

Always use _typeshed.Self, where applicable #6880

Merged
merged 10 commits into from
Jan 10, 2022
2 changes: 1 addition & 1 deletion stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,4 @@ class structseq(Generic[_T_co]):
# The second parameter will accept a dict of any kind without raising an exception,
# but only has any meaning if you supply it a dict where the keys are strings.
# https://github.com/python/typeshed/pull/6560#discussion_r767149830
def __new__(cls: type[_T], sequence: Iterable[_T_co], dict: dict[str, Any] = ...) -> _T: ...
def __new__(cls: type[Self], sequence: Iterable[_T_co], dict: dict[str, Any] = ...) -> Self: ...
16 changes: 8 additions & 8 deletions stdlib/_weakrefset.pyi
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import sys
from _typeshed import Self
from typing import Any, Generic, Iterable, Iterator, MutableSet, TypeVar

if sys.version_info >= (3, 9):
from types import GenericAlias

_S = TypeVar("_S")
_T = TypeVar("_T")
_SelfT = TypeVar("_SelfT", bound=WeakSet[Any])

class WeakSet(MutableSet[_T], Generic[_T]):
def __init__(self, data: Iterable[_T] | None = ...) -> None: ...
def add(self, item: _T) -> None: ...
def clear(self) -> None: ...
def discard(self, item: _T) -> None: ...
def copy(self: _SelfT) -> _SelfT: ...
def copy(self: Self) -> Self: ...
def pop(self) -> _T: ...
def remove(self, item: _T) -> None: ...
def update(self, other: Iterable[_T]) -> None: ...
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 difference(self: _SelfT, other: Iterable[_T]) -> _SelfT: ...
def __sub__(self: _SelfT, other: Iterable[_T]) -> _SelfT: ...
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: _SelfT, other: Iterable[_T]) -> _SelfT: ...
def intersection(self: _SelfT, other: Iterable[_T]) -> _SelfT: ...
def __and__(self: _SelfT, other: Iterable[_T]) -> _SelfT: ...
def __isub__(self: Self, other: Iterable[_T]) -> 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: _SelfT, other: Iterable[_T]) -> _SelfT: ...
def __iand__(self: Self, other: Iterable[_T]) -> Self: ...
def issubset(self, other: Iterable[_T]) -> bool: ...
def __le__(self, other: Iterable[_T]) -> bool: ...
def __lt__(self, other: Iterable[_T]) -> bool: ...
Expand Down
45 changes: 22 additions & 23 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ _T3 = TypeVar("_T3")
_T4 = TypeVar("_T4")
_T5 = TypeVar("_T5")
_TT = TypeVar("_TT", bound="type")
_TBE = TypeVar("_TBE", bound="BaseException")
_R = TypeVar("_R") # Return-type TypeVar
_SupportsNextT = TypeVar("_SupportsNextT", bound=SupportsNext[Any], covariant=True)
_SupportsAnextT = TypeVar("_SupportsAnextT", bound=SupportsAnext[Any], covariant=True)
Expand All @@ -92,7 +91,7 @@ class object:
@__class__.setter
def __class__(self, __type: type[object]) -> None: ... # type: ignore # noqa: F811
def __init__(self) -> None: ...
def __new__(cls: type[_T]) -> _T: ...
def __new__(cls: type[Self]) -> Self: ...
def __setattr__(self, __name: str, __value: Any) -> None: ...
def __eq__(self, __o: object) -> bool: ...
def __ne__(self, __o: object) -> bool: ...
Expand All @@ -117,7 +116,7 @@ class staticmethod(Generic[_R]): # Special, only valid as a decorator.
__func__: Callable[..., _R]
__isabstractmethod__: bool
def __init__(self: staticmethod[_R], __f: Callable[..., _R]) -> None: ...
def __new__(cls: type[_T], *args: Any, **kwargs: Any) -> _T: ...
def __new__(cls: type[Self], *args: Any, **kwargs: Any) -> Self: ...
def __get__(self, __obj: _T, __type: type[_T] | None = ...) -> Callable[..., _R]: ...
if sys.version_info >= (3, 10):
__name__: str
Expand All @@ -129,7 +128,7 @@ class classmethod(Generic[_R]): # Special, only valid as a decorator.
__func__: Callable[..., _R]
__isabstractmethod__: bool
def __init__(self: classmethod[_R], __f: Callable[..., _R]) -> None: ...
def __new__(cls: type[_T], *args: Any, **kwargs: Any) -> _T: ...
def __new__(cls: type[Self], *args: Any, **kwargs: Any) -> Self: ...
def __get__(self, __obj: _T, __type: type[_T] | None = ...) -> Callable[..., _R]: ...
if sys.version_info >= (3, 10):
__name__: str
Expand Down Expand Up @@ -157,7 +156,7 @@ class type:
@overload
def __new__(cls, __o: object) -> type: ...
@overload
def __new__(cls: type[_TT], __name: str, __bases: tuple[type, ...], __namespace: dict[str, Any], **kwds: Any) -> _TT: ...
def __new__(cls: type[Self], __name: str, __bases: tuple[type, ...], __namespace: dict[str, Any], **kwds: Any) -> Self: ...
def __call__(self, *args: Any, **kwds: Any) -> Any: ...
def __subclasses__(self: _TT) -> list[_TT]: ...
# Note: the documentation doesn't specify what the return type is, the standard
Expand All @@ -184,9 +183,9 @@ _NegativeInteger = Literal[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -1

class int:
@overload
def __new__(cls: type[_T], __x: str | bytes | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> _T: ...
def __new__(cls: type[Self], __x: str | bytes | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> Self: ...
@overload
def __new__(cls: type[_T], __x: str | bytes | bytearray, base: SupportsIndex) -> _T: ...
def __new__(cls: type[Self], __x: str | bytes | bytearray, base: SupportsIndex) -> Self: ...
if sys.version_info >= (3, 8):
def as_integer_ratio(self) -> tuple[int, Literal[1]]: ...
@property
Expand Down Expand Up @@ -269,7 +268,7 @@ class int:
def __index__(self) -> int: ...

class float:
def __new__(cls: type[_T], x: SupportsFloat | SupportsIndex | str | bytes | bytearray = ...) -> _T: ...
def __new__(cls: type[Self], x: SupportsFloat | SupportsIndex | str | bytes | bytearray = ...) -> Self: ...
def as_integer_ratio(self) -> tuple[int, int]: ...
def hex(self) -> str: ...
def is_integer(self) -> bool: ...
Expand Down Expand Up @@ -326,9 +325,9 @@ class float:

class complex:
@overload
def __new__(cls: type[_T], real: float = ..., imag: float = ...) -> _T: ...
def __new__(cls: type[Self], real: float = ..., imag: float = ...) -> Self: ...
@overload
def __new__(cls: type[_T], real: str | SupportsComplex | SupportsIndex | complex) -> _T: ...
def __new__(cls: type[Self], real: str | SupportsComplex | SupportsIndex | complex) -> Self: ...
@property
def real(self) -> float: ...
@property
Expand Down Expand Up @@ -359,9 +358,9 @@ class _FormatMapMapping(Protocol):

class str(Sequence[str]):
@overload
def __new__(cls: type[_T], object: object = ...) -> _T: ...
def __new__(cls: type[Self], object: object = ...) -> Self: ...
@overload
def __new__(cls: type[_T], o: bytes, encoding: str = ..., errors: str = ...) -> _T: ...
def __new__(cls: type[Self], o: bytes, encoding: str = ..., errors: str = ...) -> Self: ...
def capitalize(self) -> str: ...
def casefold(self) -> str: ...
def center(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ...
Expand Down Expand Up @@ -443,15 +442,15 @@ class str(Sequence[str]):

class bytes(ByteString):
@overload
def __new__(cls: type[_T], __ints: Iterable[SupportsIndex]) -> _T: ...
def __new__(cls: type[Self], __ints: Iterable[SupportsIndex]) -> Self: ...
@overload
def __new__(cls: type[_T], __string: str, encoding: str, errors: str = ...) -> _T: ...
def __new__(cls: type[Self], __string: str, encoding: str, errors: str = ...) -> Self: ...
@overload
def __new__(cls: type[_T], __length: SupportsIndex) -> _T: ...
def __new__(cls: type[Self], __length: SupportsIndex) -> Self: ...
@overload
def __new__(cls: type[_T]) -> _T: ...
def __new__(cls: type[Self]) -> Self: ...
@overload
def __new__(cls: type[_T], __o: SupportsBytes) -> _T: ...
def __new__(cls: type[Self], __o: SupportsBytes) -> Self: ...
def capitalize(self) -> bytes: ...
def center(self, __width: SupportsIndex, __fillchar: bytes = ...) -> bytes: ...
def count(
Expand Down Expand Up @@ -705,7 +704,7 @@ class memoryview(Sized, Sequence[int]):

@final
class bool(int):
def __new__(cls: type[_T], __o: object = ...) -> _T: ...
def __new__(cls: type[Self], __o: object = ...) -> Self: ...
@overload
def __and__(self, __x: bool) -> bool: ...
@overload
Expand Down Expand Up @@ -748,7 +747,7 @@ class slice:
def indices(self, __len: SupportsIndex) -> tuple[int, int, int]: ...

class tuple(Sequence[_T_co], Generic[_T_co]):
def __new__(cls: type[_T], __iterable: Iterable[_T_co] = ...) -> _T: ...
def __new__(cls: type[Self], __iterable: Iterable[_T_co] = ...) -> Self: ...
def __len__(self) -> int: ...
def __contains__(self, __x: object) -> bool: ...
@overload
Expand Down Expand Up @@ -820,10 +819,10 @@ class list(MutableSequence[_T], Generic[_T]):
def __setitem__(self, __s: slice, __o: Iterable[_T]) -> None: ...
def __delitem__(self, __i: SupportsIndex | slice) -> None: ...
def __add__(self, __x: list[_T]) -> list[_T]: ...
def __iadd__(self: _S, __x: Iterable[_T]) -> _S: ...
def __iadd__(self: Self, __x: Iterable[_T]) -> Self: ...
def __mul__(self, __n: SupportsIndex) -> list[_T]: ...
def __rmul__(self, __n: SupportsIndex) -> list[_T]: ...
def __imul__(self: _S, __n: SupportsIndex) -> _S: ...
def __imul__(self: Self, __n: SupportsIndex) -> Self: ...
def __contains__(self, __o: object) -> bool: ...
def __reversed__(self) -> Iterator[_T]: ...
def __gt__(self, __x: list[_T]) -> bool: ...
Expand All @@ -847,7 +846,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[_T1], *args: Any, **kwargs: Any) -> _T1: ...
def __new__(cls: type[Self], *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]: ...
Expand Down Expand Up @@ -1523,7 +1522,7 @@ class BaseException:
if sys.version_info >= (3, 11):
__note__: str | None
def __init__(self, *args: object) -> None: ...
def with_traceback(self: _TBE, __tb: TracebackType | None) -> _TBE: ...
def with_traceback(self: Self, __tb: TracebackType | None) -> Self: ...

class GeneratorExit(BaseException): ...
class KeyboardInterrupt(BaseException): ...
Expand Down
8 changes: 3 additions & 5 deletions stdlib/cgi.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import sys
from _typeshed import SupportsGetItem, SupportsItemAccess
from _typeshed import Self, SupportsGetItem, SupportsItemAccess
from builtins import type as _type
from collections.abc import Iterable, Iterator, Mapping
from typing import IO, Any, Protocol, TypeVar

_T = TypeVar("_T", bound=FieldStorage)
from typing import IO, Any, Protocol

def parse(
fp: IO[Any] | None = ...,
Expand Down Expand Up @@ -93,7 +91,7 @@ class FieldStorage:
max_num_fields: int | None = ...,
separator: str = ...,
) -> None: ...
def __enter__(self: _T) -> _T: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, *args: Any) -> None: ...
def __iter__(self) -> Iterator[str]: ...
def __getitem__(self, key: str) -> Any: ...
Expand Down
10 changes: 3 additions & 7 deletions stdlib/codecs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sys
import types
from _typeshed import Self
from abc import abstractmethod
from typing import IO, Any, BinaryIO, Callable, Generator, Iterable, Iterator, Protocol, TextIO, TypeVar, overload
from typing import IO, Any, BinaryIO, Callable, Generator, Iterable, Iterator, Protocol, TextIO, overload
from typing_extensions import Literal

BOM32_BE: Literal[b"\xfe\xff"]
Expand Down Expand Up @@ -201,8 +201,6 @@ class StreamReader(Codec):
def __iter__(self) -> Iterator[str]: ...
def __getattr__(self, name: str, getattr: Callable[[str], Any] = ...) -> Any: ...

_T = TypeVar("_T", bound=StreamReaderWriter)

# Doesn't actually inherit from TextIO, but wraps a BinaryIO to provide text reading and writing
# and delegates attributes to the underlying binary stream with __getattr__.
class StreamReaderWriter(TextIO):
Expand All @@ -211,7 +209,7 @@ class StreamReaderWriter(TextIO):
def readline(self, size: int | None = ...) -> str: ...
def readlines(self, sizehint: int | None = ...) -> list[str]: ...
def __next__(self) -> str: ...
def __iter__(self: _T) -> _T: ...
def __iter__(self: Self) -> Self: ...
# This actually returns None, but that's incompatible with the supertype
def write(self, data: str) -> int: ...
def writelines(self, list: Iterable[str]) -> None: ...
Expand All @@ -233,8 +231,6 @@ class StreamReaderWriter(TextIO):
def tell(self) -> int: ...
def writable(self) -> bool: ...

_SRT = TypeVar("_SRT", bound=StreamRecoder)

class StreamRecoder(BinaryIO):
def __init__(
self,
Expand All @@ -249,7 +245,7 @@ class StreamRecoder(BinaryIO):
def readline(self, size: int | None = ...) -> bytes: ...
def readlines(self, sizehint: int | None = ...) -> list[bytes]: ...
def __next__(self) -> bytes: ...
def __iter__(self: _SRT) -> _SRT: ...
def __iter__(self: Self) -> Self: ...
def write(self, data: bytes) -> int: ...
def writelines(self, list: Iterable[bytes]) -> int: ... # type: ignore # it's supposed to return None
def reset(self) -> None: ...
Expand Down
Loading