Skip to content

Commit de0c62f

Browse files
authored
zipfile: Replace an instance of IO with a protocol (#5471)
1 parent bce19fc commit de0c62f

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

stdlib/zipfile.pyi

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import io
22
import sys
33
from _typeshed import StrPath
44
from types import TracebackType
5-
from typing import IO, Any, Callable, Dict, Iterable, Iterator, List, Optional, Protocol, Sequence, Tuple, Type, Union
5+
from typing import IO, Callable, Dict, Iterable, Iterator, List, Optional, Protocol, Sequence, Tuple, Type, Union, overload
6+
from typing_extensions import Literal
67

78
_DateTuple = Tuple[int, int, int, int, int, int]
89

@@ -13,6 +14,16 @@ error = BadZipfile
1314

1415
class LargeZipFile(Exception): ...
1516

17+
class _ZipStream(Protocol):
18+
def read(self, __n: int) -> bytes: ...
19+
# The following methods are optional:
20+
# def seekable(self) -> bool: ...
21+
# def tell(self) -> int: ...
22+
# def seek(self, __n: int) -> object: ...
23+
24+
class _ClosableZipStream(_ZipStream, Protocol):
25+
def close(self) -> object: ...
26+
1627
class ZipExtFile(io.BufferedIOBase):
1728
MAX_N: int = ...
1829
MIN_READ_SIZE: int = ...
@@ -24,13 +35,48 @@ class ZipExtFile(io.BufferedIOBase):
2435
mode: str
2536
name: str
2637
if sys.version_info >= (3, 7):
38+
@overload
2739
def __init__(
28-
self, fileobj: IO[bytes], mode: str, zipinfo: ZipInfo, pwd: Optional[bytes] = ..., close_fileobj: bool = ...
40+
self, fileobj: _ClosableZipStream, mode: str, zipinfo: ZipInfo, pwd: Optional[bytes], close_fileobj: Literal[True]
41+
) -> None: ...
42+
@overload
43+
def __init__(
44+
self,
45+
fileobj: _ClosableZipStream,
46+
mode: str,
47+
zipinfo: ZipInfo,
48+
pwd: Optional[bytes] = ...,
49+
*,
50+
close_fileobj: Literal[True],
51+
) -> None: ...
52+
@overload
53+
def __init__(
54+
self, fileobj: _ZipStream, mode: str, zipinfo: ZipInfo, pwd: Optional[bytes] = ..., close_fileobj: bool = ...
2955
) -> None: ...
3056
else:
57+
@overload
58+
def __init__(
59+
self,
60+
fileobj: _ClosableZipStream,
61+
mode: str,
62+
zipinfo: ZipInfo,
63+
decrypter: Optional[Callable[[Sequence[int]], bytes]],
64+
close_fileobj: Literal[True],
65+
) -> None: ...
66+
@overload
67+
def __init__(
68+
self,
69+
fileobj: _ClosableZipStream,
70+
mode: str,
71+
zipinfo: ZipInfo,
72+
decrypter: Optional[Callable[[Sequence[int]], bytes]] = ...,
73+
*,
74+
close_fileobj: Literal[True],
75+
) -> None: ...
76+
@overload
3177
def __init__(
3278
self,
33-
fileobj: IO[bytes],
79+
fileobj: _ZipStream,
3480
mode: str,
3581
zipinfo: ZipInfo,
3682
decrypter: Optional[Callable[[Sequence[int]], bytes]] = ...,
@@ -43,7 +89,7 @@ class ZipExtFile(io.BufferedIOBase):
4389
def read1(self, n: Optional[int]) -> bytes: ... # type: ignore
4490

4591
class _Writer(Protocol):
46-
def write(self, __s: str) -> Any: ...
92+
def write(self, __s: str) -> object: ...
4793

4894
class ZipFile:
4995
filename: Optional[str]

0 commit comments

Comments
 (0)