From b4d0ceda5324b08e59344ec4bd9885ca122bb462 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 26 Feb 2025 15:16:38 +0100 Subject: [PATCH 1/4] Fix _Pickle.reducer_override annotation Closes: #13446 --- stdlib/pickle.pyi | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/stdlib/pickle.pyi b/stdlib/pickle.pyi index 2d80d61645e0..a30f33ca27d0 100644 --- a/stdlib/pickle.pyi +++ b/stdlib/pickle.pyi @@ -204,7 +204,6 @@ class _Pickler: dispatch_table: Mapping[type, Callable[[Any], _ReducedType]] bin: bool # undocumented dispatch: ClassVar[dict[type, Callable[[Unpickler, Any], None]]] # undocumented, _Pickler only - reducer_override: Callable[[Any], Any] def __init__( self, file: SupportsWrite[bytes], @@ -216,6 +215,10 @@ class _Pickler: def dump(self, obj: Any) -> None: ... def clear_memo(self) -> None: ... def persistent_id(self, obj: Any) -> Any: ... + # The following method is not defined on _Pickler, but can be defined on + # sub-classes. Should return `NotImplemented` if pickling the supplied + # object is not supported and returns the same types as `__reduce__()`. + def reducer_override(self, obj: object, /) -> Any: ... class _Unpickler: dispatch: ClassVar[dict[int, Callable[[Unpickler], None]]] # undocumented, _Unpickler only From 958ca6c23afa9c127696370ddd9898dd13514871 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 26 Feb 2025 15:18:53 +0100 Subject: [PATCH 2/4] Return _ReducedType --- stdlib/pickle.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/pickle.pyi b/stdlib/pickle.pyi index a30f33ca27d0..afbb83d74e4a 100644 --- a/stdlib/pickle.pyi +++ b/stdlib/pickle.pyi @@ -218,7 +218,7 @@ class _Pickler: # The following method is not defined on _Pickler, but can be defined on # sub-classes. Should return `NotImplemented` if pickling the supplied # object is not supported and returns the same types as `__reduce__()`. - def reducer_override(self, obj: object, /) -> Any: ... + def reducer_override(self, obj: object, /) -> _ReducedType: ... class _Unpickler: dispatch: ClassVar[dict[int, Callable[[Unpickler], None]]] # undocumented, _Unpickler only From ed79b9559a817db38825d9ea801d4723bc4794db Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 8 Oct 2025 13:46:03 +0200 Subject: [PATCH 3/4] Add _pickler.Pickler.reducer_override --- stdlib/_pickle.pyi | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/stdlib/_pickle.pyi b/stdlib/_pickle.pyi index 544f787172d6..4294de4c2c7f 100644 --- a/stdlib/_pickle.pyi +++ b/stdlib/_pickle.pyi @@ -61,7 +61,6 @@ class PicklerMemoProxy: class Pickler: fast: bool dispatch_table: Mapping[type, Callable[[Any], _ReducedType]] - reducer_override: Callable[[Any], Any] bin: bool # undocumented def __init__( self, @@ -79,6 +78,10 @@ class Pickler: # this method has no default implementation for Python < 3.13 def persistent_id(self, obj: Any, /) -> Any: ... + # The following method is not defined on _Pickler, but can be defined on + # sub-classes. Should return `NotImplemented` if pickling the supplied + # object is not supported and returns the same types as `__reduce__()`. + def reducer_override(self, obj: object, /) -> _ReducedType: ... @type_check_only class UnpicklerMemoProxy: From fccd909c5dbf8c34d3b9e1132137641c1b0a79bc Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 8 Oct 2025 13:53:58 +0200 Subject: [PATCH 4/4] Add allowlist entries --- stdlib/@tests/stubtest_allowlists/common.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index b697bbb6dee4..c1af6f02b396 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -394,8 +394,11 @@ os._wrap_close.write # Methods that come from __getattr__() at runtime os._wrap_close.writelines # Methods that come from __getattr__() at runtime os.PathLike.__class_getitem__ # PathLike is a protocol; we don't expect all PathLike classes to implement class_getitem +_pickle.Pickler.reducer_override # Can be added by subclasses +pickle.Pickler.reducer_override # Can be added by subclasses pickle._Pickler\..* # Best effort typing for undocumented internals pickle._Unpickler\..* # Best effort typing for undocumented internals + shutil.rmtree # function with attributes, which we approximate with a callable protocol socketserver.BaseServer.get_request # Not implemented, but expected to exist on subclasses. ssl.PROTOCOL_SSLv2 # Depends on the existence and flags of SSL