Skip to content

Commit

Permalink
Use typing_extensions.Self in the stdlib (#9694)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored Feb 9, 2023
1 parent 10086c0 commit 9ed39d8
Show file tree
Hide file tree
Showing 98 changed files with 627 additions and 654 deletions.
15 changes: 7 additions & 8 deletions stdlib/_decimal.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import numbers
import sys
from _typeshed import Self
from collections.abc import Container, Sequence
from types import TracebackType
from typing import Any, ClassVar, NamedTuple, overload
from typing_extensions import Literal, TypeAlias
from typing_extensions import Literal, Self, TypeAlias

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

class Decimal:
def __new__(cls: type[Self], value: _DecimalNew = ..., context: Context | None = ...) -> Self: ...
def __new__(cls, value: _DecimalNew = ..., context: Context | None = ...) -> Self: ...
@classmethod
def from_float(cls: type[Self], __f: float) -> Self: ...
def from_float(cls, __f: float) -> Self: ...
def __bool__(self) -> bool: ...
def compare(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
def as_tuple(self) -> DecimalTuple: ...
Expand Down Expand Up @@ -163,9 +162,9 @@ class Decimal:
def rotate(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
def scaleb(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
def shift(self, other: _Decimal, context: Context | None = None) -> Decimal: ...
def __reduce__(self: Self) -> tuple[type[Self], tuple[str]]: ...
def __copy__(self: Self) -> Self: ...
def __deepcopy__(self: Self, __memo: Any) -> Self: ...
def __reduce__(self) -> tuple[type[Self], tuple[str]]: ...
def __copy__(self) -> Self: ...
def __deepcopy__(self, __memo: Any) -> Self: ...
def __format__(self, __specifier: str, __context: Context | None = ...) -> str: ...

class _ContextManager:
Expand Down Expand Up @@ -203,7 +202,7 @@ class Context:
traps: None | dict[_TrapType, bool] | Container[_TrapType] = ...,
_ignored_flags: list[_TrapType] | None = ...,
) -> None: ...
def __reduce__(self: Self) -> tuple[type[Self], tuple[Any, ...]]: ...
def __reduce__(self) -> tuple[type[Self], tuple[Any, ...]]: ...
def clear_flags(self) -> None: ...
def clear_traps(self) -> None: ...
def copy(self) -> Context: ...
Expand Down
6 changes: 4 additions & 2 deletions stdlib/_py_abc.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from _typeshed import Self
import _typeshed
from typing import Any, NewType, TypeVar

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

class ABCMeta(type):
def __new__(__mcls: type[Self], __name: str, __bases: tuple[type[Any], ...], __namespace: dict[str, Any]) -> Self: ...
def __new__(
__mcls: type[_typeshed.Self], __name: str, __bases: tuple[type[Any], ...], __namespace: dict[str, Any]
) -> _typeshed.Self: ...
def register(cls, subclass: type[_T]) -> type[_T]: ...
5 changes: 2 additions & 3 deletions stdlib/_weakref.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import sys
from _typeshed import Self
from collections.abc import Callable
from typing import Any, Generic, TypeVar, overload
from typing_extensions import final
from typing_extensions import Self, final

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

class ReferenceType(Generic[_T]):
__callback__: Callable[[ReferenceType[_T]], Any]
def __new__(cls: type[Self], o: _T, callback: Callable[[ReferenceType[_T]], Any] | None = ...) -> Self: ...
def __new__(cls, o: _T, callback: Callable[[ReferenceType[_T]], Any] | None = ...) -> Self: ...
def __call__(self) -> _T | None: ...
if sys.version_info >= (3, 9):
def __class_getitem__(cls, item: Any) -> GenericAlias: ...
Expand Down
20 changes: 10 additions & 10 deletions stdlib/_weakrefset.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from _typeshed import Self
from collections.abc import Iterable, Iterator, MutableSet
from typing import Any, Generic, TypeVar, overload
from typing_extensions import Self

if sys.version_info >= (3, 9):
from types import GenericAlias
Expand All @@ -18,21 +18,21 @@ class WeakSet(MutableSet[_T], Generic[_T]):
def __init__(self, data: Iterable[_T]) -> None: ...
def add(self, item: _T) -> None: ...
def discard(self, item: _T) -> None: ...
def copy(self: Self) -> Self: ...
def copy(self) -> Self: ...
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: Self, other: Iterable[_T]) -> Self: ... # type: ignore[override,misc]
def difference(self: Self, other: Iterable[_T]) -> Self: ...
def __sub__(self: Self, other: Iterable[Any]) -> Self: ...
def __ior__(self, other: Iterable[_T]) -> Self: ... # type: ignore[override,misc]
def difference(self, other: Iterable[_T]) -> Self: ...
def __sub__(self, other: Iterable[Any]) -> Self: ...
def difference_update(self, other: Iterable[Any]) -> None: ...
def __isub__(self: Self, other: Iterable[Any]) -> Self: ...
def intersection(self: Self, other: Iterable[_T]) -> Self: ...
def __and__(self: Self, other: Iterable[Any]) -> Self: ...
def __isub__(self, other: Iterable[Any]) -> Self: ...
def intersection(self, other: Iterable[_T]) -> Self: ...
def __and__(self, other: Iterable[Any]) -> Self: ...
def intersection_update(self, other: Iterable[Any]) -> None: ...
def __iand__(self: Self, other: Iterable[Any]) -> Self: ...
def __iand__(self, other: Iterable[Any]) -> Self: ...
def issubset(self, other: Iterable[_T]) -> bool: ...
def __le__(self, other: Iterable[_T]) -> bool: ...
def __lt__(self, other: Iterable[_T]) -> bool: ...
Expand All @@ -43,7 +43,7 @@ class WeakSet(MutableSet[_T], Generic[_T]):
def symmetric_difference(self, other: Iterable[_S]) -> WeakSet[_S | _T]: ...
def __xor__(self, other: Iterable[_S]) -> WeakSet[_S | _T]: ...
def symmetric_difference_update(self, other: Iterable[_T]) -> None: ...
def __ixor__(self: Self, other: Iterable[_T]) -> Self: ... # type: ignore[override,misc]
def __ixor__(self, other: Iterable[_T]) -> Self: ... # type: ignore[override,misc]
def union(self, other: Iterable[_S]) -> WeakSet[_S | _T]: ...
def __or__(self, other: Iterable[_S]) -> WeakSet[_S | _T]: ...
def isdisjoint(self, other: Iterable[_T]) -> bool: ...
Expand Down
11 changes: 7 additions & 4 deletions stdlib/abc.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _typeshed
import sys
from _typeshed import Self, SupportsWrite
from _typeshed import SupportsWrite
from collections.abc import Callable
from typing import Any, Generic, TypeVar
from typing_extensions import Literal
Expand All @@ -13,10 +14,12 @@ class ABCMeta(type):
__abstractmethods__: frozenset[str]
if sys.version_info >= (3, 11):
def __new__(
__mcls: type[Self], __name: str, __bases: tuple[type, ...], __namespace: dict[str, Any], **kwargs: Any
) -> Self: ...
__mcls: type[_typeshed.Self], __name: str, __bases: tuple[type, ...], __namespace: dict[str, Any], **kwargs: Any
) -> _typeshed.Self: ...
else:
def __new__(mcls: type[Self], name: str, bases: tuple[type, ...], namespace: dict[str, Any], **kwargs: Any) -> Self: ...
def __new__(
mcls: type[_typeshed.Self], name: str, bases: tuple[type, ...], namespace: dict[str, Any], **kwargs: Any
) -> _typeshed.Self: ...

def __instancecheck__(cls: ABCMeta, instance: Any) -> bool: ...
def __subclasscheck__(cls: ABCMeta, subclass: type) -> bool: ...
Expand Down
7 changes: 3 additions & 4 deletions stdlib/aifc.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import sys
from _typeshed import Self
from types import TracebackType
from typing import IO, Any, NamedTuple, overload
from typing_extensions import Literal, TypeAlias
from typing_extensions import Literal, Self, TypeAlias

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

class Aifc_read:
def __init__(self, f: _File) -> 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: ...
Expand All @@ -48,7 +47,7 @@ class Aifc_read:
class Aifc_write:
def __init__(self, f: _File) -> None: ...
def __del__(self) -> 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: ...
Expand Down
8 changes: 4 additions & 4 deletions stdlib/array.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import sys
from _typeshed import ReadableBuffer, Self, SupportsRead, SupportsWrite
from _typeshed import ReadableBuffer, SupportsRead, SupportsWrite
from collections.abc import Iterable

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

_IntTypeCode: TypeAlias = Literal["b", "B", "h", "H", "i", "I", "l", "L", "q", "Q"]
_FloatTypeCode: TypeAlias = Literal["f", "d"]
Expand Down Expand Up @@ -72,8 +72,8 @@ class array(MutableSequence[_T], Generic[_T]):
def __add__(self, __x: array[_T]) -> array[_T]: ...
def __ge__(self, __other: array[_T]) -> bool: ...
def __gt__(self, __other: array[_T]) -> bool: ...
def __iadd__(self: Self, __x: array[_T]) -> Self: ... # type: ignore[override]
def __imul__(self: Self, __n: int) -> Self: ...
def __iadd__(self, __x: array[_T]) -> Self: ... # type: ignore[override]
def __imul__(self, __n: int) -> Self: ...
def __le__(self, __other: array[_T]) -> bool: ...
def __lt__(self, __other: array[_T]) -> bool: ...
def __mul__(self, __n: int) -> array[_T]: ...
Expand Down
6 changes: 3 additions & 3 deletions stdlib/asyncio/events.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import ssl
import sys
from _typeshed import FileDescriptorLike, ReadableBuffer, Self, StrPath, Unused, WriteableBuffer
from _typeshed import FileDescriptorLike, ReadableBuffer, StrPath, Unused, WriteableBuffer
from abc import ABCMeta, abstractmethod
from collections.abc import Awaitable, Callable, Coroutine, Generator, Sequence
from contextvars import Context
from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
from typing import IO, Any, Protocol, TypeVar, overload
from typing_extensions import Literal, TypeAlias
from typing_extensions import Literal, Self, TypeAlias

from .base_events import Server
from .futures import Future
Expand Down Expand Up @@ -95,7 +95,7 @@ class TimerHandle(Handle):
class AbstractServer:
@abstractmethod
def close(self) -> None: ...
async def __aenter__(self: Self) -> Self: ...
async def __aenter__(self) -> Self: ...
async def __aexit__(self, *exc: Unused) -> None: ...
@abstractmethod
def get_loop(self) -> AbstractEventLoop: ...
Expand Down
9 changes: 4 additions & 5 deletions stdlib/asyncio/futures.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import sys
from _typeshed import Self
from collections.abc import Awaitable, Callable, Generator, Iterable
from concurrent.futures._base import Error, Future as _ConcurrentFuture
from typing import Any, TypeVar
from typing_extensions import Literal, TypeGuard
from typing_extensions import Literal, Self, TypeGuard

from .events import AbstractEventLoop

Expand Down Expand Up @@ -43,8 +42,8 @@ class Future(Awaitable[_T], Iterable[_T]):
def __del__(self) -> None: ...
def get_loop(self) -> AbstractEventLoop: ...
@property
def _callbacks(self: Self) -> list[tuple[Callable[[Self], Any], Context]]: ...
def add_done_callback(self: Self, __fn: Callable[[Self], object], *, context: Context | None = None) -> None: ...
def _callbacks(self) -> list[tuple[Callable[[Self], Any], Context]]: ...
def add_done_callback(self, __fn: Callable[[Self], object], *, context: Context | None = None) -> None: ...
if sys.version_info >= (3, 9):
def cancel(self, msg: Any | None = None) -> bool: ...
else:
Expand All @@ -54,7 +53,7 @@ class Future(Awaitable[_T], Iterable[_T]):
def done(self) -> bool: ...
def result(self) -> _T: ...
def exception(self) -> BaseException | None: ...
def remove_done_callback(self: Self, __fn: Callable[[Self], object]) -> int: ...
def remove_done_callback(self, __fn: Callable[[Self], object]) -> int: ...
def set_result(self, __result: _T) -> None: ...
def set_exception(self, __exception: type | BaseException) -> None: ...
def __iter__(self) -> Generator[Any, None, _T]: ...
Expand Down
6 changes: 3 additions & 3 deletions stdlib/asyncio/locks.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import enum
import sys
from _typeshed import Self, Unused
from _typeshed import Unused
from collections import deque
from collections.abc import Callable, Generator
from types import TracebackType
from typing import Any, TypeVar
from typing_extensions import Literal
from typing_extensions import Literal, Self

from .events import AbstractEventLoop
from .futures import Future
Expand Down Expand Up @@ -103,7 +103,7 @@ if sys.version_info >= (3, 11):

class Barrier(_LoopBoundMixin):
def __init__(self, parties: int) -> None: ...
async def __aenter__(self: Self) -> Self: ...
async def __aenter__(self) -> Self: ...
async def __aexit__(self, *args: Unused) -> None: ...
async def wait(self) -> int: ...
async def abort(self) -> None: ...
Expand Down
6 changes: 3 additions & 3 deletions stdlib/asyncio/runners.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import sys
from _typeshed import Self, Unused
from _typeshed import Unused
from collections.abc import Callable, Coroutine
from contextvars import Context
from typing import Any, TypeVar
from typing_extensions import final
from typing_extensions import Self, final

from .events import AbstractEventLoop

Expand All @@ -17,7 +17,7 @@ if sys.version_info >= (3, 11):
@final
class Runner:
def __init__(self, *, debug: bool | None = None, loop_factory: Callable[[], AbstractEventLoop] | None = None) -> None: ...
def __enter__(self: Self) -> Self: ...
def __enter__(self) -> Self: ...
def __exit__(self, exc_type: Unused, exc_val: Unused, exc_tb: Unused) -> None: ...
def close(self) -> None: ...
def get_loop(self) -> AbstractEventLoop: ...
Expand Down
6 changes: 3 additions & 3 deletions stdlib/asyncio/streams.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import ssl
import sys
from _typeshed import Self, StrPath
from _typeshed import StrPath
from collections.abc import AsyncIterator, Awaitable, Callable, Iterable, Sequence
from typing import Any
from typing_extensions import SupportsIndex, TypeAlias
from typing_extensions import Self, SupportsIndex, TypeAlias

from . import events, protocols, transports
from .base_events import Server
Expand Down Expand Up @@ -166,5 +166,5 @@ class StreamReader(AsyncIterator[bytes]):
async def readuntil(self, separator: bytes | bytearray | memoryview = b"\n") -> bytes: ...
async def read(self, n: int = -1) -> bytes: ...
async def readexactly(self, n: int) -> bytes: ...
def __aiter__(self: Self) -> Self: ...
def __aiter__(self) -> Self: ...
async def __anext__(self) -> bytes: ...
4 changes: 2 additions & 2 deletions stdlib/asyncio/taskgroups.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# This only exists in 3.11+. See VERSIONS.

from _typeshed import Self
from collections.abc import Coroutine, Generator
from contextvars import Context
from types import TracebackType
from typing import Any, TypeVar
from typing_extensions import Self

from .tasks import Task

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

class TaskGroup:
async def __aenter__(self: Self) -> Self: ...
async def __aenter__(self) -> Self: ...
async def __aexit__(self, et: type[BaseException] | None, exc: BaseException | None, tb: TracebackType | None) -> None: ...
def create_task(
self, coro: Generator[Any, None, _T] | Coroutine[Any, Any, _T], *, name: str | None = None, context: Context | None = None
Expand Down
5 changes: 2 additions & 3 deletions stdlib/asyncio/timeouts.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from _typeshed import Self
from types import TracebackType
from typing_extensions import final
from typing_extensions import Self, final

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

Expand All @@ -10,7 +9,7 @@ class Timeout:
def when(self) -> float | None: ...
def reschedule(self, when: float | None) -> None: ...
def expired(self) -> bool: ...
async def __aenter__(self: Self) -> Self: ...
async def __aenter__(self) -> Self: ...
async def __aexit__(
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
) -> None: ...
Expand Down
Loading

0 comments on commit 9ed39d8

Please sign in to comment.