From ff9dc57f0b0d74b7b86fd3e6ee1e6c63ea15d6d0 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 23 Apr 2022 10:16:34 -0700 Subject: [PATCH 1/3] pickle: accept ReadableBuffer Another from https://github.com/python/mypy/pull/12661#issuecomment-1107533048 (the hit in optuna) --- stdlib/pickle.pyi | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/stdlib/pickle.pyi b/stdlib/pickle.pyi index 1481e435c48d..7fba7c8cb4c8 100644 --- a/stdlib/pickle.pyi +++ b/stdlib/pickle.pyi @@ -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): @@ -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 @@ -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): ... @@ -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 From e8867c4e4e456489c452a1ad6bddf61d10eb8761 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 23 Apr 2022 17:18:19 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/pickle.pyi | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/stdlib/pickle.pyi b/stdlib/pickle.pyi index 7fba7c8cb4c8..f82f482cf3ea 100644 --- a/stdlib/pickle.pyi +++ b/stdlib/pickle.pyi @@ -1,7 +1,7 @@ -from _typeshed import ReadableBuffer import sys +from _typeshed import ReadableBuffer from collections.abc import Callable, Iterable, Iterator, Mapping -from typing import Any, ClassVar, Protocol, Union, SupportsBytes, SupportsIndex +from typing import Any, ClassVar, Protocol, SupportsBytes, SupportsIndex, Union from typing_extensions import TypeAlias, final if sys.version_info >= (3, 8): @@ -210,7 +210,12 @@ if sys.version_info >= (3, 8): buffers: Iterable[Any] | None = ..., ) -> Any: ... def loads( - __data: ReadableBuffer, *, 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: From d1bcdcde3620f403cc2f634c2b76d7f1e8d72bdd Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 23 Apr 2022 10:29:01 -0700 Subject: [PATCH 3/3] SupportsIndex --- stdlib/pickle.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/pickle.pyi b/stdlib/pickle.pyi index f82f482cf3ea..d58cf8ed9d50 100644 --- a/stdlib/pickle.pyi +++ b/stdlib/pickle.pyi @@ -1,8 +1,8 @@ import sys from _typeshed import ReadableBuffer from collections.abc import Callable, Iterable, Iterator, Mapping -from typing import Any, ClassVar, Protocol, SupportsBytes, SupportsIndex, Union -from typing_extensions import TypeAlias, final +from typing import Any, ClassVar, Protocol, SupportsBytes, Union +from typing_extensions import SupportsIndex, TypeAlias, final if sys.version_info >= (3, 8): __all__ = [