Skip to content

Commit

Permalink
pickle: accept ReadableBuffer
Browse files Browse the repository at this point in the history
Another from python/mypy#12661 (comment) (the hit in optuna)
  • Loading branch information
JelleZijlstra committed Apr 23, 2022
1 parent 2a0fc1b commit ff9dc57
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions stdlib/pickle.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from _typeshed import ReadableBuffer
import sys
from collections.abc import Callable, Iterable, Iterator, Mapping
from typing import Any, ClassVar, Protocol, Union
from typing import Any, ClassVar, Protocol, Union, SupportsBytes, SupportsIndex
from typing_extensions import TypeAlias, final

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

if sys.version_info >= (3, 8):
# TODO: holistic design for buffer interface (typing.Buffer?)
@final
class PickleBuffer:
# buffer must be a buffer-providing object
def __init__(self, buffer: Any) -> None: ...
def __init__(self, buffer: ReadableBuffer) -> None: ...
def raw(self) -> memoryview: ...
def release(self) -> None: ...
_BufferCallback: TypeAlias = Callable[[PickleBuffer], Any] | None
Expand All @@ -211,14 +210,14 @@ if sys.version_info >= (3, 8):
buffers: Iterable[Any] | None = ...,
) -> Any: ...
def loads(
__data: bytes, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ..., buffers: Iterable[Any] | None = ...
__data: ReadableBuffer, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ..., buffers: Iterable[Any] | None = ...
) -> Any: ...

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

class PickleError(Exception): ...
class PicklingError(PickleError): ...
Expand Down Expand Up @@ -359,7 +358,7 @@ if sys.version_info >= (3, 8):
READONLY_BUFFER: bytes

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

# pure-Python implementations
_Pickler = Pickler # undocumented
Expand Down

0 comments on commit ff9dc57

Please sign in to comment.