Skip to content

Replace an instance of IO with a protocol #5471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions stdlib/zipfile.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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 = ...
Expand All @@ -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]] = ...,
Expand All @@ -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]
Expand Down