Skip to content

Commit 9b86402

Browse files
authored
pickle: use protocols instead of IO[bytes] (#6101)
1 parent 947e851 commit 9b86402

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

stdlib/pickle.pyi

+33-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import sys
2-
from typing import IO, Any, Callable, ClassVar, Iterable, Iterator, Mapping, Optional, Tuple, Type, Union
2+
from typing import Any, Callable, ClassVar, Iterable, Iterator, Mapping, Optional, Protocol, Tuple, Type, Union
33

44
HIGHEST_PROTOCOL: int
55
DEFAULT_PROTOCOL: int
66

77
bytes_types: Tuple[Type[Any], ...] # undocumented
88

9+
class _ReadableFileobj(Protocol):
10+
def read(self, __n: int) -> bytes: ...
11+
def readline(self) -> bytes: ...
12+
13+
class _WritableFileobj(Protocol):
14+
def write(self, __b: bytes) -> Any: ...
15+
916
if sys.version_info >= (3, 8):
1017
# TODO: holistic design for buffer interface (typing.Buffer?)
1118
class PickleBuffer:
@@ -15,22 +22,32 @@ if sys.version_info >= (3, 8):
1522
def release(self) -> None: ...
1623
_BufferCallback = Optional[Callable[[PickleBuffer], Any]]
1724
def dump(
18-
obj: Any, file: IO[bytes], protocol: int | None = ..., *, fix_imports: bool = ..., buffer_callback: _BufferCallback = ...
25+
obj: Any,
26+
file: _WritableFileobj,
27+
protocol: int | None = ...,
28+
*,
29+
fix_imports: bool = ...,
30+
buffer_callback: _BufferCallback = ...,
1931
) -> None: ...
2032
def dumps(
2133
obj: Any, protocol: int | None = ..., *, fix_imports: bool = ..., buffer_callback: _BufferCallback = ...
2234
) -> bytes: ...
2335
def load(
24-
file: IO[bytes], *, fix_imports: bool = ..., encoding: str = ..., errors: str = ..., buffers: Iterable[Any] | None = ...
36+
file: _ReadableFileobj,
37+
*,
38+
fix_imports: bool = ...,
39+
encoding: str = ...,
40+
errors: str = ...,
41+
buffers: Iterable[Any] | None = ...,
2542
) -> Any: ...
2643
def loads(
2744
__data: bytes, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ..., buffers: Iterable[Any] | None = ...
2845
) -> Any: ...
2946

3047
else:
31-
def dump(obj: Any, file: IO[bytes], protocol: int | None = ..., *, fix_imports: bool = ...) -> None: ...
48+
def dump(obj: Any, file: _WritableFileobj, protocol: int | None = ..., *, fix_imports: bool = ...) -> None: ...
3249
def dumps(obj: Any, protocol: int | None = ..., *, fix_imports: bool = ...) -> bytes: ...
33-
def load(file: IO[bytes], *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> Any: ...
50+
def load(file: _ReadableFileobj, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> Any: ...
3451
def loads(data: bytes, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> Any: ...
3552

3653
class PickleError(Exception): ...
@@ -53,11 +70,16 @@ class Pickler:
5370

5471
if sys.version_info >= (3, 8):
5572
def __init__(
56-
self, file: IO[bytes], protocol: int | None = ..., *, fix_imports: bool = ..., buffer_callback: _BufferCallback = ...
73+
self,
74+
file: _WritableFileobj,
75+
protocol: int | None = ...,
76+
*,
77+
fix_imports: bool = ...,
78+
buffer_callback: _BufferCallback = ...,
5779
) -> None: ...
5880
def reducer_override(self, obj: Any) -> Any: ...
5981
else:
60-
def __init__(self, file: IO[bytes], protocol: int | None = ..., *, fix_imports: bool = ...) -> None: ...
82+
def __init__(self, file: _WritableFileobj, protocol: int | None = ..., *, fix_imports: bool = ...) -> None: ...
6183
def dump(self, __obj: Any) -> None: ...
6284
def clear_memo(self) -> None: ...
6385
def persistent_id(self, obj: Any) -> Any: ...
@@ -68,15 +90,17 @@ class Unpickler:
6890
if sys.version_info >= (3, 8):
6991
def __init__(
7092
self,
71-
file: IO[bytes],
93+
file: _ReadableFileobj,
7294
*,
7395
fix_imports: bool = ...,
7496
encoding: str = ...,
7597
errors: str = ...,
7698
buffers: Iterable[Any] | None = ...,
7799
) -> None: ...
78100
else:
79-
def __init__(self, file: IO[bytes], *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> None: ...
101+
def __init__(
102+
self, file: _ReadableFileobj, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...
103+
) -> None: ...
80104
def load(self) -> Any: ...
81105
def find_class(self, __module_name: str, __global_name: str) -> Any: ...
82106
def persistent_load(self, pid: Any) -> Any: ...

0 commit comments

Comments
 (0)