Skip to content

Commit 9ed39d8

Browse files
authored
Use typing_extensions.Self in the stdlib (#9694)
1 parent 10086c0 commit 9ed39d8

Some content is hidden

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

98 files changed

+627
-654
lines changed

stdlib/_decimal.pyi

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import numbers
22
import sys
3-
from _typeshed import Self
43
from collections.abc import Container, Sequence
54
from types import TracebackType
65
from typing import Any, ClassVar, NamedTuple, overload
7-
from typing_extensions import Literal, TypeAlias
6+
from typing_extensions import Literal, Self, TypeAlias
87

98
_Decimal: TypeAlias = Decimal | int
109
_DecimalNew: TypeAlias = Decimal | float | str | tuple[int, Sequence[int], int]
@@ -69,9 +68,9 @@ else:
6968
def localcontext(ctx: Context | None = None) -> _ContextManager: ...
7069

7170
class Decimal:
72-
def __new__(cls: type[Self], value: _DecimalNew = ..., context: Context | None = ...) -> Self: ...
71+
def __new__(cls, value: _DecimalNew = ..., context: Context | None = ...) -> Self: ...
7372
@classmethod
74-
def from_float(cls: type[Self], __f: float) -> Self: ...
73+
def from_float(cls, __f: float) -> Self: ...
7574
def __bool__(self) -> bool: ...
7675
def compare(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
7776
def as_tuple(self) -> DecimalTuple: ...
@@ -163,9 +162,9 @@ class Decimal:
163162
def rotate(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
164163
def scaleb(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
165164
def shift(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
166-
def __reduce__(self: Self) -> tuple[type[Self], tuple[str]]: ...
167-
def __copy__(self: Self) -> Self: ...
168-
def __deepcopy__(self: Self, __memo: Any) -> Self: ...
165+
def __reduce__(self) -> tuple[type[Self], tuple[str]]: ...
166+
def __copy__(self) -> Self: ...
167+
def __deepcopy__(self, __memo: Any) -> Self: ...
169168
def __format__(self, __specifier: str, __context: Context | None = ...) -> str: ...
170169

171170
class _ContextManager:
@@ -203,7 +202,7 @@ class Context:
203202
traps: None | dict[_TrapType, bool] | Container[_TrapType] = ...,
204203
_ignored_flags: list[_TrapType] | None = ...,
205204
) -> None: ...
206-
def __reduce__(self: Self) -> tuple[type[Self], tuple[Any, ...]]: ...
205+
def __reduce__(self) -> tuple[type[Self], tuple[Any, ...]]: ...
207206
def clear_flags(self) -> None: ...
208207
def clear_traps(self) -> None: ...
209208
def copy(self) -> Context: ...

stdlib/_py_abc.pyi

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from _typeshed import Self
1+
import _typeshed
22
from typing import Any, NewType, TypeVar
33

44
_T = TypeVar("_T")
@@ -8,5 +8,7 @@ _CacheToken = NewType("_CacheToken", int)
88
def get_cache_token() -> _CacheToken: ...
99

1010
class ABCMeta(type):
11-
def __new__(__mcls: type[Self], __name: str, __bases: tuple[type[Any], ...], __namespace: dict[str, Any]) -> Self: ...
11+
def __new__(
12+
__mcls: type[_typeshed.Self], __name: str, __bases: tuple[type[Any], ...], __namespace: dict[str, Any]
13+
) -> _typeshed.Self: ...
1214
def register(cls, subclass: type[_T]) -> type[_T]: ...

stdlib/_weakref.pyi

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import sys
2-
from _typeshed import Self
32
from collections.abc import Callable
43
from typing import Any, Generic, TypeVar, overload
5-
from typing_extensions import final
4+
from typing_extensions import Self, final
65

76
if sys.version_info >= (3, 9):
87
from types import GenericAlias
@@ -21,7 +20,7 @@ class ProxyType(Generic[_T]): # "weakproxy"
2120

2221
class ReferenceType(Generic[_T]):
2322
__callback__: Callable[[ReferenceType[_T]], Any]
24-
def __new__(cls: type[Self], o: _T, callback: Callable[[ReferenceType[_T]], Any] | None = ...) -> Self: ...
23+
def __new__(cls, o: _T, callback: Callable[[ReferenceType[_T]], Any] | None = ...) -> Self: ...
2524
def __call__(self) -> _T | None: ...
2625
if sys.version_info >= (3, 9):
2726
def __class_getitem__(cls, item: Any) -> GenericAlias: ...

stdlib/_weakrefset.pyi

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
2-
from _typeshed import Self
32
from collections.abc import Iterable, Iterator, MutableSet
43
from typing import Any, Generic, TypeVar, overload
4+
from typing_extensions import Self
55

66
if sys.version_info >= (3, 9):
77
from types import GenericAlias
@@ -18,21 +18,21 @@ class WeakSet(MutableSet[_T], Generic[_T]):
1818
def __init__(self, data: Iterable[_T]) -> None: ...
1919
def add(self, item: _T) -> None: ...
2020
def discard(self, item: _T) -> None: ...
21-
def copy(self: Self) -> Self: ...
21+
def copy(self) -> Self: ...
2222
def remove(self, item: _T) -> None: ...
2323
def update(self, other: Iterable[_T]) -> None: ...
2424
def __contains__(self, item: object) -> bool: ...
2525
def __len__(self) -> int: ...
2626
def __iter__(self) -> Iterator[_T]: ...
27-
def __ior__(self: Self, other: Iterable[_T]) -> Self: ... # type: ignore[override,misc]
28-
def difference(self: Self, other: Iterable[_T]) -> Self: ...
29-
def __sub__(self: Self, other: Iterable[Any]) -> Self: ...
27+
def __ior__(self, other: Iterable[_T]) -> Self: ... # type: ignore[override,misc]
28+
def difference(self, other: Iterable[_T]) -> Self: ...
29+
def __sub__(self, other: Iterable[Any]) -> Self: ...
3030
def difference_update(self, other: Iterable[Any]) -> None: ...
31-
def __isub__(self: Self, other: Iterable[Any]) -> Self: ...
32-
def intersection(self: Self, other: Iterable[_T]) -> Self: ...
33-
def __and__(self: Self, other: Iterable[Any]) -> Self: ...
31+
def __isub__(self, other: Iterable[Any]) -> Self: ...
32+
def intersection(self, other: Iterable[_T]) -> Self: ...
33+
def __and__(self, other: Iterable[Any]) -> Self: ...
3434
def intersection_update(self, other: Iterable[Any]) -> None: ...
35-
def __iand__(self: Self, other: Iterable[Any]) -> Self: ...
35+
def __iand__(self, other: Iterable[Any]) -> Self: ...
3636
def issubset(self, other: Iterable[_T]) -> bool: ...
3737
def __le__(self, other: Iterable[_T]) -> bool: ...
3838
def __lt__(self, other: Iterable[_T]) -> bool: ...
@@ -43,7 +43,7 @@ class WeakSet(MutableSet[_T], Generic[_T]):
4343
def symmetric_difference(self, other: Iterable[_S]) -> WeakSet[_S | _T]: ...
4444
def __xor__(self, other: Iterable[_S]) -> WeakSet[_S | _T]: ...
4545
def symmetric_difference_update(self, other: Iterable[_T]) -> None: ...
46-
def __ixor__(self: Self, other: Iterable[_T]) -> Self: ... # type: ignore[override,misc]
46+
def __ixor__(self, other: Iterable[_T]) -> Self: ... # type: ignore[override,misc]
4747
def union(self, other: Iterable[_S]) -> WeakSet[_S | _T]: ...
4848
def __or__(self, other: Iterable[_S]) -> WeakSet[_S | _T]: ...
4949
def isdisjoint(self, other: Iterable[_T]) -> bool: ...

stdlib/abc.pyi

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import _typeshed
12
import sys
2-
from _typeshed import Self, SupportsWrite
3+
from _typeshed import SupportsWrite
34
from collections.abc import Callable
45
from typing import Any, Generic, TypeVar
56
from typing_extensions import Literal
@@ -13,10 +14,12 @@ class ABCMeta(type):
1314
__abstractmethods__: frozenset[str]
1415
if sys.version_info >= (3, 11):
1516
def __new__(
16-
__mcls: type[Self], __name: str, __bases: tuple[type, ...], __namespace: dict[str, Any], **kwargs: Any
17-
) -> Self: ...
17+
__mcls: type[_typeshed.Self], __name: str, __bases: tuple[type, ...], __namespace: dict[str, Any], **kwargs: Any
18+
) -> _typeshed.Self: ...
1819
else:
19-
def __new__(mcls: type[Self], name: str, bases: tuple[type, ...], namespace: dict[str, Any], **kwargs: Any) -> Self: ...
20+
def __new__(
21+
mcls: type[_typeshed.Self], name: str, bases: tuple[type, ...], namespace: dict[str, Any], **kwargs: Any
22+
) -> _typeshed.Self: ...
2023

2124
def __instancecheck__(cls: ABCMeta, instance: Any) -> bool: ...
2225
def __subclasscheck__(cls: ABCMeta, subclass: type) -> bool: ...

stdlib/aifc.pyi

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import sys
2-
from _typeshed import Self
32
from types import TracebackType
43
from typing import IO, Any, NamedTuple, overload
5-
from typing_extensions import Literal, TypeAlias
4+
from typing_extensions import Literal, Self, TypeAlias
65

76
if sys.version_info >= (3, 9):
87
__all__ = ["Error", "open"]
@@ -24,7 +23,7 @@ _Marker: TypeAlias = tuple[int, int, bytes]
2423

2524
class Aifc_read:
2625
def __init__(self, f: _File) -> None: ...
27-
def __enter__(self: Self) -> Self: ...
26+
def __enter__(self) -> Self: ...
2827
def __exit__(
2928
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
3029
) -> None: ...
@@ -48,7 +47,7 @@ class Aifc_read:
4847
class Aifc_write:
4948
def __init__(self, f: _File) -> None: ...
5049
def __del__(self) -> None: ...
51-
def __enter__(self: Self) -> Self: ...
50+
def __enter__(self) -> Self: ...
5251
def __exit__(
5352
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
5453
) -> None: ...

stdlib/array.pyi

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import sys
2-
from _typeshed import ReadableBuffer, Self, SupportsRead, SupportsWrite
2+
from _typeshed import ReadableBuffer, SupportsRead, SupportsWrite
33
from collections.abc import Iterable
44

55
# pytype crashes if array inherits from collections.abc.MutableSequence instead of typing.MutableSequence
66
from typing import Any, Generic, MutableSequence, TypeVar, overload # noqa: Y022
7-
from typing_extensions import Literal, SupportsIndex, TypeAlias
7+
from typing_extensions import Literal, Self, SupportsIndex, TypeAlias
88

99
_IntTypeCode: TypeAlias = Literal["b", "B", "h", "H", "i", "I", "l", "L", "q", "Q"]
1010
_FloatTypeCode: TypeAlias = Literal["f", "d"]
@@ -72,8 +72,8 @@ class array(MutableSequence[_T], Generic[_T]):
7272
def __add__(self, __x: array[_T]) -> array[_T]: ...
7373
def __ge__(self, __other: array[_T]) -> bool: ...
7474
def __gt__(self, __other: array[_T]) -> bool: ...
75-
def __iadd__(self: Self, __x: array[_T]) -> Self: ... # type: ignore[override]
76-
def __imul__(self: Self, __n: int) -> Self: ...
75+
def __iadd__(self, __x: array[_T]) -> Self: ... # type: ignore[override]
76+
def __imul__(self, __n: int) -> Self: ...
7777
def __le__(self, __other: array[_T]) -> bool: ...
7878
def __lt__(self, __other: array[_T]) -> bool: ...
7979
def __mul__(self, __n: int) -> array[_T]: ...

stdlib/asyncio/events.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import ssl
22
import sys
3-
from _typeshed import FileDescriptorLike, ReadableBuffer, Self, StrPath, Unused, WriteableBuffer
3+
from _typeshed import FileDescriptorLike, ReadableBuffer, StrPath, Unused, WriteableBuffer
44
from abc import ABCMeta, abstractmethod
55
from collections.abc import Awaitable, Callable, Coroutine, Generator, Sequence
66
from contextvars import Context
77
from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
88
from typing import IO, Any, Protocol, TypeVar, overload
9-
from typing_extensions import Literal, TypeAlias
9+
from typing_extensions import Literal, Self, TypeAlias
1010

1111
from .base_events import Server
1212
from .futures import Future
@@ -95,7 +95,7 @@ class TimerHandle(Handle):
9595
class AbstractServer:
9696
@abstractmethod
9797
def close(self) -> None: ...
98-
async def __aenter__(self: Self) -> Self: ...
98+
async def __aenter__(self) -> Self: ...
9999
async def __aexit__(self, *exc: Unused) -> None: ...
100100
@abstractmethod
101101
def get_loop(self) -> AbstractEventLoop: ...

stdlib/asyncio/futures.pyi

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import sys
2-
from _typeshed import Self
32
from collections.abc import Awaitable, Callable, Generator, Iterable
43
from concurrent.futures._base import Error, Future as _ConcurrentFuture
54
from typing import Any, TypeVar
6-
from typing_extensions import Literal, TypeGuard
5+
from typing_extensions import Literal, Self, TypeGuard
76

87
from .events import AbstractEventLoop
98

@@ -43,8 +42,8 @@ class Future(Awaitable[_T], Iterable[_T]):
4342
def __del__(self) -> None: ...
4443
def get_loop(self) -> AbstractEventLoop: ...
4544
@property
46-
def _callbacks(self: Self) -> list[tuple[Callable[[Self], Any], Context]]: ...
47-
def add_done_callback(self: Self, __fn: Callable[[Self], object], *, context: Context | None = None) -> None: ...
45+
def _callbacks(self) -> list[tuple[Callable[[Self], Any], Context]]: ...
46+
def add_done_callback(self, __fn: Callable[[Self], object], *, context: Context | None = None) -> None: ...
4847
if sys.version_info >= (3, 9):
4948
def cancel(self, msg: Any | None = None) -> bool: ...
5049
else:
@@ -54,7 +53,7 @@ class Future(Awaitable[_T], Iterable[_T]):
5453
def done(self) -> bool: ...
5554
def result(self) -> _T: ...
5655
def exception(self) -> BaseException | None: ...
57-
def remove_done_callback(self: Self, __fn: Callable[[Self], object]) -> int: ...
56+
def remove_done_callback(self, __fn: Callable[[Self], object]) -> int: ...
5857
def set_result(self, __result: _T) -> None: ...
5958
def set_exception(self, __exception: type | BaseException) -> None: ...
6059
def __iter__(self) -> Generator[Any, None, _T]: ...

stdlib/asyncio/locks.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import enum
22
import sys
3-
from _typeshed import Self, Unused
3+
from _typeshed import Unused
44
from collections import deque
55
from collections.abc import Callable, Generator
66
from types import TracebackType
77
from typing import Any, TypeVar
8-
from typing_extensions import Literal
8+
from typing_extensions import Literal, Self
99

1010
from .events import AbstractEventLoop
1111
from .futures import Future
@@ -103,7 +103,7 @@ if sys.version_info >= (3, 11):
103103

104104
class Barrier(_LoopBoundMixin):
105105
def __init__(self, parties: int) -> None: ...
106-
async def __aenter__(self: Self) -> Self: ...
106+
async def __aenter__(self) -> Self: ...
107107
async def __aexit__(self, *args: Unused) -> None: ...
108108
async def wait(self) -> int: ...
109109
async def abort(self) -> None: ...

stdlib/asyncio/runners.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import sys
2-
from _typeshed import Self, Unused
2+
from _typeshed import Unused
33
from collections.abc import Callable, Coroutine
44
from contextvars import Context
55
from typing import Any, TypeVar
6-
from typing_extensions import final
6+
from typing_extensions import Self, final
77

88
from .events import AbstractEventLoop
99

@@ -17,7 +17,7 @@ if sys.version_info >= (3, 11):
1717
@final
1818
class Runner:
1919
def __init__(self, *, debug: bool | None = None, loop_factory: Callable[[], AbstractEventLoop] | None = None) -> None: ...
20-
def __enter__(self: Self) -> Self: ...
20+
def __enter__(self) -> Self: ...
2121
def __exit__(self, exc_type: Unused, exc_val: Unused, exc_tb: Unused) -> None: ...
2222
def close(self) -> None: ...
2323
def get_loop(self) -> AbstractEventLoop: ...

stdlib/asyncio/streams.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import ssl
22
import sys
3-
from _typeshed import Self, StrPath
3+
from _typeshed import StrPath
44
from collections.abc import AsyncIterator, Awaitable, Callable, Iterable, Sequence
55
from typing import Any
6-
from typing_extensions import SupportsIndex, TypeAlias
6+
from typing_extensions import Self, SupportsIndex, TypeAlias
77

88
from . import events, protocols, transports
99
from .base_events import Server
@@ -166,5 +166,5 @@ class StreamReader(AsyncIterator[bytes]):
166166
async def readuntil(self, separator: bytes | bytearray | memoryview = b"\n") -> bytes: ...
167167
async def read(self, n: int = -1) -> bytes: ...
168168
async def readexactly(self, n: int) -> bytes: ...
169-
def __aiter__(self: Self) -> Self: ...
169+
def __aiter__(self) -> Self: ...
170170
async def __anext__(self) -> bytes: ...

stdlib/asyncio/taskgroups.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# This only exists in 3.11+. See VERSIONS.
22

3-
from _typeshed import Self
43
from collections.abc import Coroutine, Generator
54
from contextvars import Context
65
from types import TracebackType
76
from typing import Any, TypeVar
7+
from typing_extensions import Self
88

99
from .tasks import Task
1010

@@ -13,7 +13,7 @@ __all__ = ["TaskGroup"]
1313
_T = TypeVar("_T")
1414

1515
class TaskGroup:
16-
async def __aenter__(self: Self) -> Self: ...
16+
async def __aenter__(self) -> Self: ...
1717
async def __aexit__(self, et: type[BaseException] | None, exc: BaseException | None, tb: TracebackType | None) -> None: ...
1818
def create_task(
1919
self, coro: Generator[Any, None, _T] | Coroutine[Any, Any, _T], *, name: str | None = None, context: Context | None = None

stdlib/asyncio/timeouts.pyi

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from _typeshed import Self
21
from types import TracebackType
3-
from typing_extensions import final
2+
from typing_extensions import Self, final
43

54
__all__ = ("Timeout", "timeout", "timeout_at")
65

@@ -10,7 +9,7 @@ class Timeout:
109
def when(self) -> float | None: ...
1110
def reschedule(self, when: float | None) -> None: ...
1211
def expired(self) -> bool: ...
13-
async def __aenter__(self: Self) -> Self: ...
12+
async def __aenter__(self) -> Self: ...
1413
async def __aexit__(
1514
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
1615
) -> None: ...

0 commit comments

Comments
 (0)