From 67cf44a8e7b90b05661751256ca214124e3f0670 Mon Sep 17 00:00:00 2001 From: Guido Imperiale Date: Mon, 20 Jan 2020 17:43:37 +0000 Subject: [PATCH 1/4] Pickle 5 --- stdlib/2and3/pickle.pyi | 56 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/stdlib/2and3/pickle.pyi b/stdlib/2and3/pickle.pyi index ec9353f6b751..2f97d5f47063 100644 --- a/stdlib/2and3/pickle.pyi +++ b/stdlib/2and3/pickle.pyi @@ -1,20 +1,54 @@ import sys -from typing import Any, IO, Mapping, Union, Tuple, Callable, Optional, Iterator +from typing import ( + Any, IO, Mapping, Union, Tuple, Callable, Optional, Iterable, Iterator +) HIGHEST_PROTOCOL: int if sys.version_info >= (3, 0): DEFAULT_PROTOCOL: int -if sys.version_info >= (3, 0): +if sys.version_info >= (3, 8): + # TODO: holistic design for a type for objects with the buffer interface + from typing import NewType + + Buffer = NewType('Buffer', Any) + + class PickleBuffer(Buffer): + # buffer must be a buffer-providing object + def __init__(self, buffer: Buffer): ... + def raw(self) -> memoryview: ... + def release(self) -> None: ... + + def dump( + obj: Any, file: IO[bytes], protocol: Optional[int] = ..., *, + fix_imports: bool = ..., + buffer_callback: Optional[Callable[[PickleBuffer], None]] = ... + ) -> None: ... + def dumps( + obj: Any, protocol: Optional[int] = ..., *, + fix_imports: bool = ..., + buffer_callback: Optional[Callable[[PickleBuffer], None]] = ... + ) -> bytes: ... + def load( + file: IO[bytes], *, fix_imports: bool = ..., encoding: str = ..., + errors: str = ..., buffers: Optional[Iterable[Buffer]] = ... + ) -> Any: ... + def loads( + bytes_object: bytes, *, fix_imports: bool = ..., encoding: str = ..., + errors: str = ..., buffers: Optional[Iterable[Buffer]] = ... + ) -> Any: ... + +elif sys.version_info >= (3, 0): def dump(obj: Any, file: IO[bytes], protocol: Optional[int] = ..., *, fix_imports: bool = ...) -> None: ... def dumps(obj: Any, protocol: Optional[int] = ..., *, fix_imports: bool = ...) -> bytes: ... - def loads(bytes_object: bytes, *, fix_imports: bool = ..., - encoding: str = ..., errors: str = ...) -> Any: ... def load(file: IO[bytes], *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> Any: ... + def loads(bytes_object: bytes, *, fix_imports: bool = ..., + encoding: str = ..., errors: str = ...) -> Any: ... + else: def dump(obj: Any, file: IO[bytes], protocol: Optional[int] = ...) -> None: ... def dumps(obj: Any, protocol: Optional[int] = ...) -> bytes: ... @@ -39,7 +73,13 @@ class Pickler: if sys.version_info >= (3, 3): dispatch_table: Mapping[type, Callable[[Any], _reducedtype]] - if sys.version_info >= (3, 0): + if sys.version_info >= (3, 8): + def __init__(self, file: IO[bytes], protocol: Optional[int] = ..., *, + fix_imports: bool = ..., + buffer_callback: Optional[Callable[[PickleBuffer], None]] = ... + ) -> None: ... + def reducer_override(self, obj: Any) -> Union[tuple, type(NotImplemented)]: ... + elif sys.version_info >= (3, 0): def __init__(self, file: IO[bytes], protocol: Optional[int] = ..., *, fix_imports: bool = ...) -> None: ... else: @@ -52,7 +92,11 @@ class Pickler: def reducer_override(self, obj: Any) -> Any: ... class Unpickler: - if sys.version_info >= (3, 0): + if sys.version_info >= (3, 8): + def __init__(self, file: IO[bytes], *, fix_imports: bool = ..., + encoding: str = ..., errors: str = ..., + buffers: Optional[Iterable[Buffer]] = ...) -> None: ... + elif sys.version_info >= (3, 0): def __init__(self, file: IO[bytes], *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> None: ... else: From e4183a9d625e3a19b5175ead746d044b3616c817 Mon Sep 17 00:00:00 2001 From: Guido Imperiale Date: Mon, 20 Jan 2020 17:55:05 +0000 Subject: [PATCH 2/4] Fixes --- stdlib/2and3/pickle.pyi | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/stdlib/2and3/pickle.pyi b/stdlib/2and3/pickle.pyi index 2f97d5f47063..913e29630fce 100644 --- a/stdlib/2and3/pickle.pyi +++ b/stdlib/2and3/pickle.pyi @@ -1,7 +1,5 @@ import sys -from typing import ( - Any, IO, Mapping, Union, Tuple, Callable, Optional, Iterable, Iterator -) +from typing import Any, IO, Mapping, Union, Tuple, Callable, Optional, Iterable, Iterator HIGHEST_PROTOCOL: int if sys.version_info >= (3, 0): @@ -9,17 +7,13 @@ if sys.version_info >= (3, 0): if sys.version_info >= (3, 8): - # TODO: holistic design for a type for objects with the buffer interface - from typing import NewType + # TODO: holistic design for buffer interface (typing.Buffer?) - Buffer = NewType('Buffer', Any) - - class PickleBuffer(Buffer): + class PickleBuffer: # buffer must be a buffer-providing object - def __init__(self, buffer: Buffer): ... + def __init__(self, buffer: Any): ... def raw(self) -> memoryview: ... def release(self) -> None: ... - def dump( obj: Any, file: IO[bytes], protocol: Optional[int] = ..., *, fix_imports: bool = ..., @@ -32,13 +26,12 @@ if sys.version_info >= (3, 8): ) -> bytes: ... def load( file: IO[bytes], *, fix_imports: bool = ..., encoding: str = ..., - errors: str = ..., buffers: Optional[Iterable[Buffer]] = ... + errors: str = ..., buffers: Optional[Iterable[Any]] = ... ) -> Any: ... def loads( bytes_object: bytes, *, fix_imports: bool = ..., encoding: str = ..., - errors: str = ..., buffers: Optional[Iterable[Buffer]] = ... + errors: str = ..., buffers: Optional[Iterable[Any]] = ... ) -> Any: ... - elif sys.version_info >= (3, 0): def dump(obj: Any, file: IO[bytes], protocol: Optional[int] = ..., *, fix_imports: bool = ...) -> None: ... @@ -48,7 +41,6 @@ elif sys.version_info >= (3, 0): errors: str = ...) -> Any: ... def loads(bytes_object: bytes, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> Any: ... - else: def dump(obj: Any, file: IO[bytes], protocol: Optional[int] = ...) -> None: ... def dumps(obj: Any, protocol: Optional[int] = ...) -> bytes: ... @@ -78,7 +70,7 @@ class Pickler: fix_imports: bool = ..., buffer_callback: Optional[Callable[[PickleBuffer], None]] = ... ) -> None: ... - def reducer_override(self, obj: Any) -> Union[tuple, type(NotImplemented)]: ... + def reducer_override(self, obj: Any) -> Any: ... elif sys.version_info >= (3, 0): def __init__(self, file: IO[bytes], protocol: Optional[int] = ..., *, fix_imports: bool = ...) -> None: ... @@ -95,7 +87,7 @@ class Unpickler: if sys.version_info >= (3, 8): def __init__(self, file: IO[bytes], *, fix_imports: bool = ..., encoding: str = ..., errors: str = ..., - buffers: Optional[Iterable[Buffer]] = ...) -> None: ... + buffers: Optional[Iterable[Any]] = ...) -> None: ... elif sys.version_info >= (3, 0): def __init__(self, file: IO[bytes], *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> None: ... From c71c841a5544c3caf75a17237f1fc24dd7af881b Mon Sep 17 00:00:00 2001 From: Guido Imperiale Date: Mon, 20 Jan 2020 18:01:36 +0000 Subject: [PATCH 3/4] Force CI rebuild From be3a35868b2ff25cea28a23a8b06660c7113a610 Mon Sep 17 00:00:00 2001 From: Guido Imperiale Date: Thu, 23 Jan 2020 16:55:07 +0000 Subject: [PATCH 4/4] Code review --- stdlib/2and3/pickle.pyi | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/stdlib/2and3/pickle.pyi b/stdlib/2and3/pickle.pyi index 913e29630fce..99c047d47fe3 100644 --- a/stdlib/2and3/pickle.pyi +++ b/stdlib/2and3/pickle.pyi @@ -11,25 +11,28 @@ if sys.version_info >= (3, 8): class PickleBuffer: # buffer must be a buffer-providing object - def __init__(self, buffer: Any): ... + def __init__(self, buffer: Any) -> None: ... def raw(self) -> memoryview: ... def release(self) -> None: ... + + _BufferCallback = Optional[Callable[[PickleBuffer], Any]] + def dump( obj: Any, file: IO[bytes], protocol: Optional[int] = ..., *, fix_imports: bool = ..., - buffer_callback: Optional[Callable[[PickleBuffer], None]] = ... + buffer_callback: _BufferCallback = ... ) -> None: ... def dumps( obj: Any, protocol: Optional[int] = ..., *, fix_imports: bool = ..., - buffer_callback: Optional[Callable[[PickleBuffer], None]] = ... + buffer_callback: _BufferCallback = ... ) -> bytes: ... def load( file: IO[bytes], *, fix_imports: bool = ..., encoding: str = ..., errors: str = ..., buffers: Optional[Iterable[Any]] = ... ) -> Any: ... def loads( - bytes_object: bytes, *, fix_imports: bool = ..., encoding: str = ..., + data: bytes, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ..., buffers: Optional[Iterable[Any]] = ... ) -> Any: ... elif sys.version_info >= (3, 0): @@ -39,7 +42,7 @@ elif sys.version_info >= (3, 0): fix_imports: bool = ...) -> bytes: ... def load(file: IO[bytes], *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> Any: ... - def loads(bytes_object: bytes, *, fix_imports: bool = ..., + def loads(data: bytes, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> Any: ... else: def dump(obj: Any, file: IO[bytes], protocol: Optional[int] = ...) -> None: ... @@ -67,8 +70,7 @@ class Pickler: if sys.version_info >= (3, 8): def __init__(self, file: IO[bytes], protocol: Optional[int] = ..., *, - fix_imports: bool = ..., - buffer_callback: Optional[Callable[[PickleBuffer], None]] = ... + fix_imports: bool = ..., buffer_callback: _BufferCallback = ... ) -> None: ... def reducer_override(self, obj: Any) -> Any: ... elif sys.version_info >= (3, 0):