Skip to content

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
33 changes: 26 additions & 7 deletions stdlib/zipfile/__init__.pyi
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
Expand Down Expand Up @@ -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): ...

Expand Down Expand Up @@ -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: ...

Expand Down Expand Up @@ -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
Comment on lines +237 to +239
Copy link
Collaborator

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, since pwd is only allowed when reading files:

Suggested change
@overload
def open(
self, name: str | ZipInfo, mode: Literal["w"] = ..., pwd: bytes | None = None, *, force_zip64: bool = False
@overload
def open(
self, name: str | ZipInfo, mode: Literal["w"], pwd: 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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(
Expand Down Expand Up @@ -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: ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above:

Suggested change
def open(self, mode: Literal["wb"], *, pwd: bytes | None = None) -> _ZipWriteFile: ...
def open(self, mode: Literal["wb"], *, pwd: None = None) -> _ZipWriteFile: ...

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above:

Suggested change
@overload
def open(self, mode: Literal["w"] = ..., pwd: bytes | None = None, *, force_zip64: bool = False) -> _ZipWriteFile: ...
@overload
def open(self, mode: Literal["w"], pwd: None = None, *, force_zip64: bool = False) -> _ZipWriteFile: ...

@overload
def open(self, mode: _ReadWriteMode, pwd: bytes | None = None, *, force_zip64: bool = False) -> IO[bytes]: ...
Comment on lines +360 to +361
Copy link
Collaborator

Choose a reason for hiding this comment

The 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]: ...
Expand Down
4 changes: 3 additions & 1 deletion stdlib/zipfile/_path/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...

Check failure on line 61 in stdlib/zipfile/_path/__init__.pyi

View workflow job for this annotation

GitHub Actions / Test typeshed with pyright (Linux, 3.12)

"ZipExtFile" is not defined (reportUndefinedVariable)
@overload
def open(self, mode: Literal["wb"], *, pwd: bytes | None = None) -> _ZipWriteFile: ...

Check failure on line 63 in stdlib/zipfile/_path/__init__.pyi

View workflow job for this annotation

GitHub Actions / Test typeshed with pyright (Linux, 3.12)

"_ZipWriteFile" is not defined (reportUndefinedVariable)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def open(self, mode: Literal["wb"], *, pwd: bytes | None = None) -> _ZipWriteFile: ...
def open(self, mode: Literal["wb"], *, pwd: None = None) -> _ZipWriteFile: ...

def iterdir(self) -> Iterator[Self]: ...
def is_dir(self) -> bool: ...
def is_file(self) -> bool: ...
Expand Down
Loading