Skip to content

Commit 23749a6

Browse files
committed
overload + Literal on BZ2, gzip, LZMA
1 parent 6bec337 commit 23749a6

File tree

3 files changed

+202
-56
lines changed

3 files changed

+202
-56
lines changed

stdlib/2and3/bz2.pyi

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,58 @@
11
import io
22
import sys
3-
from typing import Any, IO, Optional, Union
3+
from os.path import _PathType
4+
from typing import IO, Any, Optional, TextIO, Union, overload
45

5-
if sys.version_info >= (3, 6):
6-
from os import PathLike
7-
_PathOrFile = Union[str, bytes, IO[Any], PathLike[Any]]
8-
elif sys.version_info >= (3, 3):
9-
_PathOrFile = Union[str, bytes, IO[Any]]
6+
if sys.version_info >= (3, 8):
7+
from typing import Literal
108
else:
11-
_PathOrFile = str
9+
from typing_extensions import Literal
10+
11+
_PathOrFile = Union[_PathType, IO[bytes]]
1212

1313
def compress(data: bytes, compresslevel: int = ...) -> bytes: ...
1414
def decompress(data: bytes) -> bytes: ...
1515

1616
if sys.version_info >= (3, 3):
17-
def open(filename: _PathOrFile,
18-
mode: str = ...,
19-
compresslevel: int = ...,
20-
encoding: Optional[str] = ...,
21-
errors: Optional[str] = ...,
22-
newline: Optional[str] = ...) -> IO[Any]: ...
17+
if sys.version_info >= (3, 4):
18+
# Changed in version 3.4: The 'x' (exclusive creation) mode was added.
19+
_OPEN_BINARY_MODE = Literal["r", "rb", "w", "wb", "x", "xb", "a", "ab"]
20+
_OPEN_TEXT_MODE = Literal["rt", "wt", "xt", "at"]
21+
else:
22+
_OPEN_BINARY_MODE = Literal["r", "rb", "w", "wb", "a", "ab"]
23+
_OPEN_TEXT_MODE = Literal["rt", "wt", "at"]
24+
@overload
25+
def open(
26+
filename: _PathOrFile,
27+
mode: _OPEN_BINARY_MODE = ...,
28+
compresslevel: int = ...,
29+
encoding: None = ...,
30+
errors: None = ...,
31+
newline: None = ...,
32+
) -> BZ2File: ...
33+
@overload
34+
def open(
35+
filename: _PathType,
36+
mode: _OPEN_TEXT_MODE,
37+
compresslevel: int = ...,
38+
encoding: Optional[str] = ...,
39+
errors: Optional[str] = ...,
40+
newline: Optional[str] = ...,
41+
) -> TextIO: ...
42+
@overload
43+
def open(
44+
filename: _PathOrFile,
45+
mode: str = ...,
46+
compresslevel: int = ...,
47+
encoding: Optional[str] = ...,
48+
errors: Optional[str] = ...,
49+
newline: Optional[str] = ...,
50+
) -> Union[BZ2File, TextIO]: ...
2351

2452
class BZ2File(io.BufferedIOBase, IO[bytes]): # type: ignore # python/mypy#5027
25-
def __init__(self,
26-
filename: _PathOrFile,
27-
mode: str = ...,
28-
buffering: Optional[Any] = ...,
29-
compresslevel: int = ...) -> None: ...
53+
def __init__(
54+
self, filename: _PathOrFile, mode: str = ..., buffering: Optional[Any] = ..., compresslevel: int = ...
55+
) -> None: ...
3056

3157
class BZ2Compressor(object):
3258
def __init__(self, compresslevel: int = ...) -> None: ...

stdlib/3/gzip.pyi

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,55 @@
1-
from typing import Any, IO, Optional
2-
from os.path import _PathType
3-
import _compression
41
import sys
52
import zlib
3+
from os.path import _PathType
4+
from typing import IO, BinaryIO, Optional, TextIO, Union, overload
65

7-
def open(filename, mode: str = ..., compresslevel: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., newline: Optional[str] = ...) -> IO[Any]: ...
6+
import _compression
7+
8+
if sys.version_info >= (3, 8):
9+
from typing import Literal
10+
else:
11+
from typing_extensions import Literal
12+
13+
if sys.version_info >= (3, 3):
14+
# Changed in version 3.3: Added support for filename being a file object, support for text mode, and the encoding, errors and newline arguments.
15+
16+
if sys.version_info >= (3, 4):
17+
# Changed in version 3.4: Added support for the 'x', 'xb' and 'xt' modes.
18+
_OPEN_BINARY_MODE = Literal["r", "rb", "a", "ab", "w", "wb", "x", "xb"]
19+
_OPEN_TEXT_MODE = Literal["rt", "at", "wt", "xt"]
20+
else:
21+
_OPEN_BINARY_MODE = Literal["r", "rb", "a", "ab", "w", "wb"]
22+
_OPEN_TEXT_MODE = Literal["rt", "at", "wt"]
23+
@overload
24+
def open(
25+
filename: Union[_PathType, IO[bytes]],
26+
mode: _OPEN_BINARY_MODE = ...,
27+
compresslevel: int = ...,
28+
encoding: None = ...,
29+
errors: None = ...,
30+
newline: None = ...,
31+
) -> GzipFile: ...
32+
@overload
33+
def open(
34+
filename: _PathType,
35+
mode: _OPEN_TEXT_MODE,
36+
compresslevel: int = ...,
37+
encoding: Optional[str] = ...,
38+
errors: Optional[str] = ...,
39+
newline: Optional[str] = ...,
40+
) -> TextIO: ...
41+
@overload
42+
def open(
43+
filename: Union[_PathType, IO[bytes]],
44+
mode: str = ...,
45+
compresslevel: int = ...,
46+
encoding: Optional[str] = ...,
47+
errors: Optional[str] = ...,
48+
newline: Optional[str] = ...,
49+
) -> Union[GzipFile, TextIO]: ...
50+
51+
else:
52+
def open(filename: _PathType, mode: str = ..., compresslevel: int = ...) -> GzipFile: ...
853

954
class _PaddedFile:
1055
file: IO[bytes]
@@ -20,7 +65,14 @@ class GzipFile(_compression.BaseStream):
2065
name: str
2166
compress: zlib._Compress
2267
fileobj: IO[bytes]
23-
def __init__(self, filename: Optional[_PathType] = ..., mode: Optional[str] = ..., compresslevel: int = ..., fileobj: Optional[IO[bytes]] = ..., mtime: Optional[float] = ...) -> None: ...
68+
def __init__(
69+
self,
70+
filename: Optional[_PathType] = ...,
71+
mode: Optional[str] = ...,
72+
compresslevel: int = ...,
73+
fileobj: Optional[IO[bytes]] = ...,
74+
mtime: Optional[float] = ...,
75+
) -> None: ...
2476
@property
2577
def filename(self) -> str: ...
2678
@property
@@ -48,6 +100,8 @@ class _GzipReader(_compression.DecompressReader):
48100

49101
if sys.version_info >= (3, 8):
50102
def compress(data, compresslevel: int = ..., *, mtime: Optional[float] = ...) -> bytes: ...
103+
51104
else:
52105
def compress(data, compresslevel: int = ...) -> bytes: ...
106+
53107
def decompress(data: bytes) -> bytes: ...

stdlib/3/lzma.pyi

Lines changed: 99 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import io
22
import sys
3-
from typing import Any, IO, Mapping, Optional, Sequence, Union
3+
from os.path import _PathType
4+
from typing import IO, Any, Mapping, Optional, Sequence, TextIO, Union, overload
45

5-
if sys.version_info >= (3, 6):
6-
from os import PathLike
7-
_PathOrFile = Union[str, bytes, IO[Any], PathLike[Any]]
6+
if sys.version_info >= (3, 8):
7+
from typing import Literal
88
else:
9-
_PathOrFile = Union[str, bytes, IO[Any]]
9+
from typing_extensions import Literal
10+
11+
if sys.version_info >= (3, 4):
12+
# Changed in version 3.4: Added support for the "x", "xb" and "xt" modes.
13+
_OPEN_BINARY_WRITING_MODE = Literal["w", "wb", "x", "xb", "a", "ab"]
14+
_OPEN_TEXT_WRITING_MODE = Literal["wt", "xt", "at"]
15+
else:
16+
_OPEN_BINARY_WRITING_MODE = Literal["w", "wb", "a", "ab"]
17+
_OPEN_TEXT_WRITING_MODE = Literal["wt", "at"]
18+
19+
_PathOrFile = Union[_PathType, IO[bytes]]
1020

1121
_FilterChain = Sequence[Mapping[str, Any]]
1222

@@ -41,7 +51,9 @@ PRESET_EXTREME: int
4151

4252
# from _lzma.c
4353
class LZMADecompressor(object):
44-
def __init__(self, format: Optional[int] = ..., memlimit: Optional[int] = ..., filters: Optional[_FilterChain] = ...) -> None: ...
54+
def __init__(
55+
self, format: Optional[int] = ..., memlimit: Optional[int] = ..., filters: Optional[_FilterChain] = ...
56+
) -> None: ...
4557
def decompress(self, data: bytes, max_length: int = ...) -> bytes: ...
4658
@property
4759
def check(self) -> int: ...
@@ -54,27 +66,25 @@ class LZMADecompressor(object):
5466

5567
# from _lzma.c
5668
class LZMACompressor(object):
57-
def __init__(self,
58-
format: Optional[int] = ...,
59-
check: int = ...,
60-
preset: Optional[int] = ...,
61-
filters: Optional[_FilterChain] = ...) -> None: ...
69+
def __init__(
70+
self, format: Optional[int] = ..., check: int = ..., preset: Optional[int] = ..., filters: Optional[_FilterChain] = ...
71+
) -> None: ...
6272
def compress(self, data: bytes) -> bytes: ...
6373
def flush(self) -> bytes: ...
6474

65-
6675
class LZMAError(Exception): ...
6776

68-
6977
class LZMAFile(io.BufferedIOBase, IO[bytes]): # type: ignore # python/mypy#5027
70-
def __init__(self,
71-
filename: Optional[_PathOrFile] = ...,
72-
mode: str = ...,
73-
*,
74-
format: Optional[int] = ...,
75-
check: int = ...,
76-
preset: Optional[int] = ...,
77-
filters: Optional[_FilterChain] = ...) -> None: ...
78+
def __init__(
79+
self,
80+
filename: Optional[_PathOrFile] = ...,
81+
mode: str = ...,
82+
*,
83+
format: Optional[int] = ...,
84+
check: int = ...,
85+
preset: Optional[int] = ...,
86+
filters: Optional[_FilterChain] = ...,
87+
) -> None: ...
7888
def close(self) -> None: ...
7989
@property
8090
def closed(self) -> bool: ...
@@ -90,17 +100,73 @@ class LZMAFile(io.BufferedIOBase, IO[bytes]): # type: ignore # python/mypy#502
90100
def seek(self, offset: int, whence: int = ...) -> int: ...
91101
def tell(self) -> int: ...
92102

93-
94-
def open(filename: _PathOrFile,
95-
mode: str = ...,
96-
*,
97-
format: Optional[int] = ...,
98-
check: int = ...,
99-
preset: Optional[int] = ...,
100-
filters: Optional[_FilterChain] = ...,
101-
encoding: Optional[str] = ...,
102-
errors: Optional[str] = ...,
103-
newline: Optional[str] = ...) -> IO[Any]: ...
104-
def compress(data: bytes, format: int = ..., check: int = ..., preset: Optional[int] = ..., filters: Optional[_FilterChain] = ...) -> bytes: ...
103+
@overload
104+
def open(
105+
filename: _PathOrFile,
106+
mode: Literal["r", "rb"] = ...,
107+
*,
108+
format: Optional[int] = ...,
109+
check: Literal[-1] = ...,
110+
preset: None = ...,
111+
filters: Optional[_FilterChain] = ...,
112+
encoding: None = ...,
113+
errors: None = ...,
114+
newline: None = ...,
115+
) -> LZMAFile: ...
116+
@overload
117+
def open(
118+
filename: _PathOrFile,
119+
mode: _OPEN_BINARY_WRITING_MODE,
120+
*,
121+
format: Optional[int] = ...,
122+
check: int = ...,
123+
preset: Optional[int] = ...,
124+
filters: Optional[_FilterChain] = ...,
125+
encoding: None = ...,
126+
errors: None = ...,
127+
newline: None = ...,
128+
) -> LZMAFile: ...
129+
@overload
130+
def open(
131+
filename: _PathType,
132+
mode: Literal["rt"],
133+
*,
134+
format: Optional[int] = ...,
135+
check: Literal[-1] = ...,
136+
preset: None = ...,
137+
filters: Optional[_FilterChain] = ...,
138+
encoding: Optional[str] = ...,
139+
errors: Optional[str] = ...,
140+
newline: Optional[str] = ...,
141+
) -> TextIO: ...
142+
@overload
143+
def open(
144+
filename: _PathType,
145+
mode: _OPEN_TEXT_WRITING_MODE,
146+
*,
147+
format: Optional[int] = ...,
148+
check: int = ...,
149+
preset: Optional[int] = ...,
150+
filters: Optional[_FilterChain] = ...,
151+
encoding: Optional[str] = ...,
152+
errors: Optional[str] = ...,
153+
newline: Optional[str] = ...,
154+
) -> TextIO: ...
155+
@overload
156+
def open(
157+
filename: _PathOrFile,
158+
mode: str = ...,
159+
*,
160+
format: Optional[int] = ...,
161+
check: int = ...,
162+
preset: Optional[int] = ...,
163+
filters: Optional[_FilterChain] = ...,
164+
encoding: Optional[str] = ...,
165+
errors: Optional[str] = ...,
166+
newline: Optional[str] = ...,
167+
) -> Union[LZMAFile, TextIO]: ...
168+
def compress(
169+
data: bytes, format: int = ..., check: int = ..., preset: Optional[int] = ..., filters: Optional[_FilterChain] = ...
170+
) -> bytes: ...
105171
def decompress(data: bytes, format: int = ..., memlimit: Optional[int] = ..., filters: Optional[_FilterChain] = ...) -> bytes: ...
106172
def is_check_supported(check: int) -> bool: ...

0 commit comments

Comments
 (0)