diff --git a/stdlib/zipfile.pyi b/stdlib/zipfile.pyi index 23d5e3b45d31..619c467f445b 100644 --- a/stdlib/zipfile.pyi +++ b/stdlib/zipfile.pyi @@ -2,7 +2,8 @@ import io import sys from _typeshed import StrPath from types import TracebackType -from typing import IO, Any, Callable, Dict, Iterable, Iterator, List, Optional, Protocol, Sequence, Tuple, Type, Union +from typing import IO, Callable, Dict, Iterable, Iterator, List, Optional, Protocol, Sequence, Tuple, Type, Union, overload +from typing_extensions import Literal _DateTuple = Tuple[int, int, int, int, int, int] @@ -13,6 +14,16 @@ error = BadZipfile class LargeZipFile(Exception): ... +class _ZipStream(Protocol): + def read(self, __n: int) -> bytes: ... + # The following methods are optional: + # def seekable(self) -> bool: ... + # def tell(self) -> int: ... + # def seek(self, __n: int) -> object: ... + +class _ClosableZipStream(_ZipStream, Protocol): + def close(self) -> object: ... + class ZipExtFile(io.BufferedIOBase): MAX_N: int = ... MIN_READ_SIZE: int = ... @@ -24,13 +35,48 @@ class ZipExtFile(io.BufferedIOBase): mode: str name: str if sys.version_info >= (3, 7): + @overload def __init__( - self, fileobj: IO[bytes], mode: str, zipinfo: ZipInfo, pwd: Optional[bytes] = ..., close_fileobj: bool = ... + self, fileobj: _ClosableZipStream, mode: str, zipinfo: ZipInfo, pwd: Optional[bytes], close_fileobj: Literal[True] + ) -> None: ... + @overload + def __init__( + self, + fileobj: _ClosableZipStream, + mode: str, + zipinfo: ZipInfo, + pwd: Optional[bytes] = ..., + *, + close_fileobj: Literal[True], + ) -> None: ... + @overload + def __init__( + self, fileobj: _ZipStream, mode: str, zipinfo: ZipInfo, pwd: Optional[bytes] = ..., close_fileobj: bool = ... ) -> None: ... else: + @overload + def __init__( + self, + fileobj: _ClosableZipStream, + mode: str, + zipinfo: ZipInfo, + decrypter: Optional[Callable[[Sequence[int]], bytes]], + close_fileobj: Literal[True], + ) -> None: ... + @overload + def __init__( + self, + fileobj: _ClosableZipStream, + mode: str, + zipinfo: ZipInfo, + decrypter: Optional[Callable[[Sequence[int]], bytes]] = ..., + *, + close_fileobj: Literal[True], + ) -> None: ... + @overload def __init__( self, - fileobj: IO[bytes], + fileobj: _ZipStream, mode: str, zipinfo: ZipInfo, decrypter: Optional[Callable[[Sequence[int]], bytes]] = ..., @@ -43,7 +89,7 @@ class ZipExtFile(io.BufferedIOBase): def read1(self, n: Optional[int]) -> bytes: ... # type: ignore class _Writer(Protocol): - def write(self, __s: str) -> Any: ... + def write(self, __s: str) -> object: ... class ZipFile: filename: Optional[str]