1
1
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
3
3
4
4
HIGHEST_PROTOCOL : int
5
5
DEFAULT_PROTOCOL : int
6
6
7
7
bytes_types : Tuple [Type [Any ], ...] # undocumented
8
8
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
+
9
16
if sys .version_info >= (3 , 8 ):
10
17
# TODO: holistic design for buffer interface (typing.Buffer?)
11
18
class PickleBuffer :
@@ -15,22 +22,32 @@ if sys.version_info >= (3, 8):
15
22
def release (self ) -> None : ...
16
23
_BufferCallback = Optional [Callable [[PickleBuffer ], Any ]]
17
24
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 = ...,
19
31
) -> None : ...
20
32
def dumps (
21
33
obj : Any , protocol : int | None = ..., * , fix_imports : bool = ..., buffer_callback : _BufferCallback = ...
22
34
) -> bytes : ...
23
35
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 = ...,
25
42
) -> Any : ...
26
43
def loads (
27
44
__data : bytes , * , fix_imports : bool = ..., encoding : str = ..., errors : str = ..., buffers : Iterable [Any ] | None = ...
28
45
) -> Any : ...
29
46
30
47
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 : ...
32
49
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 : ...
34
51
def loads (data : bytes , * , fix_imports : bool = ..., encoding : str = ..., errors : str = ...) -> Any : ...
35
52
36
53
class PickleError (Exception ): ...
@@ -53,11 +70,16 @@ class Pickler:
53
70
54
71
if sys .version_info >= (3 , 8 ):
55
72
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 = ...,
57
79
) -> None : ...
58
80
def reducer_override (self , obj : Any ) -> Any : ...
59
81
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 : ...
61
83
def dump (self , __obj : Any ) -> None : ...
62
84
def clear_memo (self ) -> None : ...
63
85
def persistent_id (self , obj : Any ) -> Any : ...
@@ -68,15 +90,17 @@ class Unpickler:
68
90
if sys .version_info >= (3 , 8 ):
69
91
def __init__ (
70
92
self ,
71
- file : IO [ bytes ] ,
93
+ file : _ReadableFileobj ,
72
94
* ,
73
95
fix_imports : bool = ...,
74
96
encoding : str = ...,
75
97
errors : str = ...,
76
98
buffers : Iterable [Any ] | None = ...,
77
99
) -> None : ...
78
100
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 : ...
80
104
def load (self ) -> Any : ...
81
105
def find_class (self , __module_name : str , __global_name : str ) -> Any : ...
82
106
def persistent_load (self , pid : Any ) -> Any : ...
0 commit comments