Skip to content

Commit ff9dc57

Browse files
committed
pickle: accept ReadableBuffer
Another from python/mypy#12661 (comment) (the hit in optuna)
1 parent 2a0fc1b commit ff9dc57

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

stdlib/pickle.pyi

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
from _typeshed import ReadableBuffer
12
import sys
23
from collections.abc import Callable, Iterable, Iterator, Mapping
3-
from typing import Any, ClassVar, Protocol, Union
4+
from typing import Any, ClassVar, Protocol, Union, SupportsBytes, SupportsIndex
45
from typing_extensions import TypeAlias, final
56

67
if sys.version_info >= (3, 8):
@@ -183,11 +184,9 @@ class _WritableFileobj(Protocol):
183184
def write(self, __b: bytes) -> Any: ...
184185

185186
if sys.version_info >= (3, 8):
186-
# TODO: holistic design for buffer interface (typing.Buffer?)
187187
@final
188188
class PickleBuffer:
189-
# buffer must be a buffer-providing object
190-
def __init__(self, buffer: Any) -> None: ...
189+
def __init__(self, buffer: ReadableBuffer) -> None: ...
191190
def raw(self) -> memoryview: ...
192191
def release(self) -> None: ...
193192
_BufferCallback: TypeAlias = Callable[[PickleBuffer], Any] | None
@@ -211,14 +210,14 @@ if sys.version_info >= (3, 8):
211210
buffers: Iterable[Any] | None = ...,
212211
) -> Any: ...
213212
def loads(
214-
__data: bytes, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ..., buffers: Iterable[Any] | None = ...
213+
__data: ReadableBuffer, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ..., buffers: Iterable[Any] | None = ...
215214
) -> Any: ...
216215

217216
else:
218217
def dump(obj: Any, file: _WritableFileobj, protocol: int | None = ..., *, fix_imports: bool = ...) -> None: ...
219218
def dumps(obj: Any, protocol: int | None = ..., *, fix_imports: bool = ...) -> bytes: ...
220219
def load(file: _ReadableFileobj, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> Any: ...
221-
def loads(data: bytes, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> Any: ...
220+
def loads(data: ReadableBuffer, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> Any: ...
222221

223222
class PickleError(Exception): ...
224223
class PicklingError(PickleError): ...
@@ -359,7 +358,7 @@ if sys.version_info >= (3, 8):
359358
READONLY_BUFFER: bytes
360359

361360
def encode_long(x: int) -> bytes: ... # undocumented
362-
def decode_long(data: bytes) -> int: ... # undocumented
361+
def decode_long(data: Iterable[SupportsIndex] | SupportsBytes | ReadableBuffer) -> int: ... # undocumented
363362

364363
# pure-Python implementations
365364
_Pickler = Pickler # undocumented

0 commit comments

Comments
 (0)