-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Accurate return types for ZipFile.open()
and zipfile.Path.open()
#13069
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
base: main
Are you sure you want to change the base?
Changes from all commits
08d210b
8463faf
3bf08ba
0894059
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,6 @@ | ||||||||||
import io | ||||||||||
import sys | ||||||||||
from _typeshed import SizedBuffer, StrOrBytesPath, StrPath | ||||||||||
from _typeshed import ReadableBuffer, SizedBuffer, StrOrBytesPath, StrPath | ||||||||||
from collections.abc import Callable, Iterable, Iterator | ||||||||||
from io import TextIOWrapper | ||||||||||
from os import PathLike | ||||||||||
|
@@ -30,7 +30,6 @@ _DateTuple = tuple[int, int, int, int, int, int] # noqa: Y026 | |||||||||
_ZipFileMode = Literal["r", "w", "x", "a"] # noqa: Y026 | ||||||||||
|
||||||||||
_ReadWriteMode: TypeAlias = Literal["r", "w"] | ||||||||||
_ReadWriteBinaryMode: TypeAlias = Literal["r", "w", "rb", "wb"] | ||||||||||
|
||||||||||
class BadZipFile(Exception): ... | ||||||||||
|
||||||||||
|
@@ -91,6 +90,12 @@ class ZipExtFile(io.BufferedIOBase): | |||||||||
def read1(self, n: int | None) -> bytes: ... # type: ignore[override] | ||||||||||
def seek(self, offset: int, whence: int = 0) -> int: ... | ||||||||||
|
||||||||||
class _ZipWriteFile(io.BufferedIOBase): # undocumented | ||||||||||
def __init__(self, zf: ZipFile, zinfo: ZipInfo, zip64: bool) -> None: ... | ||||||||||
def writable(self) -> Literal[True]: ... | ||||||||||
def write(self, data: ReadableBuffer) -> int: ... | ||||||||||
def close(self) -> None: ... | ||||||||||
|
||||||||||
class _Writer(Protocol): | ||||||||||
def write(self, s: str, /) -> object: ... | ||||||||||
|
||||||||||
|
@@ -225,8 +230,17 @@ class ZipFile: | |||||||||
def getinfo(self, name: str) -> ZipInfo: ... | ||||||||||
def infolist(self) -> list[ZipInfo]: ... | ||||||||||
def namelist(self) -> list[str]: ... | ||||||||||
@overload | ||||||||||
def open( | ||||||||||
self, name: str | ZipInfo, mode: Literal["r"] = "r", pwd: bytes | None = None, *, force_zip64: bool = False | ||||||||||
) -> ZipExtFile: ... | ||||||||||
@overload | ||||||||||
def open( | ||||||||||
self, name: str | ZipInfo, mode: Literal["w"] = ..., pwd: bytes | None = None, *, force_zip64: bool = False | ||||||||||
) -> _ZipWriteFile: ... | ||||||||||
@overload | ||||||||||
def open( | ||||||||||
self, name: str | ZipInfo, mode: _ReadWriteMode = "r", pwd: bytes | None = None, *, force_zip64: bool = False | ||||||||||
self, name: str | ZipInfo, mode: _ReadWriteMode, pwd: bytes | None = None, *, force_zip64: bool = False | ||||||||||
) -> IO[bytes]: ... | ||||||||||
Comment on lines
+241
to
244
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to leave out this "fallback" overload. If this causes too much trouble, mypy_primer in our CI should warn us. |
||||||||||
def extract(self, member: str | ZipInfo, path: StrPath | None = None, pwd: bytes | None = None) -> str: ... | ||||||||||
def extractall( | ||||||||||
|
@@ -335,11 +349,16 @@ else: | |||||||||
pwd: bytes | None = None, | ||||||||||
) -> TextIOWrapper: ... | ||||||||||
@overload | ||||||||||
def open(self, mode: Literal["rb", "wb"], *, pwd: bytes | None = None) -> IO[bytes]: ... | ||||||||||
def open(self, mode: Literal["rb"], *, pwd: bytes | None = None) -> ZipExtFile: ... | ||||||||||
@overload | ||||||||||
def open(self, mode: Literal["wb"], *, pwd: bytes | None = None) -> _ZipWriteFile: ... | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above:
Suggested change
|
||||||||||
else: | ||||||||||
def open( | ||||||||||
self, mode: _ReadWriteBinaryMode = "r", pwd: bytes | None = None, *, force_zip64: bool = False | ||||||||||
) -> IO[bytes]: ... | ||||||||||
@overload | ||||||||||
def open(self, mode: Literal["r"] = "r", pwd: bytes | None = None, *, force_zip64: bool = False) -> ZipExtFile: ... | ||||||||||
@overload | ||||||||||
def open(self, mode: Literal["w"] = ..., pwd: bytes | None = None, *, force_zip64: bool = False) -> _ZipWriteFile: ... | ||||||||||
Comment on lines
+358
to
+359
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above:
Suggested change
|
||||||||||
@overload | ||||||||||
def open(self, mode: _ReadWriteMode, pwd: bytes | None = None, *, force_zip64: bool = False) -> IO[bytes]: ... | ||||||||||
Comment on lines
+360
to
+361
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above: I would skip this overload. |
||||||||||
|
||||||||||
if sys.version_info >= (3, 10): | ||||||||||
def iterdir(self) -> Iterator[Self]: ... | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -58,7 +58,9 @@ | |||||
pwd: bytes | None = None, | ||||||
) -> TextIOWrapper: ... | ||||||
@overload | ||||||
def open(self, mode: Literal["rb", "wb"], *, pwd: bytes | None = None) -> IO[bytes]: ... | ||||||
def open(self, mode: Literal["rb"], *, pwd: bytes | None = None) -> ZipExtFile: ... | ||||||
@overload | ||||||
def open(self, mode: Literal["wb"], *, pwd: bytes | None = None) -> _ZipWriteFile: ... | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
def iterdir(self) -> Iterator[Self]: ... | ||||||
def is_dir(self) -> bool: ... | ||||||
def is_file(self) -> bool: ... | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would imply that this overload would also apply if no
mode
argument is given, which obviously isn't true. Also, sincepwd
is only allowed when reading files: