From 6e32e087430ec6d56939cc18cd0af79557266aaa Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 15 Mar 2026 13:45:55 +0100 Subject: [PATCH] [pickle] Fix _Pickle.reducer_override annotation (#13545) --- stdlib/@tests/stubtest_allowlists/common.txt | 3 +++ stdlib/_pickle.pyi | 5 ++++- stdlib/pickle.pyi | 5 ++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index 868f6050e..88d0bce5a 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -436,8 +436,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 diff --git a/stdlib/_pickle.pyi b/stdlib/_pickle.pyi index 544f78717..4294de4c2 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: diff --git a/stdlib/pickle.pyi b/stdlib/pickle.pyi index 0f486b1d0..03ba6c12c 100644 --- a/stdlib/pickle.pyi +++ b/stdlib/pickle.pyi @@ -208,7 +208,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], @@ -220,6 +219,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, /) -> _ReducedType: ... class _Unpickler: dispatch: ClassVar[dict[int, Callable[[Unpickler], None]]] # undocumented, _Unpickler only