Skip to content

Commit 6e32e08

Browse files
authored
[pickle] Fix _Pickle.reducer_override annotation (#13545)
1 parent f8f0794 commit 6e32e08

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

stdlib/@tests/stubtest_allowlists/common.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,11 @@ os._wrap_close.write # Methods that come from __getattr__() at runtime
436436
os._wrap_close.writelines # Methods that come from __getattr__() at runtime
437437
os.PathLike.__class_getitem__ # PathLike is a protocol; we don't expect all PathLike classes to implement class_getitem
438438

439+
_pickle.Pickler.reducer_override # Can be added by subclasses
440+
pickle.Pickler.reducer_override # Can be added by subclasses
439441
pickle._Pickler\..* # Best effort typing for undocumented internals
440442
pickle._Unpickler\..* # Best effort typing for undocumented internals
443+
441444
shutil.rmtree # function with attributes, which we approximate with a callable protocol
442445
socketserver.BaseServer.get_request # Not implemented, but expected to exist on subclasses.
443446
ssl.PROTOCOL_SSLv2 # Depends on the existence and flags of SSL

stdlib/_pickle.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class PicklerMemoProxy:
6161
class Pickler:
6262
fast: bool
6363
dispatch_table: Mapping[type, Callable[[Any], _ReducedType]]
64-
reducer_override: Callable[[Any], Any]
6564
bin: bool # undocumented
6665
def __init__(
6766
self,
@@ -79,6 +78,10 @@ class Pickler:
7978

8079
# this method has no default implementation for Python < 3.13
8180
def persistent_id(self, obj: Any, /) -> Any: ...
81+
# The following method is not defined on _Pickler, but can be defined on
82+
# sub-classes. Should return `NotImplemented` if pickling the supplied
83+
# object is not supported and returns the same types as `__reduce__()`.
84+
def reducer_override(self, obj: object, /) -> _ReducedType: ...
8285

8386
@type_check_only
8487
class UnpicklerMemoProxy:

stdlib/pickle.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ class _Pickler:
208208
dispatch_table: Mapping[type, Callable[[Any], _ReducedType]]
209209
bin: bool # undocumented
210210
dispatch: ClassVar[dict[type, Callable[[Unpickler, Any], None]]] # undocumented, _Pickler only
211-
reducer_override: Callable[[Any], Any]
212211
def __init__(
213212
self,
214213
file: SupportsWrite[bytes],
@@ -220,6 +219,10 @@ class _Pickler:
220219
def dump(self, obj: Any) -> None: ...
221220
def clear_memo(self) -> None: ...
222221
def persistent_id(self, obj: Any) -> Any: ...
222+
# The following method is not defined on _Pickler, but can be defined on
223+
# sub-classes. Should return `NotImplemented` if pickling the supplied
224+
# object is not supported and returns the same types as `__reduce__()`.
225+
def reducer_override(self, obj: object, /) -> _ReducedType: ...
223226

224227
class _Unpickler:
225228
dispatch: ClassVar[dict[int, Callable[[Unpickler], None]]] # undocumented, _Unpickler only

0 commit comments

Comments
 (0)