Skip to content

Commit f562713

Browse files
authored
Use protocols instead of IO for bz2 (#5499)
1 parent 04fb7ce commit f562713

File tree

2 files changed

+107
-17
lines changed

2 files changed

+107
-17
lines changed

stdlib/bz2.pyi

Lines changed: 79 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,117 @@
1-
import io
1+
import _compression
22
import sys
3+
from _compression import BaseStream
34
from _typeshed import ReadableBuffer, StrOrBytesPath, WriteableBuffer
4-
from typing import IO, Any, Iterable, List, Optional, TextIO, TypeVar, Union, overload
5+
from typing import IO, Any, Iterable, List, Optional, Protocol, TextIO, TypeVar, Union, overload
56
from typing_extensions import Literal, SupportsIndex
67

7-
_PathOrFile = Union[StrOrBytesPath, IO[bytes]]
8+
# The following attributes and methods are optional:
9+
# def fileno(self) -> int: ...
10+
# def close(self) -> object: ...
11+
class _ReadableFileobj(_compression._Reader, Protocol): ...
12+
13+
class _WritableFileobj(Protocol):
14+
def write(self, __b: bytes) -> object: ...
15+
# The following attributes and methods are optional:
16+
# def fileno(self) -> int: ...
17+
# def close(self) -> object: ...
18+
819
_T = TypeVar("_T")
920

1021
def compress(data: bytes, compresslevel: int = ...) -> bytes: ...
1122
def decompress(data: bytes) -> bytes: ...
1223

13-
_OpenBinaryMode = Literal["r", "rb", "w", "wb", "x", "xb", "a", "ab"]
14-
_OpenTextMode = Literal["rt", "wt", "xt", "at"]
24+
_ReadBinaryMode = Literal["", "r", "rb"]
25+
_WriteBinaryMode = Literal["w", "wb", "x", "xb", "a", "ab"]
26+
_ReadTextMode = Literal["rt"]
27+
_WriteTextMode = Literal["wt", "xt", "at"]
1528

1629
@overload
1730
def open(
18-
filename: _PathOrFile,
19-
mode: _OpenBinaryMode = ...,
31+
filename: _ReadableFileobj,
32+
mode: _ReadBinaryMode = ...,
2033
compresslevel: int = ...,
2134
encoding: None = ...,
2235
errors: None = ...,
2336
newline: None = ...,
2437
) -> BZ2File: ...
2538
@overload
2639
def open(
27-
filename: StrOrBytesPath,
28-
mode: _OpenTextMode,
40+
filename: _ReadableFileobj,
41+
mode: _ReadTextMode,
2942
compresslevel: int = ...,
3043
encoding: Optional[str] = ...,
3144
errors: Optional[str] = ...,
3245
newline: Optional[str] = ...,
3346
) -> TextIO: ...
3447
@overload
3548
def open(
36-
filename: _PathOrFile,
37-
mode: str,
49+
filename: _WritableFileobj,
50+
mode: _WriteBinaryMode,
51+
compresslevel: int = ...,
52+
encoding: None = ...,
53+
errors: None = ...,
54+
newline: None = ...,
55+
) -> BZ2File: ...
56+
@overload
57+
def open(
58+
filename: _WritableFileobj,
59+
mode: _WriteTextMode,
3860
compresslevel: int = ...,
3961
encoding: Optional[str] = ...,
4062
errors: Optional[str] = ...,
4163
newline: Optional[str] = ...,
42-
) -> Union[BZ2File, TextIO]: ...
64+
) -> TextIO: ...
65+
@overload
66+
def open(
67+
filename: StrOrBytesPath,
68+
mode: Union[_ReadBinaryMode, _WriteBinaryMode] = ...,
69+
compresslevel: int = ...,
70+
encoding: None = ...,
71+
errors: None = ...,
72+
newline: None = ...,
73+
) -> BZ2File: ...
74+
@overload
75+
def open(
76+
filename: StrOrBytesPath,
77+
mode: Union[_ReadTextMode, _WriteTextMode],
78+
compresslevel: int = ...,
79+
encoding: Optional[str] = ...,
80+
errors: Optional[str] = ...,
81+
newline: Optional[str] = ...,
82+
) -> TextIO: ...
4383

44-
class BZ2File(io.BufferedIOBase, IO[bytes]):
84+
class BZ2File(BaseStream, IO[bytes]):
4585
def __enter__(self: _T) -> _T: ...
4686
if sys.version_info >= (3, 9):
47-
def __init__(self, filename: _PathOrFile, mode: str = ..., *, compresslevel: int = ...) -> None: ...
87+
@overload
88+
def __init__(self, filename: _WritableFileobj, mode: _WriteBinaryMode, *, compresslevel: int = ...) -> None: ...
89+
@overload
90+
def __init__(self, filename: _ReadableFileobj, mode: _ReadBinaryMode = ..., *, compresslevel: int = ...) -> None: ...
91+
@overload
92+
def __init__(
93+
self, filename: StrOrBytesPath, mode: Union[_ReadBinaryMode, _WriteBinaryMode] = ..., *, compresslevel: int = ...
94+
) -> None: ...
4895
else:
96+
@overload
97+
def __init__(
98+
self, filename: _WritableFileobj, mode: _WriteBinaryMode, buffering: Optional[Any] = ..., compresslevel: int = ...
99+
) -> None: ...
100+
@overload
101+
def __init__(
102+
self,
103+
filename: _ReadableFileobj,
104+
mode: _ReadBinaryMode = ...,
105+
buffering: Optional[Any] = ...,
106+
compresslevel: int = ...,
107+
) -> None: ...
108+
@overload
49109
def __init__(
50-
self, filename: _PathOrFile, mode: str = ..., buffering: Optional[Any] = ..., compresslevel: int = ...
110+
self,
111+
filename: StrOrBytesPath,
112+
mode: Union[_ReadBinaryMode, _WriteBinaryMode] = ...,
113+
buffering: Optional[Any] = ...,
114+
compresslevel: int = ...,
51115
) -> None: ...
52116
def read(self, size: Optional[int] = ...) -> bytes: ...
53117
def read1(self, size: int = ...) -> bytes: ...

stdlib/tarfile.pyi

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import bz2
12
import io
23
import sys
34
from _typeshed import StrOrBytesPath, StrPath
@@ -17,6 +18,12 @@ class _Fileobj(Protocol):
1718
# name: str | bytes
1819
# mode: Literal["rb", "r+b", "wb", "xb"]
1920

21+
class _Bz2ReadableFileobj(bz2._ReadableFileobj):
22+
def close(self) -> object: ...
23+
24+
class _Bz2WritableFileobj(bz2._WritableFileobj):
25+
def close(self) -> object: ...
26+
2027
# tar constants
2128
NUL: bytes
2229
BLOCKSIZE: int
@@ -191,12 +198,31 @@ class TarFile:
191198
debug: Optional[int] = ...,
192199
errorlevel: Optional[int] = ...,
193200
) -> TarFile: ...
201+
@overload
194202
@classmethod
195203
def bz2open(
196204
cls,
197205
name: Optional[StrOrBytesPath],
198-
mode: Literal["r", "w", "x"] = ...,
199-
fileobj: Optional[IO[bytes]] = ...,
206+
mode: Literal["w", "x"],
207+
fileobj: Optional[_Bz2WritableFileobj] = ...,
208+
compresslevel: int = ...,
209+
*,
210+
format: Optional[int] = ...,
211+
tarinfo: Optional[Type[TarInfo]] = ...,
212+
dereference: Optional[bool] = ...,
213+
ignore_zeros: Optional[bool] = ...,
214+
encoding: Optional[str] = ...,
215+
pax_headers: Optional[Mapping[str, str]] = ...,
216+
debug: Optional[int] = ...,
217+
errorlevel: Optional[int] = ...,
218+
) -> TarFile: ...
219+
@overload
220+
@classmethod
221+
def bz2open(
222+
cls,
223+
name: Optional[StrOrBytesPath],
224+
mode: Literal["r"] = ...,
225+
fileobj: Optional[_Bz2ReadableFileobj] = ...,
200226
compresslevel: int = ...,
201227
*,
202228
format: Optional[int] = ...,

0 commit comments

Comments
 (0)