@@ -2,15 +2,46 @@ import _compression
2
2
import sys
3
3
import zlib
4
4
from _typeshed import AnyPath , ReadableBuffer
5
- from typing import IO , Optional , TextIO , Union , overload
5
+ from io import FileIO
6
+ from typing import Any , Optional , Protocol , TextIO , Union , overload
6
7
from typing_extensions import Literal
7
8
8
- _OpenBinaryMode = Literal ["r" , "rb" , "a" , "ab" , "w" , "wb" , "x" , "xb" ]
9
+ _ReadBinaryMode = Literal ["r" , "rb" ]
10
+ _WriteBinaryMode = Literal ["a" , "ab" , "w" , "wb" , "x" , "xb" ]
9
11
_OpenTextMode = Literal ["rt" , "at" , "wt" , "xt" ]
12
+
13
+ READ : Literal [1 ]
14
+ WRITE : Literal [2 ]
15
+
16
+ class _ReadableFileobj (Protocol ):
17
+ def read (self , __n : int ) -> bytes : ...
18
+ def seek (self , __n : int ) -> Any : ...
19
+ # The following attributes and methods are optional:
20
+ # name: str
21
+ # mode: str
22
+ # def fileno() -> int: ...
23
+
24
+ class _WritableFileobj (Protocol ):
25
+ def write (self , __b : bytes ) -> Any : ...
26
+ def flush (self ) -> Any : ...
27
+ # The following attributes and methods are optional:
28
+ # name: str
29
+ # mode: str
30
+ # def fileno() -> int: ...
31
+
32
+ @overload
33
+ def open (
34
+ filename : Union [AnyPath , _ReadableFileobj ],
35
+ mode : _ReadBinaryMode = ...,
36
+ compresslevel : int = ...,
37
+ encoding : None = ...,
38
+ errors : None = ...,
39
+ newline : None = ...,
40
+ ) -> GzipFile : ...
10
41
@overload
11
42
def open (
12
- filename : Union [AnyPath , IO [ bytes ] ],
13
- mode : _OpenBinaryMode = ... ,
43
+ filename : Union [AnyPath , _WritableFileobj ],
44
+ mode : _WriteBinaryMode ,
14
45
compresslevel : int = ...,
15
46
encoding : None = ...,
16
47
errors : None = ...,
@@ -27,7 +58,7 @@ def open(
27
58
) -> TextIO : ...
28
59
@overload
29
60
def open (
30
- filename : Union [AnyPath , IO [ bytes ] ],
61
+ filename : Union [AnyPath , _ReadableFileobj , _WritableFileobj ],
31
62
mode : str ,
32
63
compresslevel : int = ...,
33
64
encoding : Optional [str ] = ...,
@@ -36,8 +67,8 @@ def open(
36
67
) -> Union [GzipFile , TextIO ]: ...
37
68
38
69
class _PaddedFile :
39
- file : IO [ bytes ]
40
- def __init__ (self , f : IO [ bytes ] , prepend : bytes = ...) -> None : ...
70
+ file : _ReadableFileobj
71
+ def __init__ (self , f : _ReadableFileobj , prepend : bytes = ...) -> None : ...
41
72
def read (self , size : int ) -> bytes : ...
42
73
def prepend (self , prepend : bytes = ...) -> None : ...
43
74
def seek (self , off : int ) -> int : ...
@@ -47,17 +78,54 @@ if sys.version_info >= (3, 8):
47
78
class BadGzipFile (OSError ): ...
48
79
49
80
class GzipFile (_compression .BaseStream ):
50
- myfileobj : Optional [IO [ bytes ] ]
51
- mode : str
81
+ myfileobj : Optional [FileIO ]
82
+ mode : Literal [ 1 , 2 ]
52
83
name : str
53
84
compress : zlib ._Compress
54
- fileobj : IO [bytes ]
85
+ fileobj : Union [_ReadableFileobj , _WritableFileobj ]
86
+ @overload
87
+ def __init__ (
88
+ self ,
89
+ filename : Optional [AnyPath ],
90
+ mode : _ReadBinaryMode ,
91
+ compresslevel : int = ...,
92
+ fileobj : Optional [_ReadableFileobj ] = ...,
93
+ mtime : Optional [float ] = ...,
94
+ ) -> None : ...
95
+ @overload
96
+ def __init__ (
97
+ self ,
98
+ * ,
99
+ mode : _ReadBinaryMode ,
100
+ compresslevel : int = ...,
101
+ fileobj : Optional [_ReadableFileobj ] = ...,
102
+ mtime : Optional [float ] = ...,
103
+ ) -> None : ...
104
+ @overload
105
+ def __init__ (
106
+ self ,
107
+ filename : Optional [AnyPath ],
108
+ mode : _WriteBinaryMode ,
109
+ compresslevel : int = ...,
110
+ fileobj : Optional [_WritableFileobj ] = ...,
111
+ mtime : Optional [float ] = ...,
112
+ ) -> None : ...
113
+ @overload
114
+ def __init__ (
115
+ self ,
116
+ * ,
117
+ mode : _WriteBinaryMode ,
118
+ compresslevel : int = ...,
119
+ fileobj : Optional [_WritableFileobj ] = ...,
120
+ mtime : Optional [float ] = ...,
121
+ ) -> None : ...
122
+ @overload
55
123
def __init__ (
56
124
self ,
57
125
filename : Optional [AnyPath ] = ...,
58
126
mode : Optional [str ] = ...,
59
127
compresslevel : int = ...,
60
- fileobj : Optional [ IO [ bytes ] ] = ...,
128
+ fileobj : Union [ _ReadableFileobj , _WritableFileobj , None ] = ...,
61
129
mtime : Optional [float ] = ...,
62
130
) -> None : ...
63
131
@property
@@ -82,7 +150,7 @@ class GzipFile(_compression.BaseStream):
82
150
def readline (self , size : Optional [int ] = ...) -> bytes : ...
83
151
84
152
class _GzipReader (_compression .DecompressReader ):
85
- def __init__ (self , fp : IO [ bytes ] ) -> None : ...
153
+ def __init__ (self , fp : _ReadableFileobj ) -> None : ...
86
154
def read (self , size : int = ...) -> bytes : ...
87
155
88
156
if sys .version_info >= (3 , 8 ):
0 commit comments