From a5c9adfd8b98fe330e438c832846f13437ab0cba Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Fri, 6 Dec 2024 12:40:14 -0800 Subject: [PATCH 1/6] fix the __init__ of several C-classes --- stdlib/_asyncio.pyi | 6 +++--- stdlib/_bz2.pyi | 2 +- stdlib/_io.pyi | 2 +- stdlib/_pickle.pyi | 1 - stdlib/decimal.pyi | 1 - 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/stdlib/_asyncio.pyi b/stdlib/_asyncio.pyi index a259026615aa..89cdff6cc283 100644 --- a/stdlib/_asyncio.pyi +++ b/stdlib/_asyncio.pyi @@ -65,7 +65,7 @@ class Task(Future[_T_co]): # type: ignore[type-var] # pyright: ignore[reportIn self, coro: _TaskCompatibleCoro[_T_co], *, - loop: AbstractEventLoop = ..., + loop: AbstractEventLoop | None = None, name: str | None = ..., context: Context | None = None, eager_start: bool = False, @@ -75,13 +75,13 @@ class Task(Future[_T_co]): # type: ignore[type-var] # pyright: ignore[reportIn self, coro: _TaskCompatibleCoro[_T_co], *, - loop: AbstractEventLoop = ..., + loop: AbstractEventLoop | None = None, name: str | None = ..., context: Context | None = None, ) -> None: ... else: def __init__( - self, coro: _TaskCompatibleCoro[_T_co], *, loop: AbstractEventLoop = ..., name: str | None = ... + self, coro: _TaskCompatibleCoro[_T_co], *, loop: AbstractEventLoop | None = None, name: str | None = ... ) -> None: ... if sys.version_info >= (3, 12): diff --git a/stdlib/_bz2.pyi b/stdlib/_bz2.pyi index 4ba26fe96be0..d5eeeb6494c9 100644 --- a/stdlib/_bz2.pyi +++ b/stdlib/_bz2.pyi @@ -3,7 +3,7 @@ from typing import final @final class BZ2Compressor: - def __init__(self, compresslevel: int = 9) -> None: ... + def __init__(self, compresslevel: int = 9, /) -> None: ... def compress(self, data: ReadableBuffer, /) -> bytes: ... def flush(self) -> bytes: ... diff --git a/stdlib/_io.pyi b/stdlib/_io.pyi index 284d99f92b60..54efd3199760 100644 --- a/stdlib/_io.pyi +++ b/stdlib/_io.pyi @@ -112,7 +112,7 @@ class BufferedRandom(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore def truncate(self, pos: int | None = None, /) -> int: ... class BufferedRWPair(BufferedIOBase, _BufferedIOBase): - def __init__(self, reader: RawIOBase, writer: RawIOBase, buffer_size: int = 8192) -> None: ... + def __init__(self, reader: RawIOBase, writer: RawIOBase, buffer_size: int = 8192, /) -> None: ... def peek(self, size: int = 0, /) -> bytes: ... class _TextIOBase(_IOBase): diff --git a/stdlib/_pickle.pyi b/stdlib/_pickle.pyi index 5566f0f65d6e..50bbb6bc16cd 100644 --- a/stdlib/_pickle.pyi +++ b/stdlib/_pickle.pyi @@ -66,7 +66,6 @@ class Pickler: self, file: SupportsWrite[bytes], protocol: int | None = None, - *, fix_imports: bool = True, buffer_callback: _BufferCallback = None, ) -> None: ... diff --git a/stdlib/decimal.pyi b/stdlib/decimal.pyi index 7f8708a020fd..7eb922c8a7ed 100644 --- a/stdlib/decimal.pyi +++ b/stdlib/decimal.pyi @@ -189,7 +189,6 @@ class Context: clamp: int | None = ..., flags: None | dict[_TrapType, bool] | Container[_TrapType] = ..., traps: None | dict[_TrapType, bool] | Container[_TrapType] = ..., - _ignored_flags: list[_TrapType] | None = ..., ) -> None: ... def __reduce__(self) -> tuple[type[Self], tuple[Any, ...]]: ... def clear_flags(self) -> None: ... From 7db162df396878fd1d2a82f2d8b1d7f61b605174 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Sat, 7 Dec 2024 01:10:56 -0800 Subject: [PATCH 2/6] validating both __new__ and __init__ against class.__text_signature__ --- stdlib/_asyncio.pyi | 21 +++++++++++++ stdlib/_blake2.pyi | 24 +++++++-------- stdlib/_bz2.pyi | 7 ++++- stdlib/_contextvars.pyi | 6 ++-- stdlib/_csv.pyi | 6 ++-- stdlib/_ctypes.pyi | 14 ++++----- stdlib/_io.pyi | 30 +++++++++++++++++++ stdlib/_json.pyi | 9 +++--- stdlib/_lzma.pyi | 19 ++++++++---- stdlib/_pickle.pyi | 18 +++++++++++- stdlib/_random.pyi | 8 +++-- stdlib/_struct.pyi | 5 ++++ stdlib/array.pyi | 20 +++++++------ stdlib/builtins.pyi | 29 ++++++++++++++---- stdlib/datetime.pyi | 2 +- stdlib/decimal.pyi | 11 +++++++ stdlib/ipaddress.pyi | 8 +++-- stdlib/itertools.pyi | 24 +++++++-------- stdlib/mmap.pyi | 12 ++++---- stdlib/operator.pyi | 4 +-- stdlib/pickle.pyi | 3 +- stdlib/sqlite3/__init__.pyi | 2 +- stdlib/types.pyi | 35 +++++++++++++++------- stdlib/typing.pyi | 57 +++++++++++++++++++++++------------- stdlib/zoneinfo/__init__.pyi | 2 +- 25 files changed, 266 insertions(+), 110 deletions(-) diff --git a/stdlib/_asyncio.pyi b/stdlib/_asyncio.pyi index 89cdff6cc283..62e64ed14f44 100644 --- a/stdlib/_asyncio.pyi +++ b/stdlib/_asyncio.pyi @@ -24,6 +24,7 @@ class Future(Awaitable[_T]): def _log_traceback(self, val: Literal[False]) -> None: ... _asyncio_future_blocking: bool # is a part of duck-typing contract for `Future` def __init__(self, *, loop: AbstractEventLoop | None = ...) -> None: ... + def __new__(cls, *, loop: AbstractEventLoop | None = ...) -> Self: ... def __del__(self) -> None: ... def get_loop(self) -> AbstractEventLoop: ... @property @@ -70,6 +71,15 @@ class Task(Future[_T_co]): # type: ignore[type-var] # pyright: ignore[reportIn context: Context | None = None, eager_start: bool = False, ) -> None: ... + def __new__( + cls, + coro: _TaskCompatibleCoro[_T_co], + *, + loop: AbstractEventLoop | None = None, + name: str | None = ..., + context: Context | None = None, + eager_start: bool = False, + ) -> Self: ... elif sys.version_info >= (3, 11): def __init__( self, @@ -79,10 +89,21 @@ class Task(Future[_T_co]): # type: ignore[type-var] # pyright: ignore[reportIn name: str | None = ..., context: Context | None = None, ) -> None: ... + def __new__( + cls, + coro: _TaskCompatibleCoro[_T_co], + *, + loop: AbstractEventLoop | None = None, + name: str | None = ..., + context: Context | None = None, + ) -> Self: ... else: def __init__( self, coro: _TaskCompatibleCoro[_T_co], *, loop: AbstractEventLoop | None = None, name: str | None = ... ) -> None: ... + def __new__( + cls, coro: _TaskCompatibleCoro[_T_co], *, loop: AbstractEventLoop | None = None, name: str | None = ... + ) -> Self: ... if sys.version_info >= (3, 12): def get_coro(self) -> _TaskCompatibleCoro[_T_co] | None: ... diff --git a/stdlib/_blake2.pyi b/stdlib/_blake2.pyi index 10d7019a222f..3d17cb59c79b 100644 --- a/stdlib/_blake2.pyi +++ b/stdlib/_blake2.pyi @@ -22,8 +22,8 @@ class blake2b: digest_size: int name: str if sys.version_info >= (3, 9): - def __init__( - self, + def __new__( + cls, data: ReadableBuffer = b"", /, *, @@ -39,10 +39,10 @@ class blake2b: inner_size: int = 0, last_node: bool = False, usedforsecurity: bool = True, - ) -> None: ... + ) -> Self: ... else: - def __init__( - self, + def __new__( + cls, data: ReadableBuffer = b"", /, *, @@ -57,7 +57,7 @@ class blake2b: node_depth: int = 0, inner_size: int = 0, last_node: bool = False, - ) -> None: ... + ) -> Self: ... def copy(self) -> Self: ... def digest(self) -> bytes: ... @@ -74,8 +74,8 @@ class blake2s: digest_size: int name: str if sys.version_info >= (3, 9): - def __init__( - self, + def __new__( + cls, data: ReadableBuffer = b"", /, *, @@ -91,10 +91,10 @@ class blake2s: inner_size: int = 0, last_node: bool = False, usedforsecurity: bool = True, - ) -> None: ... + ) -> Self: ... else: - def __init__( - self, + def __new__( + cls, data: ReadableBuffer = b"", /, *, @@ -109,7 +109,7 @@ class blake2s: node_depth: int = 0, inner_size: int = 0, last_node: bool = False, - ) -> None: ... + ) -> Self: ... def copy(self) -> Self: ... def digest(self) -> bytes: ... diff --git a/stdlib/_bz2.pyi b/stdlib/_bz2.pyi index d5eeeb6494c9..87e9ddfce633 100644 --- a/stdlib/_bz2.pyi +++ b/stdlib/_bz2.pyi @@ -1,9 +1,14 @@ +import sys from _typeshed import ReadableBuffer from typing import final +from typing_extensions import Self @final class BZ2Compressor: - def __init__(self, compresslevel: int = 9, /) -> None: ... + if sys.version_info < (3, 12): + def __init__(self, compresslevel: int = 9, /) -> None: ... + + def __new__(cls, compresslevel: int = 9, /) -> Self: ... def compress(self, data: ReadableBuffer, /) -> bytes: ... def flush(self) -> bytes: ... diff --git a/stdlib/_contextvars.pyi b/stdlib/_contextvars.pyi index 2e21a8c5d017..f84a758d6e09 100644 --- a/stdlib/_contextvars.pyi +++ b/stdlib/_contextvars.pyi @@ -1,7 +1,7 @@ import sys from collections.abc import Callable, Iterator, Mapping from typing import Any, ClassVar, Generic, TypeVar, final, overload -from typing_extensions import ParamSpec +from typing_extensions import ParamSpec, Self if sys.version_info >= (3, 9): from types import GenericAlias @@ -13,9 +13,9 @@ _P = ParamSpec("_P") @final class ContextVar(Generic[_T]): @overload - def __init__(self, name: str) -> None: ... + def __new__(cls, name: str) -> Self: ... @overload - def __init__(self, name: str, *, default: _T) -> None: ... + def __new__(cls, name: str, *, default: _T) -> Self: ... def __hash__(self) -> int: ... @property def name(self) -> str: ... diff --git a/stdlib/_csv.pyi b/stdlib/_csv.pyi index afa2870be158..8262114346ef 100644 --- a/stdlib/_csv.pyi +++ b/stdlib/_csv.pyi @@ -32,8 +32,8 @@ class Dialect: lineterminator: str quoting: _QuotingType strict: bool - def __init__( - self, + def __new__( + cls, dialect: _DialectLike | None = ..., delimiter: str = ",", doublequote: bool = True, @@ -43,7 +43,7 @@ class Dialect: quoting: _QuotingType = 0, skipinitialspace: bool = False, strict: bool = False, - ) -> None: ... + ) -> Self: ... if sys.version_info >= (3, 10): # This class calls itself _csv.reader. diff --git a/stdlib/_ctypes.pyi b/stdlib/_ctypes.pyi index 1f3f8f38802b..0ce1cb39eff6 100644 --- a/stdlib/_ctypes.pyi +++ b/stdlib/_ctypes.pyi @@ -169,18 +169,18 @@ class CFuncPtr(_PointerLike, _CData, metaclass=_PyCFuncPtrType): # Abstract attribute that must be defined on subclasses _flags_: ClassVar[int] @overload - def __init__(self) -> None: ... + def __new__(cls) -> Self: ... @overload - def __init__(self, address: int, /) -> None: ... + def __new__(cls, address: int, /) -> Self: ... @overload - def __init__(self, callable: Callable[..., Any], /) -> None: ... + def __new__(cls, callable: Callable[..., Any], /) -> Self: ... @overload - def __init__(self, func_spec: tuple[str | int, CDLL], paramflags: tuple[_PF, ...] | None = ..., /) -> None: ... + def __new__(cls, func_spec: tuple[str | int, CDLL], paramflags: tuple[_PF, ...] | None = ..., /) -> Self: ... if sys.platform == "win32": @overload - def __init__( - self, vtbl_index: int, name: str, paramflags: tuple[_PF, ...] | None = ..., iid: _CData | _CDataType | None = ..., / - ) -> None: ... + def __new__( + cls, vtbl_index: int, name: str, paramflags: tuple[_PF, ...] | None = ..., iid: _CData | _CDataType | None = ..., / + ) -> Self: ... def __call__(self, *args: Any, **kwargs: Any) -> Any: ... diff --git a/stdlib/_io.pyi b/stdlib/_io.pyi index 54efd3199760..4f9dc9007e80 100644 --- a/stdlib/_io.pyi +++ b/stdlib/_io.pyi @@ -71,6 +71,9 @@ class FileIO(RawIOBase, _RawIOBase, BinaryIO): # type: ignore[misc] # incompat def __init__( self, file: FileDescriptorOrPath, mode: str = "r", closefd: bool = True, opener: _Opener | None = None ) -> None: ... + def __new__( + cls, file: FileDescriptorOrPath, mode: str = "r", closefd: bool = True, opener: _Opener | None = None + ) -> Self: ... @property def closefd(self) -> bool: ... def seek(self, pos: int, whence: int = 0, /) -> int: ... @@ -78,6 +81,7 @@ class FileIO(RawIOBase, _RawIOBase, BinaryIO): # type: ignore[misc] # incompat class BytesIO(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of methods in the base classes def __init__(self, initial_bytes: ReadableBuffer = b"") -> None: ... + def __new__(cls, initial_bytes: ReadableBuffer = b"") -> Self: ... # BytesIO does not contain a "name" field. This workaround is necessary # to allow BytesIO sub-classes to add this field, as it is defined # as a read-only property on IO[]. @@ -91,6 +95,9 @@ class BytesIO(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore[misc] class BufferedReader(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of methods in the base classes raw: RawIOBase def __init__(self, raw: RawIOBase, buffer_size: int = 8192) -> None: ... + if sys.version_info < (3, 12): + def __new__(cls, raw: RawIOBase, buffer_size: int = 8192) -> Self: ... + def peek(self, size: int = 0, /) -> bytes: ... def seek(self, target: int, whence: int = 0, /) -> int: ... def truncate(self, pos: int | None = None, /) -> int: ... @@ -98,6 +105,9 @@ class BufferedReader(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore class BufferedWriter(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of writelines in the base classes raw: RawIOBase def __init__(self, raw: RawIOBase, buffer_size: int = 8192) -> None: ... + if sys.version_info < (3, 12): + def __new__(cls, raw: RawIOBase, buffer_size: int = 8192) -> Self: ... + def write(self, buffer: ReadableBuffer, /) -> int: ... def seek(self, target: int, whence: int = 0, /) -> int: ... def truncate(self, pos: int | None = None, /) -> int: ... @@ -107,12 +117,18 @@ class BufferedRandom(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore name: Any raw: RawIOBase def __init__(self, raw: RawIOBase, buffer_size: int = 8192) -> None: ... + if sys.version_info < (3, 12): + def __new__(cls, raw: RawIOBase, buffer_size: int = 8192) -> Self: ... + def seek(self, target: int, whence: int = 0, /) -> int: ... # stubtest needs this def peek(self, size: int = 0, /) -> bytes: ... def truncate(self, pos: int | None = None, /) -> int: ... class BufferedRWPair(BufferedIOBase, _BufferedIOBase): def __init__(self, reader: RawIOBase, writer: RawIOBase, buffer_size: int = 8192, /) -> None: ... + if sys.version_info < (3, 12): + def __new__(cls, reader: RawIOBase, writer: RawIOBase, buffer_size: int = 8192, /) -> Self: ... + def peek(self, size: int = 0, /) -> bytes: ... class _TextIOBase(_IOBase): @@ -164,6 +180,16 @@ class TextIOWrapper(TextIOBase, _TextIOBase, TextIO, Generic[_BufferT_co]): # t line_buffering: bool = False, write_through: bool = False, ) -> None: ... + if sys.version_info < (3, 12): + def __new__( + cls, + buffer: _BufferT_co, + encoding: str | None = None, + errors: str | None = None, + newline: str | None = None, + line_buffering: bool = False, + write_through: bool = False, + ) -> Self: ... # Equals the "buffer" argument passed in to the constructor. @property def buffer(self) -> _BufferT_co: ... # type: ignore[override] @@ -190,6 +216,7 @@ class TextIOWrapper(TextIOBase, _TextIOBase, TextIO, Generic[_BufferT_co]): # t class StringIO(TextIOBase, _TextIOBase, TextIO): # type: ignore[misc] # incompatible definitions of write in the base classes def __init__(self, initial_value: str | None = "", newline: str | None = "\n") -> None: ... + def __new__(cls, initial_value: str | None = "", newline: str | None = "\n") -> Self: ... # StringIO does not contain a "name" field. This workaround is necessary # to allow StringIO sub-classes to add this field, as it is defined # as a read-only property on IO[]. @@ -202,6 +229,9 @@ class StringIO(TextIOBase, _TextIOBase, TextIO): # type: ignore[misc] # incomp class IncrementalNewlineDecoder: def __init__(self, decoder: codecs.IncrementalDecoder | None, translate: bool, errors: str = "strict") -> None: ... + if sys.version_info < (3, 12): + def __new__(cls, decoder: codecs.IncrementalDecoder | None, translate: bool, errors: str = "strict") -> Self: ... + def decode(self, input: ReadableBuffer | str, final: bool = False) -> str: ... @property def newlines(self) -> str | tuple[str, ...] | None: ... diff --git a/stdlib/_json.pyi b/stdlib/_json.pyi index e1c7c52ca3b1..5296b8e62a02 100644 --- a/stdlib/_json.pyi +++ b/stdlib/_json.pyi @@ -1,5 +1,6 @@ from collections.abc import Callable from typing import Any, final +from typing_extensions import Self @final class make_encoder: @@ -19,8 +20,8 @@ class make_encoder: def encoder(self) -> Callable[[str], str]: ... @property def item_separator(self) -> str: ... - def __init__( - self, + def __new__( + cls, markers: dict[int, Any] | None, default: Callable[[Any], Any], encoder: Callable[[str], str], @@ -30,7 +31,7 @@ class make_encoder: sort_keys: bool, skipkeys: bool, allow_nan: bool, - ) -> None: ... + ) -> Self: ... def __call__(self, obj: object, _current_indent_level: int) -> Any: ... @final @@ -42,7 +43,7 @@ class make_scanner: parse_float: Any strict: bool # TODO: 'context' needs the attrs above (ducktype), but not __call__. - def __init__(self, context: make_scanner) -> None: ... + def __new__(cls, context: make_scanner) -> Self: ... def __call__(self, string: str, index: int) -> tuple[Any, int]: ... def encode_basestring(s: str, /) -> str: ... diff --git a/stdlib/_lzma.pyi b/stdlib/_lzma.pyi index 1f5be78876c6..6b2d48d57c58 100644 --- a/stdlib/_lzma.pyi +++ b/stdlib/_lzma.pyi @@ -1,7 +1,8 @@ +import sys from _typeshed import ReadableBuffer from collections.abc import Mapping, Sequence from typing import Any, Final, final -from typing_extensions import TypeAlias +from typing_extensions import Self, TypeAlias _FilterChain: TypeAlias = Sequence[Mapping[str, Any]] @@ -36,7 +37,10 @@ PRESET_EXTREME: int # v big number @final class LZMADecompressor: - def __init__(self, format: int | None = ..., memlimit: int | None = ..., filters: _FilterChain | None = ...) -> None: ... + if sys.version_info < (3, 12): + def __init__(self, format: int | None = ..., memlimit: int | None = ..., filters: _FilterChain | None = ...) -> None: ... + + def __new__(cls, format: int | None = ..., memlimit: int | None = ..., filters: _FilterChain | None = ...) -> Self: ... def decompress(self, data: ReadableBuffer, max_length: int = -1) -> bytes: ... @property def check(self) -> int: ... @@ -49,9 +53,14 @@ class LZMADecompressor: @final class LZMACompressor: - def __init__( - self, format: int | None = ..., check: int = ..., preset: int | None = ..., filters: _FilterChain | None = ... - ) -> None: ... + if sys.version_info < (3, 12): + def __init__( + self, format: int | None = ..., check: int = ..., preset: int | None = ..., filters: _FilterChain | None = ... + ) -> None: ... + + def __new__( + cls, format: int | None = ..., check: int = ..., preset: int | None = ..., filters: _FilterChain | None = ... + ) -> Self: ... def compress(self, data: ReadableBuffer, /) -> bytes: ... def flush(self) -> bytes: ... diff --git a/stdlib/_pickle.pyi b/stdlib/_pickle.pyi index 50bbb6bc16cd..313e3eba2ea7 100644 --- a/stdlib/_pickle.pyi +++ b/stdlib/_pickle.pyi @@ -3,7 +3,7 @@ from _typeshed import ReadableBuffer, SupportsWrite from collections.abc import Callable, Iterable, Iterator, Mapping from pickle import PickleBuffer as PickleBuffer from typing import Any, Protocol, type_check_only -from typing_extensions import TypeAlias +from typing_extensions import Self, TypeAlias class _ReadableFileobj(Protocol): def read(self, n: int, /) -> bytes: ... @@ -69,6 +69,13 @@ class Pickler: fix_imports: bool = True, buffer_callback: _BufferCallback = None, ) -> None: ... + def __new__( + cls, + file: SupportsWrite[bytes], + protocol: int | None = None, + fix_imports: bool = True, + buffer_callback: _BufferCallback = None, + ) -> Self: ... @property def memo(self) -> PicklerMemoProxy: ... @memo.setter @@ -95,6 +102,15 @@ class Unpickler: errors: str = "strict", buffers: Iterable[Any] | None = (), ) -> None: ... + def __new__( + cls, + file: _ReadableFileobj, + *, + fix_imports: bool = True, + encoding: str = "ASCII", + errors: str = "strict", + buffers: Iterable[Any] | None = (), + ) -> Self: ... @property def memo(self) -> UnpicklerMemoProxy: ... @memo.setter diff --git a/stdlib/_random.pyi b/stdlib/_random.pyi index 4082344ade8e..c2deec7df6ce 100644 --- a/stdlib/_random.pyi +++ b/stdlib/_random.pyi @@ -1,10 +1,14 @@ -from typing_extensions import TypeAlias +import sys +from typing_extensions import Self, TypeAlias # Actually Tuple[(int,) * 625] _State: TypeAlias = tuple[int, ...] class Random: - def __init__(self, seed: object = ...) -> None: ... + if sys.version_info >= (3, 11): + def __init__(self, seed: object = ...) -> None: ... + + def __new__(cls, seed: object = ...) -> Self: ... def seed(self, n: object = None, /) -> None: ... def getstate(self) -> _State: ... def setstate(self, state: _State, /) -> None: ... diff --git a/stdlib/_struct.pyi b/stdlib/_struct.pyi index 662170e869f3..6249e3e502eb 100644 --- a/stdlib/_struct.pyi +++ b/stdlib/_struct.pyi @@ -1,6 +1,8 @@ +import sys from _typeshed import ReadableBuffer, WriteableBuffer from collections.abc import Iterator from typing import Any +from typing_extensions import Self def pack(fmt: str | bytes, /, *v: Any) -> bytes: ... def pack_into(fmt: str | bytes, buffer: WriteableBuffer, offset: int, /, *v: Any) -> None: ... @@ -15,6 +17,9 @@ class Struct: @property def size(self) -> int: ... def __init__(self, format: str | bytes) -> None: ... + if sys.version_info < (3, 9): + def __new__(cls, format: str | bytes) -> Self: ... + def pack(self, *v: Any) -> bytes: ... def pack_into(self, buffer: WriteableBuffer, offset: int, *v: Any) -> None: ... def unpack(self, buffer: ReadableBuffer, /) -> tuple[Any, ...]: ... diff --git a/stdlib/array.pyi b/stdlib/array.pyi index 878d8d8cb808..0cdd651f9ad5 100644 --- a/stdlib/array.pyi +++ b/stdlib/array.pyi @@ -24,19 +24,21 @@ class array(MutableSequence[_T]): @property def itemsize(self) -> int: ... @overload - def __init__(self: array[int], typecode: _IntTypeCode, initializer: bytes | bytearray | Iterable[int] = ..., /) -> None: ... + def __new__( + cls: type[array[int]], typecode: _IntTypeCode, initializer: bytes | bytearray | Iterable[int] = ..., / + ) -> array[int]: ... @overload - def __init__( - self: array[float], typecode: _FloatTypeCode, initializer: bytes | bytearray | Iterable[float] = ..., / - ) -> None: ... + def __new__( + cls: type[array[float]], typecode: _FloatTypeCode, initializer: bytes | bytearray | Iterable[float] = ..., / + ) -> array[float]: ... @overload - def __init__( - self: array[str], typecode: _UnicodeTypeCode, initializer: bytes | bytearray | Iterable[str] = ..., / - ) -> None: ... + def __new__( + cls: type[array[str]], typecode: _UnicodeTypeCode, initializer: bytes | bytearray | Iterable[str] = ..., / + ) -> array[str]: ... @overload - def __init__(self, typecode: str, initializer: Iterable[_T], /) -> None: ... + def __new__(cls, typecode: str, initializer: Iterable[_T], /) -> Self: ... @overload - def __init__(self, typecode: str, initializer: bytes | bytearray = ..., /) -> None: ... + def __new__(cls, typecode: str, initializer: bytes | bytearray = ..., /) -> Self: ... def append(self, v: _T, /) -> None: ... def buffer_info(self) -> tuple[int, int]: ... def byteswap(self) -> None: ... diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 4c7467fc84c8..09c22a19844a 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -140,6 +140,7 @@ class staticmethod(Generic[_P, _R_co]): @property def __isabstractmethod__(self) -> bool: ... def __init__(self, f: Callable[_P, _R_co], /) -> None: ... + def __new__(cls, f: Callable[_P, _R_co], /) -> Self: ... @overload def __get__(self, instance: None, owner: type, /) -> Callable[_P, _R_co]: ... @overload @@ -157,6 +158,7 @@ class classmethod(Generic[_T, _P, _R_co]): @property def __isabstractmethod__(self) -> bool: ... def __init__(self, f: Callable[Concatenate[type[_T], _P], _R_co], /) -> None: ... + def __new__(cls, f: Callable[Concatenate[type[_T], _P], _R_co], /) -> Self: ... @overload def __get__(self, instance: _T, owner: type[_T] | None = None, /) -> Callable[_P, _R_co]: ... @overload @@ -1026,6 +1028,10 @@ class list(MutableSequence[_T]): def __init__(self) -> None: ... @overload def __init__(self, iterable: Iterable[_T], /) -> None: ... + @overload + def __new__(cls) -> Self: ... + @overload + def __new__(cls, iterable: Iterable[_T], /) -> Self: ... def copy(self) -> list[_T]: ... def append(self, object: _T, /) -> None: ... def extend(self, iterable: Iterable[_T], /) -> None: ... @@ -1164,6 +1170,10 @@ class set(MutableSet[_T]): def __init__(self) -> None: ... @overload def __init__(self, iterable: Iterable[_T], /) -> None: ... + @overload + def __new__(cls) -> Self: ... + @overload + def __new__(cls, iterable: Iterable[_T], /) -> Self: ... def add(self, element: _T, /) -> None: ... def copy(self) -> set[_T]: ... def difference(self, *s: Iterable[Any]) -> set[_T]: ... @@ -1275,6 +1285,13 @@ class property: fdel: Callable[[Any], None] | None = ..., doc: str | None = ..., ) -> None: ... + def __new__( + cls, + fget: Callable[[Any], Any] | None = ..., + fset: Callable[[Any, Any], None] | None = ..., + fdel: Callable[[Any], None] | None = ..., + doc: str | None = ..., + ) -> Self: ... def getter(self, fget: Callable[[Any], Any], /) -> property: ... def setter(self, fset: Callable[[Any, Any], None], /) -> property: ... def deleter(self, fdel: Callable[[Any], None], /) -> property: ... @@ -1483,18 +1500,18 @@ def locals() -> dict[str, Any]: ... class map(Generic[_S]): @overload - def __new__(cls, func: Callable[[_T1], _S], iter1: Iterable[_T1], /) -> Self: ... + def __new__(cls, func: Callable[[_T1], _S], iterable: Iterable[_T1], /) -> Self: ... @overload - def __new__(cls, func: Callable[[_T1, _T2], _S], iter1: Iterable[_T1], iter2: Iterable[_T2], /) -> Self: ... + def __new__(cls, func: Callable[[_T1, _T2], _S], iterable: Iterable[_T1], iter2: Iterable[_T2], /) -> Self: ... @overload def __new__( - cls, func: Callable[[_T1, _T2, _T3], _S], iter1: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], / + cls, func: Callable[[_T1, _T2, _T3], _S], iterable: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], / ) -> Self: ... @overload def __new__( cls, func: Callable[[_T1, _T2, _T3, _T4], _S], - iter1: Iterable[_T1], + iterable: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], iter4: Iterable[_T4], @@ -1504,7 +1521,7 @@ class map(Generic[_S]): def __new__( cls, func: Callable[[_T1, _T2, _T3, _T4, _T5], _S], - iter1: Iterable[_T1], + iterable: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], iter4: Iterable[_T4], @@ -1515,7 +1532,7 @@ class map(Generic[_S]): def __new__( cls, func: Callable[..., _S], - iter1: Iterable[Any], + iterable: Iterable[Any], iter2: Iterable[Any], iter3: Iterable[Any], iter4: Iterable[Any], diff --git a/stdlib/datetime.pyi b/stdlib/datetime.pyi index 87037ef39be7..4907bf4607c8 100644 --- a/stdlib/datetime.pyi +++ b/stdlib/datetime.pyi @@ -29,7 +29,7 @@ class timezone(tzinfo): utc: ClassVar[timezone] min: ClassVar[timezone] max: ClassVar[timezone] - def __init__(self, offset: timedelta, name: str = ...) -> None: ... + def __new__(cls, offset: timedelta, name: str = ...) -> Self: ... def tzname(self, dt: datetime | None, /) -> str: ... def utcoffset(self, dt: datetime | None, /) -> timedelta: ... def dst(self, dt: datetime | None, /) -> None: ... diff --git a/stdlib/decimal.pyi b/stdlib/decimal.pyi index 7eb922c8a7ed..bb399f290045 100644 --- a/stdlib/decimal.pyi +++ b/stdlib/decimal.pyi @@ -190,6 +190,17 @@ class Context: flags: None | dict[_TrapType, bool] | Container[_TrapType] = ..., traps: None | dict[_TrapType, bool] | Container[_TrapType] = ..., ) -> None: ... + def __new__( + cls, + prec: int | None = ..., + rounding: str | None = ..., + Emin: int | None = ..., + Emax: int | None = ..., + capitals: int | None = ..., + clamp: int | None = ..., + flags: None | dict[_TrapType, bool] | Container[_TrapType] = ..., + traps: None | dict[_TrapType, bool] | Container[_TrapType] = ..., + ) -> Self: ... def __reduce__(self) -> tuple[type[Self], tuple[Any, ...]]: ... def clear_flags(self) -> None: ... def clear_traps(self) -> None: ... diff --git a/stdlib/ipaddress.pyi b/stdlib/ipaddress.pyi index f5cee43d6b32..0563ed9b00ba 100644 --- a/stdlib/ipaddress.pyi +++ b/stdlib/ipaddress.pyi @@ -32,7 +32,6 @@ class _IPAddressBase: def version(self) -> int: ... class _BaseAddress(_IPAddressBase): - def __init__(self, address: object) -> None: ... def __add__(self, other: int) -> Self: ... def __hash__(self) -> int: ... def __int__(self) -> int: ... @@ -54,7 +53,6 @@ class _BaseAddress(_IPAddressBase): class _BaseNetwork(_IPAddressBase, Generic[_A]): network_address: _A netmask: _A - def __init__(self, address: object, strict: bool = ...) -> None: ... def __contains__(self, other: Any) -> bool: ... def __getitem__(self, n: int) -> _A: ... def __iter__(self) -> Iterator[_A]: ... @@ -114,6 +112,7 @@ class _BaseV4: def max_prefixlen(self) -> Literal[32]: ... class IPv4Address(_BaseV4, _BaseAddress): + def __init__(self, address: object) -> None: ... @property def is_global(self) -> bool: ... @property @@ -134,7 +133,8 @@ class IPv4Address(_BaseV4, _BaseAddress): @property def ipv6_mapped(self) -> IPv6Address: ... -class IPv4Network(_BaseV4, _BaseNetwork[IPv4Address]): ... +class IPv4Network(_BaseV4, _BaseNetwork[IPv4Address]): + def __init__(self, address: object, strict: bool = ...) -> None: ... class IPv4Interface(IPv4Address): netmask: IPv4Address @@ -159,6 +159,7 @@ class _BaseV6: def max_prefixlen(self) -> Literal[128]: ... class IPv6Address(_BaseV6, _BaseAddress): + def __init__(self, address: object) -> None: ... @property def is_global(self) -> bool: ... @property @@ -191,6 +192,7 @@ class IPv6Address(_BaseV6, _BaseAddress): def __eq__(self, other: object) -> bool: ... class IPv6Network(_BaseV6, _BaseNetwork[IPv6Address]): + def __init__(self, address: object, strict: bool = ...) -> None: ... @property def is_site_local(self) -> bool: ... diff --git a/stdlib/itertools.pyi b/stdlib/itertools.pyi index 013c3cba120f..55b0814ac5e0 100644 --- a/stdlib/itertools.pyi +++ b/stdlib/itertools.pyi @@ -40,29 +40,29 @@ class count(Generic[_N]): def __iter__(self) -> Self: ... class cycle(Generic[_T]): - def __init__(self, iterable: Iterable[_T], /) -> None: ... + def __new__(cls, iterable: Iterable[_T], /) -> Self: ... def __next__(self) -> _T: ... def __iter__(self) -> Self: ... class repeat(Generic[_T]): @overload - def __init__(self, object: _T) -> None: ... + def __new__(cls, object: _T) -> Self: ... @overload - def __init__(self, object: _T, times: int) -> None: ... + def __new__(cls, object: _T, times: int) -> Self: ... def __next__(self) -> _T: ... def __iter__(self) -> Self: ... def __length_hint__(self) -> int: ... class accumulate(Generic[_T]): @overload - def __init__(self, iterable: Iterable[_T], func: None = None, *, initial: _T | None = ...) -> None: ... + def __new__(cls, iterable: Iterable[_T], func: None = None, *, initial: _T | None = ...) -> Self: ... @overload - def __init__(self, iterable: Iterable[_S], func: Callable[[_T, _S], _T], *, initial: _T | None = ...) -> None: ... + def __new__(cls, iterable: Iterable[_S], func: Callable[[_T, _S], _T], *, initial: _T | None = ...) -> Self: ... def __iter__(self) -> Self: ... def __next__(self) -> _T: ... class chain(Generic[_T]): - def __init__(self, *iterables: Iterable[_T]) -> None: ... + def __new__(cls, *iterables: Iterable[_T]) -> Self: ... def __next__(self) -> _T: ... def __iter__(self) -> Self: ... @classmethod @@ -72,17 +72,17 @@ class chain(Generic[_T]): def __class_getitem__(cls, item: Any, /) -> GenericAlias: ... class compress(Generic[_T]): - def __init__(self, data: Iterable[_T], selectors: Iterable[Any]) -> None: ... + def __new__(cls, data: Iterable[_T], selectors: Iterable[Any]) -> Self: ... def __iter__(self) -> Self: ... def __next__(self) -> _T: ... class dropwhile(Generic[_T]): - def __init__(self, predicate: _Predicate[_T], iterable: Iterable[_T], /) -> None: ... + def __new__(cls, predicate: _Predicate[_T], iterable: Iterable[_T], /) -> Self: ... def __iter__(self) -> Self: ... def __next__(self) -> _T: ... class filterfalse(Generic[_T]): - def __init__(self, predicate: _Predicate[_T] | None, iterable: Iterable[_T], /) -> None: ... + def __new__(cls, function: _Predicate[_T] | None, iterable: Iterable[_T], /) -> Self: ... def __iter__(self) -> Self: ... def __next__(self) -> _T: ... @@ -96,9 +96,9 @@ class groupby(Generic[_T_co, _S_co]): class islice(Generic[_T]): @overload - def __init__(self, iterable: Iterable[_T], stop: int | None, /) -> None: ... + def __new__(cls, iterable: Iterable[_T], stop: int | None, /) -> Self: ... @overload - def __init__(self, iterable: Iterable[_T], start: int | None, stop: int | None, step: int | None = ..., /) -> None: ... + def __new__(cls, iterable: Iterable[_T], start: int | None, stop: int | None, step: int | None = ..., /) -> Self: ... def __iter__(self) -> Self: ... def __next__(self) -> _T: ... @@ -108,7 +108,7 @@ class starmap(Generic[_T_co]): def __next__(self) -> _T_co: ... class takewhile(Generic[_T]): - def __init__(self, predicate: _Predicate[_T], iterable: Iterable[_T], /) -> None: ... + def __new__(cls, predicate: _Predicate[_T], iterable: Iterable[_T], /) -> Self: ... def __iter__(self) -> Self: ... def __next__(self) -> _T: ... diff --git a/stdlib/mmap.pyi b/stdlib/mmap.pyi index f94e876237d1..c9b8358cde6c 100644 --- a/stdlib/mmap.pyi +++ b/stdlib/mmap.pyi @@ -35,8 +35,8 @@ class mmap: def __init__(self, fileno: int, length: int, tagname: str | None = ..., access: int = ..., offset: int = ...) -> None: ... else: if sys.version_info >= (3, 13): - def __init__( - self, + def __new__( + cls, fileno: int, length: int, flags: int = ..., @@ -45,11 +45,11 @@ class mmap: offset: int = ..., *, trackfd: bool = True, - ) -> None: ... + ) -> Self: ... else: - def __init__( - self, fileno: int, length: int, flags: int = ..., prot: int = ..., access: int = ..., offset: int = ... - ) -> None: ... + def __new__( + cls, fileno: int, length: int, flags: int = ..., prot: int = ..., access: int = ..., offset: int = ... + ) -> Self: ... def close(self) -> None: ... def flush(self, offset: int = ..., size: int = ...) -> None: ... diff --git a/stdlib/operator.pyi b/stdlib/operator.pyi index b73e037f3ed9..bc2b5e026617 100644 --- a/stdlib/operator.pyi +++ b/stdlib/operator.pyi @@ -54,7 +54,7 @@ from _operator import ( ) from _typeshed import SupportsGetItem from typing import Any, Generic, TypeVar, final, overload -from typing_extensions import TypeVarTuple, Unpack +from typing_extensions import Self, TypeVarTuple, Unpack _T = TypeVar("_T") _T_co = TypeVar("_T_co", covariant=True) @@ -211,5 +211,5 @@ class itemgetter(Generic[_T_co]): @final class methodcaller: - def __init__(self, name: str, /, *args: Any, **kwargs: Any) -> None: ... + def __new__(cls, name: str, /, *args: Any, **kwargs: Any) -> Self: ... def __call__(self, obj: Any) -> Any: ... diff --git a/stdlib/pickle.pyi b/stdlib/pickle.pyi index 5e398f2d4921..2d80d61645e0 100644 --- a/stdlib/pickle.pyi +++ b/stdlib/pickle.pyi @@ -15,6 +15,7 @@ from _pickle import ( from _typeshed import ReadableBuffer, SupportsWrite from collections.abc import Callable, Iterable, Mapping from typing import Any, ClassVar, SupportsBytes, SupportsIndex, final +from typing_extensions import Self __all__ = [ "PickleBuffer", @@ -108,7 +109,7 @@ bytes_types: tuple[type[Any], ...] # undocumented @final class PickleBuffer: - def __init__(self, buffer: ReadableBuffer) -> None: ... + def __new__(cls, buffer: ReadableBuffer) -> Self: ... def raw(self) -> memoryview: ... def release(self) -> None: ... def __buffer__(self, flags: int, /) -> memoryview: ... diff --git a/stdlib/sqlite3/__init__.pyi b/stdlib/sqlite3/__init__.pyi index bc0ff6469d5e..b83516b4d4eb 100644 --- a/stdlib/sqlite3/__init__.pyi +++ b/stdlib/sqlite3/__init__.pyi @@ -429,7 +429,7 @@ class PrepareProtocol: def __init__(self, *args: object, **kwargs: object) -> None: ... class Row(Sequence[Any]): - def __init__(self, cursor: Cursor, data: tuple[Any, ...], /) -> None: ... + def __new__(cls, cursor: Cursor, data: tuple[Any, ...], /) -> Self: ... def keys(self) -> list[str]: ... @overload def __getitem__(self, key: int | str, /) -> Any: ... diff --git a/stdlib/types.pyi b/stdlib/types.pyi index b513bd77468a..c7737e79a0a0 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -89,14 +89,26 @@ class FunctionType: __type_params__: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] __module__: str - def __new__( - cls, - code: CodeType, - globals: dict[str, Any], - name: str | None = ..., - argdefs: tuple[object, ...] | None = ..., - closure: tuple[CellType, ...] | None = ..., - ) -> Self: ... + if sys.version_info >= (3, 13): + def __new__( + cls, + code: CodeType, + globals: dict[str, Any], + name: str | None = None, + argdefs: tuple[object, ...] | None = None, + closure: tuple[CellType, ...] | None = None, + kwdefaults: dict[str, object] | None = None, + ) -> Self: ... + else: + def __new__( + cls, + code: CodeType, + globals: dict[str, Any], + name: str | None = None, + argdefs: tuple[object, ...] | None = None, + closure: tuple[CellType, ...] | None = None, + ) -> Self: ... + def __call__(self, *args: Any, **kwargs: Any) -> Any: ... @overload def __get__(self, instance: None, owner: type, /) -> FunctionType: ... @@ -318,8 +330,10 @@ class SimpleNamespace: __hash__: ClassVar[None] # type: ignore[assignment] if sys.version_info >= (3, 13): def __init__(self, mapping_or_iterable: Mapping[str, Any] | Iterable[tuple[str, Any]] = (), /, **kwargs: Any) -> None: ... + def __new__(cls, mapping_or_iterable: Mapping[str, Any] | Iterable[tuple[str, Any]] = (), /, **kwargs: Any) -> Self: ... else: def __init__(self, **kwargs: Any) -> None: ... + def __new__(cls, **kwargs: Any) -> Self: ... def __eq__(self, value: object, /) -> bool: ... def __getattribute__(self, name: str, /) -> Any: ... @@ -345,6 +359,7 @@ class ModuleType: # as an implicit global in all modules, similar to `__name__`, `__file__`, `__spec__`, etc. __doc__: str | None def __init__(self, name: str, doc: str | None = ...) -> None: ... + def __new__(cls, name: str, doc: str | None = ...) -> Self: ... # __getattr__ doesn't exist at runtime, # but having it here in typeshed makes dynamic imports # using `builtins.__import__` or `importlib.import_module` less painful @@ -442,7 +457,7 @@ class MethodType: def __name__(self) -> str: ... # inherited from the added function @property def __qualname__(self) -> str: ... # inherited from the added function - def __new__(cls, func: Callable[..., Any], obj: object, /) -> Self: ... + def __new__(cls, func: Callable[..., Any], instance: object, /) -> Self: ... def __call__(self, *args: Any, **kwargs: Any) -> Any: ... def __eq__(self, value: object, /) -> bool: ... def __hash__(self) -> int: ... @@ -604,7 +619,7 @@ if sys.version_info >= (3, 9): def __args__(self) -> tuple[Any, ...]: ... @property def __parameters__(self) -> tuple[Any, ...]: ... - def __new__(cls, origin: type, args: Any) -> Self: ... + def __new__(cls, origin: type, args: Any, /) -> Self: ... def __getitem__(self, typeargs: Any, /) -> GenericAlias: ... def __eq__(self, value: object, /) -> bool: ... def __hash__(self) -> int: ... diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 741e7b8a3167..8e1834a43c0b 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -155,8 +155,8 @@ class TypeVar: @property def __default__(self) -> Any: ... if sys.version_info >= (3, 13): - def __init__( - self, + def __new__( + cls, name: str, *constraints: Any, bound: Any | None = None, @@ -164,21 +164,26 @@ class TypeVar: covariant: bool = False, infer_variance: bool = False, default: Any = ..., - ) -> None: ... + ) -> Self: ... elif sys.version_info >= (3, 12): - def __init__( - self, + def __new__( + cls, name: str, *constraints: Any, bound: Any | None = None, covariant: bool = False, contravariant: bool = False, infer_variance: bool = False, - ) -> None: ... + ) -> Self: ... + elif sys.version_info >= (3, 11): + def __new__( + cls, name: str, *constraints: Any, bound: Any | None = None, covariant: bool = False, contravariant: bool = False + ) -> Self: ... else: def __init__( self, name: str, *constraints: Any, bound: Any | None = None, covariant: bool = False, contravariant: bool = False ) -> None: ... + if sys.version_info >= (3, 10): def __or__(self, right: Any) -> _SpecialForm: ... def __ror__(self, left: Any) -> _SpecialForm: ... @@ -232,7 +237,9 @@ if sys.version_info >= (3, 11): def __default__(self) -> Any: ... def has_default(self) -> bool: ... if sys.version_info >= (3, 13): - def __init__(self, name: str, *, default: Any = ...) -> None: ... + def __new__(cls, name: str, *, default: Any = ...) -> Self: ... + elif sys.version_info >= (3, 12): + def __new__(cls, name: str) -> Self: ... else: def __init__(self, name: str) -> None: ... @@ -245,14 +252,22 @@ if sys.version_info >= (3, 10): class ParamSpecArgs: @property def __origin__(self) -> ParamSpec: ... - def __init__(self, origin: ParamSpec) -> None: ... + if sys.version_info >= (3, 12): + def __new__(cls, origin: ParamSpec) -> Self: ... + else: + def __init__(self, origin: ParamSpec) -> None: ... + def __eq__(self, other: object) -> bool: ... @final class ParamSpecKwargs: @property def __origin__(self) -> ParamSpec: ... - def __init__(self, origin: ParamSpec) -> None: ... + if sys.version_info >= (3, 12): + def __new__(cls, origin: ParamSpec) -> Self: ... + else: + def __init__(self, origin: ParamSpec) -> None: ... + def __eq__(self, other: object) -> bool: ... @final @@ -272,8 +287,8 @@ if sys.version_info >= (3, 10): @property def __default__(self) -> Any: ... if sys.version_info >= (3, 13): - def __init__( - self, + def __new__( + cls, name: str, *, bound: Any | None = None, @@ -281,17 +296,21 @@ if sys.version_info >= (3, 10): covariant: bool = False, infer_variance: bool = False, default: Any = ..., - ) -> None: ... + ) -> Self: ... elif sys.version_info >= (3, 12): - def __init__( - self, + def __new__( + cls, name: str, *, bound: Any | None = None, contravariant: bool = False, covariant: bool = False, infer_variance: bool = False, - ) -> None: ... + ) -> Self: ... + elif sys.version_info >= (3, 11): + def __new__( + cls, name: str, *, bound: Any | None = None, contravariant: bool = False, covariant: bool = False + ) -> Self: ... else: def __init__( self, name: str, *, bound: Any | None = None, contravariant: bool = False, covariant: bool = False @@ -925,12 +944,12 @@ class NamedTuple(tuple[Any, ...]): __orig_bases__: ClassVar[tuple[Any, ...]] @overload - def __init__(self, typename: str, fields: Iterable[tuple[str, Any]], /) -> None: ... + def __new__(cls, typename: str, fields: Iterable[tuple[str, Any]], /) -> Self: ... @overload @typing_extensions.deprecated( "Creating a typing.NamedTuple using keyword arguments is deprecated and support will be removed in Python 3.15" ) - def __init__(self, typename: str, fields: None = None, /, **kwargs: Any) -> None: ... + def __new__(cls, typename: str, fields: None = None, /, **kwargs: Any) -> Self: ... @classmethod def _make(cls, iterable: Iterable[Any]) -> typing_extensions.Self: ... def _asdict(self) -> dict[str, Any]: ... @@ -1039,9 +1058,7 @@ if sys.version_info >= (3, 12): def override(method: _F, /) -> _F: ... @final class TypeAliasType: - def __init__( - self, name: str, value: Any, *, type_params: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] = () - ) -> None: ... + def __new__(cls, name: str, value: Any, *, type_params: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] = ()) -> Self: ... @property def __value__(self) -> Any: ... @property diff --git a/stdlib/zoneinfo/__init__.pyi b/stdlib/zoneinfo/__init__.pyi index cc483afad9ff..fb21b00c45dc 100644 --- a/stdlib/zoneinfo/__init__.pyi +++ b/stdlib/zoneinfo/__init__.pyi @@ -21,7 +21,7 @@ if sys.version_info >= (3, 9): class ZoneInfo(tzinfo): @property def key(self) -> str: ... - def __init__(self, key: str) -> None: ... + def __new__(cls, key: str) -> Self: ... @classmethod def no_cache(cls, key: str) -> Self: ... @classmethod From c74293777436e27817736315e9756b5dd8f090cb Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Sat, 7 Dec 2024 04:02:11 -0800 Subject: [PATCH 3/6] reduce duplication --- stdlib/@tests/stubtest_allowlists/py38.txt | 1 - stdlib/_asyncio.pyi | 21 --------------- stdlib/_io.pyi | 30 ---------------------- stdlib/_pickle.pyi | 18 +------------ stdlib/_random.pyi | 3 +-- stdlib/builtins.pyi | 12 --------- stdlib/decimal.pyi | 11 -------- 7 files changed, 2 insertions(+), 94 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/py38.txt b/stdlib/@tests/stubtest_allowlists/py38.txt index 6d98ee251773..26494f6f4b6a 100644 --- a/stdlib/@tests/stubtest_allowlists/py38.txt +++ b/stdlib/@tests/stubtest_allowlists/py38.txt @@ -16,7 +16,6 @@ builtins.float.__set_format__ # Internal method for CPython test suite dummy_threading.Condition.acquire dummy_threading.Condition.release dummy_threading.Thread.native_id -typing.NamedTuple.__new__ typing.NamedTuple._asdict typing.NamedTuple._make typing.NamedTuple._replace diff --git a/stdlib/_asyncio.pyi b/stdlib/_asyncio.pyi index 62e64ed14f44..89cdff6cc283 100644 --- a/stdlib/_asyncio.pyi +++ b/stdlib/_asyncio.pyi @@ -24,7 +24,6 @@ class Future(Awaitable[_T]): def _log_traceback(self, val: Literal[False]) -> None: ... _asyncio_future_blocking: bool # is a part of duck-typing contract for `Future` def __init__(self, *, loop: AbstractEventLoop | None = ...) -> None: ... - def __new__(cls, *, loop: AbstractEventLoop | None = ...) -> Self: ... def __del__(self) -> None: ... def get_loop(self) -> AbstractEventLoop: ... @property @@ -71,15 +70,6 @@ class Task(Future[_T_co]): # type: ignore[type-var] # pyright: ignore[reportIn context: Context | None = None, eager_start: bool = False, ) -> None: ... - def __new__( - cls, - coro: _TaskCompatibleCoro[_T_co], - *, - loop: AbstractEventLoop | None = None, - name: str | None = ..., - context: Context | None = None, - eager_start: bool = False, - ) -> Self: ... elif sys.version_info >= (3, 11): def __init__( self, @@ -89,21 +79,10 @@ class Task(Future[_T_co]): # type: ignore[type-var] # pyright: ignore[reportIn name: str | None = ..., context: Context | None = None, ) -> None: ... - def __new__( - cls, - coro: _TaskCompatibleCoro[_T_co], - *, - loop: AbstractEventLoop | None = None, - name: str | None = ..., - context: Context | None = None, - ) -> Self: ... else: def __init__( self, coro: _TaskCompatibleCoro[_T_co], *, loop: AbstractEventLoop | None = None, name: str | None = ... ) -> None: ... - def __new__( - cls, coro: _TaskCompatibleCoro[_T_co], *, loop: AbstractEventLoop | None = None, name: str | None = ... - ) -> Self: ... if sys.version_info >= (3, 12): def get_coro(self) -> _TaskCompatibleCoro[_T_co] | None: ... diff --git a/stdlib/_io.pyi b/stdlib/_io.pyi index 4f9dc9007e80..54efd3199760 100644 --- a/stdlib/_io.pyi +++ b/stdlib/_io.pyi @@ -71,9 +71,6 @@ class FileIO(RawIOBase, _RawIOBase, BinaryIO): # type: ignore[misc] # incompat def __init__( self, file: FileDescriptorOrPath, mode: str = "r", closefd: bool = True, opener: _Opener | None = None ) -> None: ... - def __new__( - cls, file: FileDescriptorOrPath, mode: str = "r", closefd: bool = True, opener: _Opener | None = None - ) -> Self: ... @property def closefd(self) -> bool: ... def seek(self, pos: int, whence: int = 0, /) -> int: ... @@ -81,7 +78,6 @@ class FileIO(RawIOBase, _RawIOBase, BinaryIO): # type: ignore[misc] # incompat class BytesIO(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of methods in the base classes def __init__(self, initial_bytes: ReadableBuffer = b"") -> None: ... - def __new__(cls, initial_bytes: ReadableBuffer = b"") -> Self: ... # BytesIO does not contain a "name" field. This workaround is necessary # to allow BytesIO sub-classes to add this field, as it is defined # as a read-only property on IO[]. @@ -95,9 +91,6 @@ class BytesIO(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore[misc] class BufferedReader(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of methods in the base classes raw: RawIOBase def __init__(self, raw: RawIOBase, buffer_size: int = 8192) -> None: ... - if sys.version_info < (3, 12): - def __new__(cls, raw: RawIOBase, buffer_size: int = 8192) -> Self: ... - def peek(self, size: int = 0, /) -> bytes: ... def seek(self, target: int, whence: int = 0, /) -> int: ... def truncate(self, pos: int | None = None, /) -> int: ... @@ -105,9 +98,6 @@ class BufferedReader(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore class BufferedWriter(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of writelines in the base classes raw: RawIOBase def __init__(self, raw: RawIOBase, buffer_size: int = 8192) -> None: ... - if sys.version_info < (3, 12): - def __new__(cls, raw: RawIOBase, buffer_size: int = 8192) -> Self: ... - def write(self, buffer: ReadableBuffer, /) -> int: ... def seek(self, target: int, whence: int = 0, /) -> int: ... def truncate(self, pos: int | None = None, /) -> int: ... @@ -117,18 +107,12 @@ class BufferedRandom(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore name: Any raw: RawIOBase def __init__(self, raw: RawIOBase, buffer_size: int = 8192) -> None: ... - if sys.version_info < (3, 12): - def __new__(cls, raw: RawIOBase, buffer_size: int = 8192) -> Self: ... - def seek(self, target: int, whence: int = 0, /) -> int: ... # stubtest needs this def peek(self, size: int = 0, /) -> bytes: ... def truncate(self, pos: int | None = None, /) -> int: ... class BufferedRWPair(BufferedIOBase, _BufferedIOBase): def __init__(self, reader: RawIOBase, writer: RawIOBase, buffer_size: int = 8192, /) -> None: ... - if sys.version_info < (3, 12): - def __new__(cls, reader: RawIOBase, writer: RawIOBase, buffer_size: int = 8192, /) -> Self: ... - def peek(self, size: int = 0, /) -> bytes: ... class _TextIOBase(_IOBase): @@ -180,16 +164,6 @@ class TextIOWrapper(TextIOBase, _TextIOBase, TextIO, Generic[_BufferT_co]): # t line_buffering: bool = False, write_through: bool = False, ) -> None: ... - if sys.version_info < (3, 12): - def __new__( - cls, - buffer: _BufferT_co, - encoding: str | None = None, - errors: str | None = None, - newline: str | None = None, - line_buffering: bool = False, - write_through: bool = False, - ) -> Self: ... # Equals the "buffer" argument passed in to the constructor. @property def buffer(self) -> _BufferT_co: ... # type: ignore[override] @@ -216,7 +190,6 @@ class TextIOWrapper(TextIOBase, _TextIOBase, TextIO, Generic[_BufferT_co]): # t class StringIO(TextIOBase, _TextIOBase, TextIO): # type: ignore[misc] # incompatible definitions of write in the base classes def __init__(self, initial_value: str | None = "", newline: str | None = "\n") -> None: ... - def __new__(cls, initial_value: str | None = "", newline: str | None = "\n") -> Self: ... # StringIO does not contain a "name" field. This workaround is necessary # to allow StringIO sub-classes to add this field, as it is defined # as a read-only property on IO[]. @@ -229,9 +202,6 @@ class StringIO(TextIOBase, _TextIOBase, TextIO): # type: ignore[misc] # incomp class IncrementalNewlineDecoder: def __init__(self, decoder: codecs.IncrementalDecoder | None, translate: bool, errors: str = "strict") -> None: ... - if sys.version_info < (3, 12): - def __new__(cls, decoder: codecs.IncrementalDecoder | None, translate: bool, errors: str = "strict") -> Self: ... - def decode(self, input: ReadableBuffer | str, final: bool = False) -> str: ... @property def newlines(self) -> str | tuple[str, ...] | None: ... diff --git a/stdlib/_pickle.pyi b/stdlib/_pickle.pyi index 313e3eba2ea7..50bbb6bc16cd 100644 --- a/stdlib/_pickle.pyi +++ b/stdlib/_pickle.pyi @@ -3,7 +3,7 @@ from _typeshed import ReadableBuffer, SupportsWrite from collections.abc import Callable, Iterable, Iterator, Mapping from pickle import PickleBuffer as PickleBuffer from typing import Any, Protocol, type_check_only -from typing_extensions import Self, TypeAlias +from typing_extensions import TypeAlias class _ReadableFileobj(Protocol): def read(self, n: int, /) -> bytes: ... @@ -69,13 +69,6 @@ class Pickler: fix_imports: bool = True, buffer_callback: _BufferCallback = None, ) -> None: ... - def __new__( - cls, - file: SupportsWrite[bytes], - protocol: int | None = None, - fix_imports: bool = True, - buffer_callback: _BufferCallback = None, - ) -> Self: ... @property def memo(self) -> PicklerMemoProxy: ... @memo.setter @@ -102,15 +95,6 @@ class Unpickler: errors: str = "strict", buffers: Iterable[Any] | None = (), ) -> None: ... - def __new__( - cls, - file: _ReadableFileobj, - *, - fix_imports: bool = True, - encoding: str = "ASCII", - errors: str = "strict", - buffers: Iterable[Any] | None = (), - ) -> Self: ... @property def memo(self) -> UnpicklerMemoProxy: ... @memo.setter diff --git a/stdlib/_random.pyi b/stdlib/_random.pyi index c2deec7df6ce..0c3d61efb36f 100644 --- a/stdlib/_random.pyi +++ b/stdlib/_random.pyi @@ -1,5 +1,5 @@ import sys -from typing_extensions import Self, TypeAlias +from typing_extensions import TypeAlias # Actually Tuple[(int,) * 625] _State: TypeAlias = tuple[int, ...] @@ -8,7 +8,6 @@ class Random: if sys.version_info >= (3, 11): def __init__(self, seed: object = ...) -> None: ... - def __new__(cls, seed: object = ...) -> Self: ... def seed(self, n: object = None, /) -> None: ... def getstate(self) -> _State: ... def setstate(self, state: _State, /) -> None: ... diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 09c22a19844a..dfb0d8559500 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -158,7 +158,6 @@ class classmethod(Generic[_T, _P, _R_co]): @property def __isabstractmethod__(self) -> bool: ... def __init__(self, f: Callable[Concatenate[type[_T], _P], _R_co], /) -> None: ... - def __new__(cls, f: Callable[Concatenate[type[_T], _P], _R_co], /) -> Self: ... @overload def __get__(self, instance: _T, owner: type[_T] | None = None, /) -> Callable[_P, _R_co]: ... @overload @@ -1028,10 +1027,6 @@ class list(MutableSequence[_T]): def __init__(self) -> None: ... @overload def __init__(self, iterable: Iterable[_T], /) -> None: ... - @overload - def __new__(cls) -> Self: ... - @overload - def __new__(cls, iterable: Iterable[_T], /) -> Self: ... def copy(self) -> list[_T]: ... def append(self, object: _T, /) -> None: ... def extend(self, iterable: Iterable[_T], /) -> None: ... @@ -1285,13 +1280,6 @@ class property: fdel: Callable[[Any], None] | None = ..., doc: str | None = ..., ) -> None: ... - def __new__( - cls, - fget: Callable[[Any], Any] | None = ..., - fset: Callable[[Any, Any], None] | None = ..., - fdel: Callable[[Any], None] | None = ..., - doc: str | None = ..., - ) -> Self: ... def getter(self, fget: Callable[[Any], Any], /) -> property: ... def setter(self, fset: Callable[[Any, Any], None], /) -> property: ... def deleter(self, fdel: Callable[[Any], None], /) -> property: ... diff --git a/stdlib/decimal.pyi b/stdlib/decimal.pyi index bb399f290045..7eb922c8a7ed 100644 --- a/stdlib/decimal.pyi +++ b/stdlib/decimal.pyi @@ -190,17 +190,6 @@ class Context: flags: None | dict[_TrapType, bool] | Container[_TrapType] = ..., traps: None | dict[_TrapType, bool] | Container[_TrapType] = ..., ) -> None: ... - def __new__( - cls, - prec: int | None = ..., - rounding: str | None = ..., - Emin: int | None = ..., - Emax: int | None = ..., - capitals: int | None = ..., - clamp: int | None = ..., - flags: None | dict[_TrapType, bool] | Container[_TrapType] = ..., - traps: None | dict[_TrapType, bool] | Container[_TrapType] = ..., - ) -> Self: ... def __reduce__(self) -> tuple[type[Self], tuple[Any, ...]]: ... def clear_flags(self) -> None: ... def clear_traps(self) -> None: ... From 570c7cab65a9cbe27f8a2de26d38003cda001c05 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Sat, 7 Dec 2024 04:06:41 -0800 Subject: [PATCH 4/6] reduce duplication --- stdlib/builtins.pyi | 5 ----- stdlib/types.pyi | 3 --- stdlib/typing.pyi | 1 - 3 files changed, 9 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index dfb0d8559500..bd43ecb98ecd 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -140,7 +140,6 @@ class staticmethod(Generic[_P, _R_co]): @property def __isabstractmethod__(self) -> bool: ... def __init__(self, f: Callable[_P, _R_co], /) -> None: ... - def __new__(cls, f: Callable[_P, _R_co], /) -> Self: ... @overload def __get__(self, instance: None, owner: type, /) -> Callable[_P, _R_co]: ... @overload @@ -1165,10 +1164,6 @@ class set(MutableSet[_T]): def __init__(self) -> None: ... @overload def __init__(self, iterable: Iterable[_T], /) -> None: ... - @overload - def __new__(cls) -> Self: ... - @overload - def __new__(cls, iterable: Iterable[_T], /) -> Self: ... def add(self, element: _T, /) -> None: ... def copy(self) -> set[_T]: ... def difference(self, *s: Iterable[Any]) -> set[_T]: ... diff --git a/stdlib/types.pyi b/stdlib/types.pyi index c7737e79a0a0..fdd484c66b4f 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -330,10 +330,8 @@ class SimpleNamespace: __hash__: ClassVar[None] # type: ignore[assignment] if sys.version_info >= (3, 13): def __init__(self, mapping_or_iterable: Mapping[str, Any] | Iterable[tuple[str, Any]] = (), /, **kwargs: Any) -> None: ... - def __new__(cls, mapping_or_iterable: Mapping[str, Any] | Iterable[tuple[str, Any]] = (), /, **kwargs: Any) -> Self: ... else: def __init__(self, **kwargs: Any) -> None: ... - def __new__(cls, **kwargs: Any) -> Self: ... def __eq__(self, value: object, /) -> bool: ... def __getattribute__(self, name: str, /) -> Any: ... @@ -359,7 +357,6 @@ class ModuleType: # as an implicit global in all modules, similar to `__name__`, `__file__`, `__spec__`, etc. __doc__: str | None def __init__(self, name: str, doc: str | None = ...) -> None: ... - def __new__(cls, name: str, doc: str | None = ...) -> Self: ... # __getattr__ doesn't exist at runtime, # but having it here in typeshed makes dynamic imports # using `builtins.__import__` or `importlib.import_module` less painful diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 8e1834a43c0b..4527b28b0e47 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -183,7 +183,6 @@ class TypeVar: def __init__( self, name: str, *constraints: Any, bound: Any | None = None, covariant: bool = False, contravariant: bool = False ) -> None: ... - if sys.version_info >= (3, 10): def __or__(self, right: Any) -> _SpecialForm: ... def __ror__(self, left: Any) -> _SpecialForm: ... From 820b653071fb8438cef9b3c6a70046c2a269b90b Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Sat, 7 Dec 2024 04:20:42 -0800 Subject: [PATCH 5/6] cleanup --- stdlib/@tests/stubtest_allowlists/py38.txt | 1 + stdlib/_bz2.pyi | 7 +------ stdlib/_lzma.pyi | 19 +++++-------------- stdlib/_random.pyi | 5 +---- stdlib/_struct.pyi | 5 ----- stdlib/typing.pyi | 4 ++-- 6 files changed, 10 insertions(+), 31 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/py38.txt b/stdlib/@tests/stubtest_allowlists/py38.txt index 26494f6f4b6a..6d98ee251773 100644 --- a/stdlib/@tests/stubtest_allowlists/py38.txt +++ b/stdlib/@tests/stubtest_allowlists/py38.txt @@ -16,6 +16,7 @@ builtins.float.__set_format__ # Internal method for CPython test suite dummy_threading.Condition.acquire dummy_threading.Condition.release dummy_threading.Thread.native_id +typing.NamedTuple.__new__ typing.NamedTuple._asdict typing.NamedTuple._make typing.NamedTuple._replace diff --git a/stdlib/_bz2.pyi b/stdlib/_bz2.pyi index 87e9ddfce633..d5eeeb6494c9 100644 --- a/stdlib/_bz2.pyi +++ b/stdlib/_bz2.pyi @@ -1,14 +1,9 @@ -import sys from _typeshed import ReadableBuffer from typing import final -from typing_extensions import Self @final class BZ2Compressor: - if sys.version_info < (3, 12): - def __init__(self, compresslevel: int = 9, /) -> None: ... - - def __new__(cls, compresslevel: int = 9, /) -> Self: ... + def __init__(self, compresslevel: int = 9, /) -> None: ... def compress(self, data: ReadableBuffer, /) -> bytes: ... def flush(self) -> bytes: ... diff --git a/stdlib/_lzma.pyi b/stdlib/_lzma.pyi index 6b2d48d57c58..1f5be78876c6 100644 --- a/stdlib/_lzma.pyi +++ b/stdlib/_lzma.pyi @@ -1,8 +1,7 @@ -import sys from _typeshed import ReadableBuffer from collections.abc import Mapping, Sequence from typing import Any, Final, final -from typing_extensions import Self, TypeAlias +from typing_extensions import TypeAlias _FilterChain: TypeAlias = Sequence[Mapping[str, Any]] @@ -37,10 +36,7 @@ PRESET_EXTREME: int # v big number @final class LZMADecompressor: - if sys.version_info < (3, 12): - def __init__(self, format: int | None = ..., memlimit: int | None = ..., filters: _FilterChain | None = ...) -> None: ... - - def __new__(cls, format: int | None = ..., memlimit: int | None = ..., filters: _FilterChain | None = ...) -> Self: ... + def __init__(self, format: int | None = ..., memlimit: int | None = ..., filters: _FilterChain | None = ...) -> None: ... def decompress(self, data: ReadableBuffer, max_length: int = -1) -> bytes: ... @property def check(self) -> int: ... @@ -53,14 +49,9 @@ class LZMADecompressor: @final class LZMACompressor: - if sys.version_info < (3, 12): - def __init__( - self, format: int | None = ..., check: int = ..., preset: int | None = ..., filters: _FilterChain | None = ... - ) -> None: ... - - def __new__( - cls, format: int | None = ..., check: int = ..., preset: int | None = ..., filters: _FilterChain | None = ... - ) -> Self: ... + def __init__( + self, format: int | None = ..., check: int = ..., preset: int | None = ..., filters: _FilterChain | None = ... + ) -> None: ... def compress(self, data: ReadableBuffer, /) -> bytes: ... def flush(self) -> bytes: ... diff --git a/stdlib/_random.pyi b/stdlib/_random.pyi index 0c3d61efb36f..4082344ade8e 100644 --- a/stdlib/_random.pyi +++ b/stdlib/_random.pyi @@ -1,13 +1,10 @@ -import sys from typing_extensions import TypeAlias # Actually Tuple[(int,) * 625] _State: TypeAlias = tuple[int, ...] class Random: - if sys.version_info >= (3, 11): - def __init__(self, seed: object = ...) -> None: ... - + def __init__(self, seed: object = ...) -> None: ... def seed(self, n: object = None, /) -> None: ... def getstate(self) -> _State: ... def setstate(self, state: _State, /) -> None: ... diff --git a/stdlib/_struct.pyi b/stdlib/_struct.pyi index 6249e3e502eb..662170e869f3 100644 --- a/stdlib/_struct.pyi +++ b/stdlib/_struct.pyi @@ -1,8 +1,6 @@ -import sys from _typeshed import ReadableBuffer, WriteableBuffer from collections.abc import Iterator from typing import Any -from typing_extensions import Self def pack(fmt: str | bytes, /, *v: Any) -> bytes: ... def pack_into(fmt: str | bytes, buffer: WriteableBuffer, offset: int, /, *v: Any) -> None: ... @@ -17,9 +15,6 @@ class Struct: @property def size(self) -> int: ... def __init__(self, format: str | bytes) -> None: ... - if sys.version_info < (3, 9): - def __new__(cls, format: str | bytes) -> Self: ... - def pack(self, *v: Any) -> bytes: ... def pack_into(self, buffer: WriteableBuffer, offset: int, *v: Any) -> None: ... def unpack(self, buffer: ReadableBuffer, /) -> tuple[Any, ...]: ... diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 4527b28b0e47..eb8ac16941a6 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -943,12 +943,12 @@ class NamedTuple(tuple[Any, ...]): __orig_bases__: ClassVar[tuple[Any, ...]] @overload - def __new__(cls, typename: str, fields: Iterable[tuple[str, Any]], /) -> Self: ... + def __init__(self, typename: str, fields: Iterable[tuple[str, Any]], /) -> None: ... @overload @typing_extensions.deprecated( "Creating a typing.NamedTuple using keyword arguments is deprecated and support will be removed in Python 3.15" ) - def __new__(cls, typename: str, fields: None = None, /, **kwargs: Any) -> Self: ... + def __init__(self, typename: str, fields: None = None, /, **kwargs: Any) -> None: ... @classmethod def _make(cls, iterable: Iterable[Any]) -> typing_extensions.Self: ... def _asdict(self) -> dict[str, Any]: ... From 2db605c2da57b409afa85e94d8ad20664b0ac876 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Sat, 7 Dec 2024 04:35:05 -0800 Subject: [PATCH 6/6] these flipped from __init__ to __new__ --- stdlib/_bz2.pyi | 8 +++++++- stdlib/_lzma.pyi | 21 ++++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/stdlib/_bz2.pyi b/stdlib/_bz2.pyi index d5eeeb6494c9..fdad932ca22e 100644 --- a/stdlib/_bz2.pyi +++ b/stdlib/_bz2.pyi @@ -1,9 +1,15 @@ +import sys from _typeshed import ReadableBuffer from typing import final +from typing_extensions import Self @final class BZ2Compressor: - def __init__(self, compresslevel: int = 9, /) -> None: ... + if sys.version_info >= (3, 12): + def __new__(cls, compresslevel: int = 9, /) -> Self: ... + else: + def __init__(self, compresslevel: int = 9, /) -> None: ... + def compress(self, data: ReadableBuffer, /) -> bytes: ... def flush(self) -> bytes: ... diff --git a/stdlib/_lzma.pyi b/stdlib/_lzma.pyi index 1f5be78876c6..1a27c7428e8e 100644 --- a/stdlib/_lzma.pyi +++ b/stdlib/_lzma.pyi @@ -1,7 +1,8 @@ +import sys from _typeshed import ReadableBuffer from collections.abc import Mapping, Sequence from typing import Any, Final, final -from typing_extensions import TypeAlias +from typing_extensions import Self, TypeAlias _FilterChain: TypeAlias = Sequence[Mapping[str, Any]] @@ -36,7 +37,11 @@ PRESET_EXTREME: int # v big number @final class LZMADecompressor: - def __init__(self, format: int | None = ..., memlimit: int | None = ..., filters: _FilterChain | None = ...) -> None: ... + if sys.version_info >= (3, 12): + def __new__(cls, format: int | None = ..., memlimit: int | None = ..., filters: _FilterChain | None = ...) -> Self: ... + else: + def __init__(self, format: int | None = ..., memlimit: int | None = ..., filters: _FilterChain | None = ...) -> None: ... + def decompress(self, data: ReadableBuffer, max_length: int = -1) -> bytes: ... @property def check(self) -> int: ... @@ -49,9 +54,15 @@ class LZMADecompressor: @final class LZMACompressor: - def __init__( - self, format: int | None = ..., check: int = ..., preset: int | None = ..., filters: _FilterChain | None = ... - ) -> None: ... + if sys.version_info >= (3, 12): + def __new__( + cls, format: int | None = ..., check: int = ..., preset: int | None = ..., filters: _FilterChain | None = ... + ) -> Self: ... + else: + def __init__( + self, format: int | None = ..., check: int = ..., preset: int | None = ..., filters: _FilterChain | None = ... + ) -> None: ... + def compress(self, data: ReadableBuffer, /) -> bytes: ... def flush(self) -> bytes: ...