Skip to content

Commit 96c9abb

Browse files
authored
Always use _typeshed.Self, where applicable (#6880)
* Always use `_typeshed.Self`, where applicable * Revert changes to `google-cloud-ndb` (ambiguous) * Remove empty line added by script * Revert changes to `stubs/python-dateutil/dateutil/relativedelta.pyi` * Manually add a few more that the script missed * Improve `filelock` annotation Source code here: https://github.com/tox-dev/py-filelock/blob/79ec7b2826e33b982fe83b057f359448b9d966ba/src/filelock/_api.py#L207 * Improve `opentracing/scope` annotation Source code here: https://github.com/opentracing/opentracing-python/blob/3e1d357a348269ef54d67f761302fab93dbfc0f7/opentracing/scope.py#L71 * Improve `redis/client` stub Source code here: https://github.com/redis/redis-py/blob/15f315a496c3267c8cbcc6d6d9c6005ea4d4a4d5/redis/client.py#L1217 * Improve `redis/lock` annotation Source code here: https://github.com/redis/redis-py/blob/15f315a496c3267c8cbcc6d6d9c6005ea4d4a4d5/redis/lock.py#L155 * Improve `requests/models` annotation Source code here: https://github.com/psf/requests/blob/d718e753834b84018014a23d663369ac27d1ab9c/requests/models.py#L653
1 parent 3351f0c commit 96c9abb

File tree

48 files changed

+295
-321
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+295
-321
lines changed

stdlib/_typeshed/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,4 @@ class structseq(Generic[_T_co]):
215215
# The second parameter will accept a dict of any kind without raising an exception,
216216
# but only has any meaning if you supply it a dict where the keys are strings.
217217
# https://github.com/python/typeshed/pull/6560#discussion_r767149830
218-
def __new__(cls: type[_T], sequence: Iterable[_T_co], dict: dict[str, Any] = ...) -> _T: ...
218+
def __new__(cls: type[Self], sequence: Iterable[_T_co], dict: dict[str, Any] = ...) -> Self: ...

stdlib/_weakrefset.pyi

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
import sys
2+
from _typeshed import Self
23
from typing import Any, Generic, Iterable, Iterator, MutableSet, TypeVar
34

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

78
_S = TypeVar("_S")
89
_T = TypeVar("_T")
9-
_SelfT = TypeVar("_SelfT", bound=WeakSet[Any])
1010

1111
class WeakSet(MutableSet[_T], Generic[_T]):
1212
def __init__(self, data: Iterable[_T] | None = ...) -> None: ...
1313
def add(self, item: _T) -> None: ...
1414
def clear(self) -> None: ...
1515
def discard(self, item: _T) -> None: ...
16-
def copy(self: _SelfT) -> _SelfT: ...
16+
def copy(self: Self) -> Self: ...
1717
def pop(self) -> _T: ...
1818
def remove(self, item: _T) -> None: ...
1919
def update(self, other: Iterable[_T]) -> None: ...
2020
def __contains__(self, item: object) -> bool: ...
2121
def __len__(self) -> int: ...
2222
def __iter__(self) -> Iterator[_T]: ...
2323
def __ior__(self, other: Iterable[_S]) -> WeakSet[_S | _T]: ...
24-
def difference(self: _SelfT, other: Iterable[_T]) -> _SelfT: ...
25-
def __sub__(self: _SelfT, other: Iterable[_T]) -> _SelfT: ...
24+
def difference(self: Self, other: Iterable[_T]) -> Self: ...
25+
def __sub__(self: Self, other: Iterable[_T]) -> Self: ...
2626
def difference_update(self, other: Iterable[_T]) -> None: ...
27-
def __isub__(self: _SelfT, other: Iterable[_T]) -> _SelfT: ...
28-
def intersection(self: _SelfT, other: Iterable[_T]) -> _SelfT: ...
29-
def __and__(self: _SelfT, other: Iterable[_T]) -> _SelfT: ...
27+
def __isub__(self: Self, other: Iterable[_T]) -> Self: ...
28+
def intersection(self: Self, other: Iterable[_T]) -> Self: ...
29+
def __and__(self: Self, other: Iterable[_T]) -> Self: ...
3030
def intersection_update(self, other: Iterable[_T]) -> None: ...
31-
def __iand__(self: _SelfT, other: Iterable[_T]) -> _SelfT: ...
31+
def __iand__(self: Self, other: Iterable[_T]) -> Self: ...
3232
def issubset(self, other: Iterable[_T]) -> bool: ...
3333
def __le__(self, other: Iterable[_T]) -> bool: ...
3434
def __lt__(self, other: Iterable[_T]) -> bool: ...

stdlib/builtins.pyi

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ _T2 = TypeVar("_T2")
6969
_T3 = TypeVar("_T3")
7070
_T4 = TypeVar("_T4")
7171
_T5 = TypeVar("_T5")
72-
_TT = TypeVar("_TT", bound="type")
73-
_TBE = TypeVar("_TBE", bound="BaseException")
7472
_R = TypeVar("_R") # Return-type TypeVar
7573
_SupportsNextT = TypeVar("_SupportsNextT", bound=SupportsNext[Any], covariant=True)
7674
_SupportsAnextT = TypeVar("_SupportsAnextT", bound=SupportsAnext[Any], covariant=True)
@@ -87,12 +85,12 @@ class object:
8785
__module__: str
8886
__annotations__: dict[str, Any]
8987
@property
90-
def __class__(self: _T) -> type[_T]: ...
88+
def __class__(self: Self) -> type[Self]: ...
9189
# Ignore errors about type mismatch between property getter and setter
9290
@__class__.setter
9391
def __class__(self, __type: type[object]) -> None: ... # type: ignore # noqa: F811
9492
def __init__(self) -> None: ...
95-
def __new__(cls: type[_T]) -> _T: ...
93+
def __new__(cls: type[Self]) -> Self: ...
9694
def __setattr__(self, __name: str, __value: Any) -> None: ...
9795
def __eq__(self, __o: object) -> bool: ...
9896
def __ne__(self, __o: object) -> bool: ...
@@ -117,7 +115,7 @@ class staticmethod(Generic[_R]): # Special, only valid as a decorator.
117115
__func__: Callable[..., _R]
118116
__isabstractmethod__: bool
119117
def __init__(self: staticmethod[_R], __f: Callable[..., _R]) -> None: ...
120-
def __new__(cls: type[_T], *args: Any, **kwargs: Any) -> _T: ...
118+
def __new__(cls: type[Self], *args: Any, **kwargs: Any) -> Self: ...
121119
def __get__(self, __obj: _T, __type: type[_T] | None = ...) -> Callable[..., _R]: ...
122120
if sys.version_info >= (3, 10):
123121
__name__: str
@@ -129,7 +127,7 @@ class classmethod(Generic[_R]): # Special, only valid as a decorator.
129127
__func__: Callable[..., _R]
130128
__isabstractmethod__: bool
131129
def __init__(self: classmethod[_R], __f: Callable[..., _R]) -> None: ...
132-
def __new__(cls: type[_T], *args: Any, **kwargs: Any) -> _T: ...
130+
def __new__(cls: type[Self], *args: Any, **kwargs: Any) -> Self: ...
133131
def __get__(self, __obj: _T, __type: type[_T] | None = ...) -> Callable[..., _R]: ...
134132
if sys.version_info >= (3, 10):
135133
__name__: str
@@ -157,9 +155,9 @@ class type:
157155
@overload
158156
def __new__(cls, __o: object) -> type: ...
159157
@overload
160-
def __new__(cls: type[_TT], __name: str, __bases: tuple[type, ...], __namespace: dict[str, Any], **kwds: Any) -> _TT: ...
158+
def __new__(cls: type[Self], __name: str, __bases: tuple[type, ...], __namespace: dict[str, Any], **kwds: Any) -> Self: ...
161159
def __call__(self, *args: Any, **kwds: Any) -> Any: ...
162-
def __subclasses__(self: _TT) -> list[_TT]: ...
160+
def __subclasses__(self: Self) -> list[Self]: ...
163161
# Note: the documentation doesn't specify what the return type is, the standard
164162
# implementation seems to be returning a list.
165163
def mro(self) -> list[type]: ...
@@ -184,9 +182,9 @@ _NegativeInteger = Literal[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -1
184182

185183
class int:
186184
@overload
187-
def __new__(cls: type[_T], __x: str | bytes | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> _T: ...
185+
def __new__(cls: type[Self], __x: str | bytes | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> Self: ...
188186
@overload
189-
def __new__(cls: type[_T], __x: str | bytes | bytearray, base: SupportsIndex) -> _T: ...
187+
def __new__(cls: type[Self], __x: str | bytes | bytearray, base: SupportsIndex) -> Self: ...
190188
if sys.version_info >= (3, 8):
191189
def as_integer_ratio(self) -> tuple[int, Literal[1]]: ...
192190
@property
@@ -269,7 +267,7 @@ class int:
269267
def __index__(self) -> int: ...
270268

271269
class float:
272-
def __new__(cls: type[_T], x: SupportsFloat | SupportsIndex | str | bytes | bytearray = ...) -> _T: ...
270+
def __new__(cls: type[Self], x: SupportsFloat | SupportsIndex | str | bytes | bytearray = ...) -> Self: ...
273271
def as_integer_ratio(self) -> tuple[int, int]: ...
274272
def hex(self) -> str: ...
275273
def is_integer(self) -> bool: ...
@@ -326,9 +324,9 @@ class float:
326324

327325
class complex:
328326
@overload
329-
def __new__(cls: type[_T], real: float = ..., imag: float = ...) -> _T: ...
327+
def __new__(cls: type[Self], real: float = ..., imag: float = ...) -> Self: ...
330328
@overload
331-
def __new__(cls: type[_T], real: str | SupportsComplex | SupportsIndex | complex) -> _T: ...
329+
def __new__(cls: type[Self], real: str | SupportsComplex | SupportsIndex | complex) -> Self: ...
332330
@property
333331
def real(self) -> float: ...
334332
@property
@@ -359,9 +357,9 @@ class _FormatMapMapping(Protocol):
359357

360358
class str(Sequence[str]):
361359
@overload
362-
def __new__(cls: type[_T], object: object = ...) -> _T: ...
360+
def __new__(cls: type[Self], object: object = ...) -> Self: ...
363361
@overload
364-
def __new__(cls: type[_T], o: bytes, encoding: str = ..., errors: str = ...) -> _T: ...
362+
def __new__(cls: type[Self], o: bytes, encoding: str = ..., errors: str = ...) -> Self: ...
365363
def capitalize(self) -> str: ...
366364
def casefold(self) -> str: ...
367365
def center(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ...
@@ -443,15 +441,15 @@ class str(Sequence[str]):
443441

444442
class bytes(ByteString):
445443
@overload
446-
def __new__(cls: type[_T], __ints: Iterable[SupportsIndex]) -> _T: ...
444+
def __new__(cls: type[Self], __ints: Iterable[SupportsIndex]) -> Self: ...
447445
@overload
448-
def __new__(cls: type[_T], __string: str, encoding: str, errors: str = ...) -> _T: ...
446+
def __new__(cls: type[Self], __string: str, encoding: str, errors: str = ...) -> Self: ...
449447
@overload
450-
def __new__(cls: type[_T], __length: SupportsIndex) -> _T: ...
448+
def __new__(cls: type[Self], __length: SupportsIndex) -> Self: ...
451449
@overload
452-
def __new__(cls: type[_T]) -> _T: ...
450+
def __new__(cls: type[Self]) -> Self: ...
453451
@overload
454-
def __new__(cls: type[_T], __o: SupportsBytes) -> _T: ...
452+
def __new__(cls: type[Self], __o: SupportsBytes) -> Self: ...
455453
def capitalize(self) -> bytes: ...
456454
def center(self, __width: SupportsIndex, __fillchar: bytes = ...) -> bytes: ...
457455
def count(
@@ -705,7 +703,7 @@ class memoryview(Sized, Sequence[int]):
705703

706704
@final
707705
class bool(int):
708-
def __new__(cls: type[_T], __o: object = ...) -> _T: ...
706+
def __new__(cls: type[Self], __o: object = ...) -> Self: ...
709707
@overload
710708
def __and__(self, __x: bool) -> bool: ...
711709
@overload
@@ -748,7 +746,7 @@ class slice:
748746
def indices(self, __len: SupportsIndex) -> tuple[int, int, int]: ...
749747

750748
class tuple(Sequence[_T_co], Generic[_T_co]):
751-
def __new__(cls: type[_T], __iterable: Iterable[_T_co] = ...) -> _T: ...
749+
def __new__(cls: type[Self], __iterable: Iterable[_T_co] = ...) -> Self: ...
752750
def __len__(self) -> int: ...
753751
def __contains__(self, __x: object) -> bool: ...
754752
@overload
@@ -820,10 +818,10 @@ class list(MutableSequence[_T], Generic[_T]):
820818
def __setitem__(self, __s: slice, __o: Iterable[_T]) -> None: ...
821819
def __delitem__(self, __i: SupportsIndex | slice) -> None: ...
822820
def __add__(self, __x: list[_T]) -> list[_T]: ...
823-
def __iadd__(self: _S, __x: Iterable[_T]) -> _S: ...
821+
def __iadd__(self: Self, __x: Iterable[_T]) -> Self: ...
824822
def __mul__(self, __n: SupportsIndex) -> list[_T]: ...
825823
def __rmul__(self, __n: SupportsIndex) -> list[_T]: ...
826-
def __imul__(self: _S, __n: SupportsIndex) -> _S: ...
824+
def __imul__(self: Self, __n: SupportsIndex) -> Self: ...
827825
def __contains__(self, __o: object) -> bool: ...
828826
def __reversed__(self) -> Iterator[_T]: ...
829827
def __gt__(self, __x: list[_T]) -> bool: ...
@@ -847,7 +845,7 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
847845
# Cannot be Iterable[Sequence[_T]] or otherwise dict(["foo", "bar", "baz"]) is not an error
848846
@overload
849847
def __init__(self: dict[str, str], __iterable: Iterable[list[str]]) -> None: ...
850-
def __new__(cls: type[_T1], *args: Any, **kwargs: Any) -> _T1: ...
848+
def __new__(cls: type[Self], *args: Any, **kwargs: Any) -> Self: ...
851849
def copy(self) -> dict[_KT, _VT]: ...
852850
def keys(self) -> dict_keys[_KT, _VT]: ...
853851
def values(self) -> dict_values[_KT, _VT]: ...
@@ -1523,7 +1521,7 @@ class BaseException:
15231521
if sys.version_info >= (3, 11):
15241522
__note__: str | None
15251523
def __init__(self, *args: object) -> None: ...
1526-
def with_traceback(self: _TBE, __tb: TracebackType | None) -> _TBE: ...
1524+
def with_traceback(self: Self, __tb: TracebackType | None) -> Self: ...
15271525

15281526
class GeneratorExit(BaseException): ...
15291527
class KeyboardInterrupt(BaseException): ...

stdlib/cgi.pyi

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import sys
2-
from _typeshed import SupportsGetItem, SupportsItemAccess
2+
from _typeshed import Self, SupportsGetItem, SupportsItemAccess
33
from builtins import type as _type
44
from collections.abc import Iterable, Iterator, Mapping
5-
from typing import IO, Any, Protocol, TypeVar
6-
7-
_T = TypeVar("_T", bound=FieldStorage)
5+
from typing import IO, Any, Protocol
86

97
def parse(
108
fp: IO[Any] | None = ...,
@@ -93,7 +91,7 @@ class FieldStorage:
9391
max_num_fields: int | None = ...,
9492
separator: str = ...,
9593
) -> None: ...
96-
def __enter__(self: _T) -> _T: ...
94+
def __enter__(self: Self) -> Self: ...
9795
def __exit__(self, *args: Any) -> None: ...
9896
def __iter__(self) -> Iterator[str]: ...
9997
def __getitem__(self, key: str) -> Any: ...

stdlib/codecs.pyi

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import sys
22
import types
33
from _typeshed import Self
44
from abc import abstractmethod
5-
from typing import IO, Any, BinaryIO, Callable, Generator, Iterable, Iterator, Protocol, TextIO, TypeVar, overload
5+
from typing import IO, Any, BinaryIO, Callable, Generator, Iterable, Iterator, Protocol, TextIO, overload
66
from typing_extensions import Literal
77

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

204-
_T = TypeVar("_T", bound=StreamReaderWriter)
205-
206204
# Doesn't actually inherit from TextIO, but wraps a BinaryIO to provide text reading and writing
207205
# and delegates attributes to the underlying binary stream with __getattr__.
208206
class StreamReaderWriter(TextIO):
@@ -211,7 +209,7 @@ class StreamReaderWriter(TextIO):
211209
def readline(self, size: int | None = ...) -> str: ...
212210
def readlines(self, sizehint: int | None = ...) -> list[str]: ...
213211
def __next__(self) -> str: ...
214-
def __iter__(self: _T) -> _T: ...
212+
def __iter__(self: Self) -> Self: ...
215213
# This actually returns None, but that's incompatible with the supertype
216214
def write(self, data: str) -> int: ...
217215
def writelines(self, list: Iterable[str]) -> None: ...
@@ -233,8 +231,6 @@ class StreamReaderWriter(TextIO):
233231
def tell(self) -> int: ...
234232
def writable(self) -> bool: ...
235233

236-
_SRT = TypeVar("_SRT", bound=StreamRecoder)
237-
238234
class StreamRecoder(BinaryIO):
239235
def __init__(
240236
self,
@@ -249,7 +245,7 @@ class StreamRecoder(BinaryIO):
249245
def readline(self, size: int | None = ...) -> bytes: ...
250246
def readlines(self, sizehint: int | None = ...) -> list[bytes]: ...
251247
def __next__(self) -> bytes: ...
252-
def __iter__(self: _SRT) -> _SRT: ...
248+
def __iter__(self: Self) -> Self: ...
253249
def write(self, data: bytes) -> int: ...
254250
def writelines(self, list: Iterable[bytes]) -> int: ... # type: ignore # it's supposed to return None
255251
def reset(self) -> None: ...

0 commit comments

Comments
 (0)