Skip to content

Commit 7ccbbdb

Browse files
authored
stdlib: Improve many __iter__ and constructor methods (#7112)
1 parent b327f64 commit 7ccbbdb

File tree

11 files changed

+36
-24
lines changed

11 files changed

+36
-24
lines changed

stdlib/_py_abc.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from _typeshed import Self
12
from typing import Any, NewType, TypeVar
23

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

910
class ABCMeta(type):
10-
def __new__(__mcls, __name: str, __bases: tuple[type[Any], ...], __namespace: dict[str, Any]) -> ABCMeta: ...
11+
def __new__(__mcls: type[Self], __name: str, __bases: tuple[type[Any], ...], __namespace: dict[str, Any]) -> Self: ...
1112
def register(cls, subclass: type[_T]) -> type[_T]: ...

stdlib/builtins.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ class frozenset(AbstractSet[_T_co], Generic[_T_co]):
964964

965965
class enumerate(Iterator[tuple[int, _T]], Generic[_T]):
966966
def __init__(self, iterable: Iterable[_T], start: int = ...) -> None: ...
967-
def __iter__(self) -> Iterator[tuple[int, _T]]: ...
967+
def __iter__(self: Self) -> Self: ...
968968
def __next__(self) -> tuple[int, _T]: ...
969969
if sys.version_info >= (3, 9):
970970
def __class_getitem__(cls, __item: Any) -> GenericAlias: ...
@@ -1095,7 +1095,7 @@ class filter(Iterator[_T], Generic[_T]):
10951095
def __init__(self, __function: Callable[[_S], TypeGuard[_T]], __iterable: Iterable[_S]) -> None: ...
10961096
@overload
10971097
def __init__(self, __function: Callable[[_T], Any], __iterable: Iterable[_T]) -> None: ...
1098-
def __iter__(self) -> Iterator[_T]: ...
1098+
def __iter__(self: Self) -> Self: ...
10991099
def __next__(self) -> _T: ...
11001100

11011101
def format(__value: object, __format_spec: str = ...) -> str: ... # TODO unicode
@@ -1186,7 +1186,7 @@ class map(Iterator[_S], Generic[_S]):
11861186
__iter6: Iterable[Any],
11871187
*iterables: Iterable[Any],
11881188
) -> None: ...
1189-
def __iter__(self) -> Iterator[_S]: ...
1189+
def __iter__(self: Self) -> Self: ...
11901190
def __next__(self) -> _S: ...
11911191

11921192
@overload
@@ -1699,7 +1699,7 @@ if sys.version_info >= (3, 11):
16991699
_SplitCondition = type[BaseException] | tuple[type[BaseException], ...] | Callable[[BaseException], bool]
17001700

17011701
class BaseExceptionGroup(BaseException):
1702-
def __new__(cls, __message: str, __exceptions: Sequence[BaseException]) -> BaseExceptionGroup | ExceptionGroup: ...
1702+
def __new__(cls: type[Self], __message: str, __exceptions: Sequence[BaseException]) -> Self: ...
17031703
@property
17041704
def message(self) -> str: ...
17051705
@property
@@ -1709,6 +1709,6 @@ if sys.version_info >= (3, 11):
17091709
def derive(self: Self, __excs: Sequence[BaseException]) -> Self: ...
17101710

17111711
class ExceptionGroup(BaseExceptionGroup, Exception):
1712-
def __new__(cls, __message: str, __exceptions: Sequence[Exception]) -> ExceptionGroup: ...
1712+
def __new__(cls: type[Self], __message: str, __exceptions: Sequence[Exception]) -> Self: ...
17131713
@property
17141714
def exceptions(self) -> tuple[Exception, ...]: ...

stdlib/decimal.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def localcontext(ctx: Context | None = ...) -> _ContextManager: ...
5252
class Decimal:
5353
def __new__(cls: type[Self], value: _DecimalNew = ..., context: Context | None = ...) -> Self: ...
5454
@classmethod
55-
def from_float(cls, __f: float) -> Decimal: ...
55+
def from_float(cls: type[Self], __f: float) -> Self: ...
5656
def __bool__(self) -> bool: ...
5757
def compare(self, other: _Decimal, context: Context | None = ...) -> Decimal: ...
5858
def __hash__(self) -> int: ...

stdlib/dis.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
import types
3+
from _typeshed import Self
34
from opcode import * # `dis` re-exports it as a part of public API
45
from typing import IO, Any, Callable, Iterator, NamedTuple, Union
56

@@ -54,7 +55,7 @@ class Bytecode:
5455
def info(self) -> str: ...
5556
def dis(self) -> str: ...
5657
@classmethod
57-
def from_traceback(cls, tb: types.TracebackType) -> Bytecode: ...
58+
def from_traceback(cls: type[Self], tb: types.TracebackType) -> Self: ...
5859

5960
COMPILER_FLAG_NAMES: dict[int, str]
6061

stdlib/email/headerregistry.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
import types
3+
from _typeshed import Self
34
from collections.abc import Iterable, Mapping
45
from datetime import datetime as _datetime
56
from email._header_value_parser import (
@@ -23,7 +24,7 @@ class BaseHeader(str):
2324
def name(self) -> str: ...
2425
@property
2526
def defects(self) -> tuple[MessageDefect, ...]: ...
26-
def __new__(cls, name: str, value: Any) -> BaseHeader: ...
27+
def __new__(cls: type[Self], name: str, value: Any) -> Self: ...
2728
def init(self, name: str, *, parse_tree: TokenList, defects: Iterable[MessageDefect]) -> None: ...
2829
def fold(self, *, policy: Policy) -> str: ...
2930

stdlib/fractions.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class Fraction(Rational):
2525
@overload
2626
def __new__(cls: type[Self], __value: float | Decimal | str, *, _normalize: bool = ...) -> Self: ...
2727
@classmethod
28-
def from_float(cls, f: float) -> Fraction: ...
28+
def from_float(cls: type[Self], f: float) -> Self: ...
2929
@classmethod
30-
def from_decimal(cls, dec: Decimal) -> Fraction: ...
30+
def from_decimal(cls: type[Self], dec: Decimal) -> Self: ...
3131
def limit_denominator(self, max_denominator: int = ...) -> Fraction: ...
3232
if sys.version_info >= (3, 8):
3333
def as_integer_ratio(self) -> tuple[int, int]: ...

stdlib/sqlite3/dbapi2.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class Cursor(Iterator[Any]):
204204
def fetchone(self) -> Any: ...
205205
def setinputsizes(self, __sizes: object) -> None: ... # does nothing
206206
def setoutputsize(self, __size: object, __column: object = ...) -> None: ... # does nothing
207-
def __iter__(self) -> Cursor: ...
207+
def __iter__(self: Self) -> Self: ...
208208
def __next__(self) -> Any: ...
209209

210210
class DataError(DatabaseError): ...

stdlib/tarfile.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import bz2
22
import io
33
import sys
44
from _typeshed import Self, StrOrBytesPath, StrPath
5+
from builtins import type as Type # alias to avoid name clashes with fields named "type"
56
from collections.abc import Callable, Iterable, Iterator, Mapping
67
from gzip import _ReadableFileobj as _GzipReadableFileobj, _WritableFileobj as _GzipWritableFileobj
78
from types import TracebackType
@@ -339,9 +340,9 @@ class TarInfo:
339340
pax_headers: Mapping[str, str]
340341
def __init__(self, name: str = ...) -> None: ...
341342
@classmethod
342-
def frombuf(cls, buf: bytes, encoding: str, errors: str) -> TarInfo: ...
343+
def frombuf(cls: Type[Self], buf: bytes, encoding: str, errors: str) -> Self: ...
343344
@classmethod
344-
def fromtarfile(cls, tarfile: TarFile) -> TarInfo: ...
345+
def fromtarfile(cls: Type[Self], tarfile: TarFile) -> Self: ...
345346
@property
346347
def linkpath(self) -> str: ...
347348
@linkpath.setter

stdlib/traceback.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from _typeshed import SupportsWrite
2+
from _typeshed import Self, SupportsWrite
33
from types import FrameType, TracebackType
44
from typing import IO, Any, Generator, Iterable, Iterator, Mapping, Optional, overload
55

@@ -98,14 +98,14 @@ class TracebackException:
9898
) -> None: ...
9999
@classmethod
100100
def from_exception(
101-
cls,
101+
cls: type[Self],
102102
exc: BaseException,
103103
*,
104104
limit: int | None = ...,
105105
lookup_lines: bool = ...,
106106
capture_locals: bool = ...,
107107
compact: bool = ...,
108-
) -> TracebackException: ...
108+
) -> Self: ...
109109
else:
110110
def __init__(
111111
self,
@@ -120,8 +120,8 @@ class TracebackException:
120120
) -> None: ...
121121
@classmethod
122122
def from_exception(
123-
cls, exc: BaseException, *, limit: int | None = ..., lookup_lines: bool = ..., capture_locals: bool = ...
124-
) -> TracebackException: ...
123+
cls: type[Self], exc: BaseException, *, limit: int | None = ..., lookup_lines: bool = ..., capture_locals: bool = ...
124+
) -> Self: ...
125125

126126
def format(self, *, chain: bool = ...) -> Generator[str, None, None]: ...
127127
def format_exception_only(self) -> Generator[str, None, None]: ...

stdlib/unittest/mock.pyi

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from _typeshed import Self
23
from typing import Any, Awaitable, Callable, Generic, Iterable, Mapping, Sequence, TypeVar, overload
34
from typing_extensions import Literal
45

@@ -76,8 +77,13 @@ DEFAULT: Any
7677

7778
class _Call(tuple[Any, ...]):
7879
def __new__(
79-
cls, value: Any = ..., name: Any | None = ..., parent: Any | None = ..., two: bool = ..., from_kall: bool = ...
80-
) -> Any: ...
80+
cls: type[Self],
81+
value: Any = ...,
82+
name: Any | None = ...,
83+
parent: Any | None = ...,
84+
two: bool = ...,
85+
from_kall: bool = ...,
86+
) -> Self: ...
8187
name: Any
8288
parent: Any
8389
from_kall: Any
@@ -105,7 +111,7 @@ class Base:
105111
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
106112

107113
class NonCallableMock(Base, Any):
108-
def __new__(__cls, *args: Any, **kw: Any) -> NonCallableMock: ...
114+
def __new__(__cls: type[Self], *args: Any, **kw: Any) -> Self: ...
109115
def __init__(
110116
self,
111117
spec: list[str] | object | type[object] | None = ...,

0 commit comments

Comments
 (0)