|
1 |
| -import io |
| 1 | +import _compression |
2 | 2 | import sys
|
| 3 | +from _compression import BaseStream |
3 | 4 | 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 |
5 | 6 | from typing_extensions import Literal, SupportsIndex
|
6 | 7 |
|
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 | + |
8 | 19 | _T = TypeVar("_T")
|
9 | 20 |
|
10 | 21 | def compress(data: bytes, compresslevel: int = ...) -> bytes: ...
|
11 | 22 | def decompress(data: bytes) -> bytes: ...
|
12 | 23 |
|
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"] |
15 | 28 |
|
16 | 29 | @overload
|
17 | 30 | def open(
|
18 |
| - filename: _PathOrFile, |
19 |
| - mode: _OpenBinaryMode = ..., |
| 31 | + filename: _ReadableFileobj, |
| 32 | + mode: _ReadBinaryMode = ..., |
20 | 33 | compresslevel: int = ...,
|
21 | 34 | encoding: None = ...,
|
22 | 35 | errors: None = ...,
|
23 | 36 | newline: None = ...,
|
24 | 37 | ) -> BZ2File: ...
|
25 | 38 | @overload
|
26 | 39 | def open(
|
27 |
| - filename: StrOrBytesPath, |
28 |
| - mode: _OpenTextMode, |
| 40 | + filename: _ReadableFileobj, |
| 41 | + mode: _ReadTextMode, |
29 | 42 | compresslevel: int = ...,
|
30 | 43 | encoding: Optional[str] = ...,
|
31 | 44 | errors: Optional[str] = ...,
|
32 | 45 | newline: Optional[str] = ...,
|
33 | 46 | ) -> TextIO: ...
|
34 | 47 | @overload
|
35 | 48 | 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, |
38 | 60 | compresslevel: int = ...,
|
39 | 61 | encoding: Optional[str] = ...,
|
40 | 62 | errors: Optional[str] = ...,
|
41 | 63 | 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: ... |
43 | 83 |
|
44 |
| -class BZ2File(io.BufferedIOBase, IO[bytes]): |
| 84 | +class BZ2File(BaseStream, IO[bytes]): |
45 | 85 | def __enter__(self: _T) -> _T: ...
|
46 | 86 | 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: ... |
48 | 95 | 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 |
49 | 109 | 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 = ..., |
51 | 115 | ) -> None: ...
|
52 | 116 | def read(self, size: Optional[int] = ...) -> bytes: ...
|
53 | 117 | def read1(self, size: int = ...) -> bytes: ...
|
|
0 commit comments