Skip to content

stdlib: Improve many __iter__ and constructor methods #7112

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

Merged
merged 3 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion stdlib/_py_abc.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from _typeshed import Self
from typing import Any, NewType, TypeVar

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

class ABCMeta(type):
def __new__(__mcls, __name: str, __bases: tuple[type[Any], ...], __namespace: dict[str, Any]) -> ABCMeta: ...
def __new__(__mcls: type[Self], __name: str, __bases: tuple[type[Any], ...], __namespace: dict[str, Any]) -> Self: ...
def register(cls, subclass: type[_T]) -> type[_T]: ...
10 changes: 5 additions & 5 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ class frozenset(AbstractSet[_T_co], Generic[_T_co]):

class enumerate(Iterator[tuple[int, _T]], Generic[_T]):
def __init__(self, iterable: Iterable[_T], start: int = ...) -> None: ...
def __iter__(self) -> Iterator[tuple[int, _T]]: ...
def __iter__(self: Self) -> Self: ...
def __next__(self) -> tuple[int, _T]: ...
if sys.version_info >= (3, 9):
def __class_getitem__(cls, __item: Any) -> GenericAlias: ...
Expand Down Expand Up @@ -1095,7 +1095,7 @@ class filter(Iterator[_T], Generic[_T]):
def __init__(self, __function: Callable[[_S], TypeGuard[_T]], __iterable: Iterable[_S]) -> None: ...
@overload
def __init__(self, __function: Callable[[_T], Any], __iterable: Iterable[_T]) -> None: ...
def __iter__(self) -> Iterator[_T]: ...
def __iter__(self: Self) -> Self: ...
def __next__(self) -> _T: ...

def format(__value: object, __format_spec: str = ...) -> str: ... # TODO unicode
Expand Down Expand Up @@ -1186,7 +1186,7 @@ class map(Iterator[_S], Generic[_S]):
__iter6: Iterable[Any],
*iterables: Iterable[Any],
) -> None: ...
def __iter__(self) -> Iterator[_S]: ...
def __iter__(self: Self) -> Self: ...
def __next__(self) -> _S: ...

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

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

class ExceptionGroup(BaseExceptionGroup, Exception):
def __new__(cls, __message: str, __exceptions: Sequence[Exception]) -> ExceptionGroup: ...
def __new__(cls: type[Self], __message: str, __exceptions: Sequence[Exception]) -> Self: ...
@property
def exceptions(self) -> tuple[Exception, ...]: ...
2 changes: 1 addition & 1 deletion stdlib/decimal.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def localcontext(ctx: Context | None = ...) -> _ContextManager: ...
class Decimal:
def __new__(cls: type[Self], value: _DecimalNew = ..., context: Context | None = ...) -> Self: ...
@classmethod
def from_float(cls, __f: float) -> Decimal: ...
def from_float(cls: type[Self], __f: float) -> Self: ...
def __bool__(self) -> bool: ...
def compare(self, other: _Decimal, context: Context | None = ...) -> Decimal: ...
def __hash__(self) -> int: ...
Expand Down
3 changes: 2 additions & 1 deletion stdlib/dis.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import types
from _typeshed import Self
from opcode import * # `dis` re-exports it as a part of public API
from typing import IO, Any, Callable, Iterator, NamedTuple, Union

Expand Down Expand Up @@ -54,7 +55,7 @@ class Bytecode:
def info(self) -> str: ...
def dis(self) -> str: ...
@classmethod
def from_traceback(cls, tb: types.TracebackType) -> Bytecode: ...
def from_traceback(cls: type[Self], tb: types.TracebackType) -> Self: ...

COMPILER_FLAG_NAMES: dict[int, str]

Expand Down
3 changes: 2 additions & 1 deletion stdlib/email/headerregistry.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import types
from _typeshed import Self
from collections.abc import Iterable, Mapping
from datetime import datetime as _datetime
from email._header_value_parser import (
Expand All @@ -23,7 +24,7 @@ class BaseHeader(str):
def name(self) -> str: ...
@property
def defects(self) -> tuple[MessageDefect, ...]: ...
def __new__(cls, name: str, value: Any) -> BaseHeader: ...
def __new__(cls: type[Self], name: str, value: Any) -> Self: ...
def init(self, name: str, *, parse_tree: TokenList, defects: Iterable[MessageDefect]) -> None: ...
def fold(self, *, policy: Policy) -> str: ...

Expand Down
4 changes: 2 additions & 2 deletions stdlib/fractions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class Fraction(Rational):
@overload
def __new__(cls: type[Self], __value: float | Decimal | str, *, _normalize: bool = ...) -> Self: ...
@classmethod
def from_float(cls, f: float) -> Fraction: ...
def from_float(cls: type[Self], f: float) -> Self: ...
@classmethod
def from_decimal(cls, dec: Decimal) -> Fraction: ...
def from_decimal(cls: type[Self], dec: Decimal) -> Self: ...
def limit_denominator(self, max_denominator: int = ...) -> Fraction: ...
if sys.version_info >= (3, 8):
def as_integer_ratio(self) -> tuple[int, int]: ...
Expand Down
2 changes: 1 addition & 1 deletion stdlib/sqlite3/dbapi2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class Cursor(Iterator[Any]):
def fetchone(self) -> Any: ...
def setinputsizes(self, __sizes: object) -> None: ... # does nothing
def setoutputsize(self, __size: object, __column: object = ...) -> None: ... # does nothing
def __iter__(self) -> Cursor: ...
def __iter__(self: Self) -> Self: ...
def __next__(self) -> Any: ...

class DataError(DatabaseError): ...
Expand Down
5 changes: 3 additions & 2 deletions stdlib/tarfile.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import bz2
import io
import sys
from _typeshed import Self, StrOrBytesPath, StrPath
from builtins import type as Type # alias to avoid name clashes with fields named "type"
from collections.abc import Callable, Iterable, Iterator, Mapping
from gzip import _ReadableFileobj as _GzipReadableFileobj, _WritableFileobj as _GzipWritableFileobj
from types import TracebackType
Expand Down Expand Up @@ -339,9 +340,9 @@ class TarInfo:
pax_headers: Mapping[str, str]
def __init__(self, name: str = ...) -> None: ...
@classmethod
def frombuf(cls, buf: bytes, encoding: str, errors: str) -> TarInfo: ...
def frombuf(cls: Type[Self], buf: bytes, encoding: str, errors: str) -> Self: ...
@classmethod
def fromtarfile(cls, tarfile: TarFile) -> TarInfo: ...
def fromtarfile(cls: Type[Self], tarfile: TarFile) -> Self: ...
@property
def linkpath(self) -> str: ...
@linkpath.setter
Expand Down
10 changes: 5 additions & 5 deletions stdlib/traceback.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from _typeshed import SupportsWrite
from _typeshed import Self, SupportsWrite
from types import FrameType, TracebackType
from typing import IO, Any, Generator, Iterable, Iterator, Mapping, Optional, overload

Expand Down Expand Up @@ -98,14 +98,14 @@ class TracebackException:
) -> None: ...
@classmethod
def from_exception(
cls,
cls: type[Self],
exc: BaseException,
*,
limit: int | None = ...,
lookup_lines: bool = ...,
capture_locals: bool = ...,
compact: bool = ...,
) -> TracebackException: ...
) -> Self: ...
else:
def __init__(
self,
Expand All @@ -120,8 +120,8 @@ class TracebackException:
) -> None: ...
@classmethod
def from_exception(
cls, exc: BaseException, *, limit: int | None = ..., lookup_lines: bool = ..., capture_locals: bool = ...
) -> TracebackException: ...
cls: type[Self], exc: BaseException, *, limit: int | None = ..., lookup_lines: bool = ..., capture_locals: bool = ...
) -> Self: ...

def format(self, *, chain: bool = ...) -> Generator[str, None, None]: ...
def format_exception_only(self) -> Generator[str, None, None]: ...
Expand Down
12 changes: 9 additions & 3 deletions stdlib/unittest/mock.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from _typeshed import Self
from typing import Any, Awaitable, Callable, Generic, Iterable, Mapping, Sequence, TypeVar, overload
from typing_extensions import Literal

Expand Down Expand Up @@ -76,8 +77,13 @@ DEFAULT: Any

class _Call(tuple[Any, ...]):
def __new__(
cls, value: Any = ..., name: Any | None = ..., parent: Any | None = ..., two: bool = ..., from_kall: bool = ...
) -> Any: ...
cls: type[Self],
value: Any = ...,
name: Any | None = ...,
parent: Any | None = ...,
two: bool = ...,
from_kall: bool = ...,
) -> Self: ...
name: Any
parent: Any
from_kall: Any
Expand Down Expand Up @@ -105,7 +111,7 @@ class Base:
def __init__(self, *args: Any, **kwargs: Any) -> None: ...

class NonCallableMock(Base, Any):
def __new__(__cls, *args: Any, **kw: Any) -> NonCallableMock: ...
def __new__(__cls: type[Self], *args: Any, **kw: Any) -> Self: ...
def __init__(
self,
spec: list[str] | object | type[object] | None = ...,
Expand Down
6 changes: 4 additions & 2 deletions stdlib/zipfile.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,12 @@ class ZipInfo:
def __init__(self, filename: str = ..., date_time: _DateTuple = ...) -> None: ...
if sys.version_info >= (3, 8):
@classmethod
def from_file(cls, filename: StrPath, arcname: StrPath | None = ..., *, strict_timestamps: bool = ...) -> ZipInfo: ...
def from_file(
cls: type[Self], filename: StrPath, arcname: StrPath | None = ..., *, strict_timestamps: bool = ...
) -> Self: ...
else:
@classmethod
def from_file(cls, filename: StrPath, arcname: StrPath | None = ...) -> ZipInfo: ...
def from_file(cls: type[Self], filename: StrPath, arcname: StrPath | None = ...) -> Self: ...

def is_dir(self) -> bool: ...
def FileHeader(self, zip64: bool | None = ...) -> bytes: ...
Expand Down