From 1e27a56b097679da94a27314116717b6a338f018 Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Tue, 2 Jul 2024 20:06:10 -0500 Subject: [PATCH 1/6] Add missing replace dunders --- stdlib/@tests/stubtest_allowlists/py313.txt | 2 -- stdlib/types.pyi | 31 +++++++++++++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/py313.txt b/stdlib/@tests/stubtest_allowlists/py313.txt index f877583d39e3..c520a2f89636 100644 --- a/stdlib/@tests/stubtest_allowlists/py313.txt +++ b/stdlib/@tests/stubtest_allowlists/py313.txt @@ -159,9 +159,7 @@ trace.CoverageResults.write_results turtle.RawTurtle.settiltangle turtle.__all__ turtle.settiltangle -types.CodeType.__replace__ types.MappingProxyType.get -types.SimpleNamespace.__replace__ unittest.IsolatedAsyncioTestCase.loop_factory unittest.TestProgram.usageExit unittest.__all__ diff --git a/stdlib/types.pyi b/stdlib/types.pyi index 9e9dc56b8529..b65beb3a753f 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -245,7 +245,7 @@ class CodeType: co_qualname: str = ..., co_linetable: bytes = ..., co_exceptiontable: bytes = ..., - ) -> CodeType: ... + ) -> Self: ... elif sys.version_info >= (3, 10): def replace( self, @@ -266,7 +266,7 @@ class CodeType: co_filename: str = ..., co_name: str = ..., co_linetable: bytes = ..., - ) -> CodeType: ... + ) -> Self: ... else: def replace( self, @@ -287,7 +287,30 @@ class CodeType: co_filename: str = ..., co_name: str = ..., co_lnotab: bytes = ..., - ) -> CodeType: ... + ) -> Self: ... + + if sys.version_info >= (3, 13): + def __replace__( + *, + co_argcount: int = -1, + co_posonlyargcount: int = -1, + co_kwonlyargcount: int = -1, + co_nlocals: int = -1, + co_stacksize: int = -1, + co_flags: int = -1, + co_firstlineno: int = -1, + co_code: bytes = ..., + co_consts: tuple[object, ...] = ..., + co_names: tuple[str, ...] = ..., + co_varnames: tuple[str, ...] = ..., + co_freevars: tuple[str, ...] = ..., + co_cellvars: tuple[str, ...] = ..., + co_filename: str = ..., + co_name: str = ..., + co_qualname: str = ..., + co_linetable: bytes = ..., + co_exceptiontable: bytes = ..., + ) -> Self: ... @final class MappingProxyType(Mapping[_KT, _VT_co]): @@ -314,6 +337,8 @@ class SimpleNamespace: def __getattribute__(self, name: str, /) -> Any: ... def __setattr__(self, name: str, value: Any, /) -> None: ... def __delattr__(self, name: str, /) -> None: ... + if sys.version_info >= (3, 13): + def __replace__(self, **kwargs: Any) -> Self: ... class ModuleType: __name__: str From 0fcf5f6bc3e38d137fd4a5e09759724032d43f2f Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Tue, 2 Jul 2024 21:07:22 -0500 Subject: [PATCH 2/6] Fix --- stdlib/types.pyi | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stdlib/types.pyi b/stdlib/types.pyi index b65beb3a753f..bb90e32e45fc 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -290,7 +290,10 @@ class CodeType: ) -> Self: ... if sys.version_info >= (3, 13): + # Same as replace, but positional for `self`. def __replace__( + self, + /, *, co_argcount: int = -1, co_posonlyargcount: int = -1, From 1d4b3414575effe3c0a8e4b133b17a79f36adf21 Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Tue, 2 Jul 2024 21:09:28 -0500 Subject: [PATCH 3/6] Add comment --- stdlib/@tests/stubtest_allowlists/py313.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/@tests/stubtest_allowlists/py313.txt b/stdlib/@tests/stubtest_allowlists/py313.txt index c520a2f89636..87d800b55ad5 100644 --- a/stdlib/@tests/stubtest_allowlists/py313.txt +++ b/stdlib/@tests/stubtest_allowlists/py313.txt @@ -116,6 +116,7 @@ pdb.Pdb.do_exceptions pdb.Pdb.interaction pdb.Pdb.message pdb.Pdb.user_opcode +# `__replace__` to be special cased in dataclasses pstats.FunctionProfile.__replace__ pstats.StatsProfile.__replace__ pydoc.pager From 5bea94260f9a56d124e8cbcd989fb8721d9f5827 Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Tue, 2 Jul 2024 21:12:13 -0500 Subject: [PATCH 4/6] Add to dataclass instance --- stdlib/_typeshed/__init__.pyi | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index 7201819b25ed..2d488a7461b1 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -3,6 +3,7 @@ # See the README.md file in this directory for more information. import sys +import typing_extensions as ts from collections.abc import Awaitable, Callable, Iterable, Sequence, Set as AbstractSet, Sized from dataclasses import Field from os import PathLike @@ -348,6 +349,8 @@ TraceFunction: TypeAlias = Callable[[FrameType, str, Any], TraceFunction | None] # https://github.com/microsoft/pyright/issues/4339 class DataclassInstance(Protocol): __dataclass_fields__: ClassVar[dict[str, Field[Any]]] + if sys.version_info >= (3, 13): + def __replace__(self, **kwargs: Any) -> ts.Self: ... # Anything that can be passed to the int/float constructors ConvertibleToInt: TypeAlias = str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc From d5c706afb2cd38e0613d506b9becec9fbc5fdd9f Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Tue, 2 Jul 2024 21:16:17 -0500 Subject: [PATCH 5/6] Avoid duplication --- stdlib/types.pyi | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/stdlib/types.pyi b/stdlib/types.pyi index bb90e32e45fc..df2d853a630c 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -290,30 +290,7 @@ class CodeType: ) -> Self: ... if sys.version_info >= (3, 13): - # Same as replace, but positional for `self`. - def __replace__( - self, - /, - *, - co_argcount: int = -1, - co_posonlyargcount: int = -1, - co_kwonlyargcount: int = -1, - co_nlocals: int = -1, - co_stacksize: int = -1, - co_flags: int = -1, - co_firstlineno: int = -1, - co_code: bytes = ..., - co_consts: tuple[object, ...] = ..., - co_names: tuple[str, ...] = ..., - co_varnames: tuple[str, ...] = ..., - co_freevars: tuple[str, ...] = ..., - co_cellvars: tuple[str, ...] = ..., - co_filename: str = ..., - co_name: str = ..., - co_qualname: str = ..., - co_linetable: bytes = ..., - co_exceptiontable: bytes = ..., - ) -> Self: ... + __replace__ = replace @final class MappingProxyType(Mapping[_KT, _VT_co]): From 4dabac79962038e3e5b83a7cd54edb7bb6b4cb38 Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Tue, 2 Jul 2024 21:17:13 -0500 Subject: [PATCH 6/6] Remove from DC protocol --- stdlib/_typeshed/__init__.pyi | 3 --- 1 file changed, 3 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index 2d488a7461b1..7201819b25ed 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -3,7 +3,6 @@ # See the README.md file in this directory for more information. import sys -import typing_extensions as ts from collections.abc import Awaitable, Callable, Iterable, Sequence, Set as AbstractSet, Sized from dataclasses import Field from os import PathLike @@ -349,8 +348,6 @@ TraceFunction: TypeAlias = Callable[[FrameType, str, Any], TraceFunction | None] # https://github.com/microsoft/pyright/issues/4339 class DataclassInstance(Protocol): __dataclass_fields__: ClassVar[dict[str, Field[Any]]] - if sys.version_info >= (3, 13): - def __replace__(self, **kwargs: Any) -> ts.Self: ... # Anything that can be passed to the int/float constructors ConvertibleToInt: TypeAlias = str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc