Skip to content

Accurate return types for ZipFile.open() and zipfile.Path.open()#13069

Open
AT0myks wants to merge 4 commits into
python:mainfrom
AT0myks:zipfile-open
Open

Accurate return types for ZipFile.open() and zipfile.Path.open()#13069
AT0myks wants to merge 4 commits into
python:mainfrom
AT0myks:zipfile-open

Conversation

@AT0myks

@AT0myks AT0myks commented Nov 22, 2024

Copy link
Copy Markdown

An attempt to close #13051. Initially I used SizedBuffer for data in _ZipWriteFile.write() but this resulted in LSP violations.
For Python 3.8 _ReadWriteBinaryMode is replaced by _ReadWriteMode in zipfile.Path.open() because it did not yet accept modes rb and wb.
If I understand correctly, tests are failing because zipfile.Path.open(mode="rb") which now returns ZipExtFile does not comply anymore with importlib.abc.Traversable.open(mode="rb") which returns IO[bytes] and I'm not sure how this should be addressed.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@srittau

srittau commented Mar 6, 2025

Copy link
Copy Markdown
Collaborator

Sorry for the late reply. I've noticed that there are unnecessary version_info branches both in zipfile/__init__.pyi and zipfile/_path/__init__.pyi. This makes this PR larger than it needs to be. I'll try to remedy that in #13588. After merging it and updating the PR I will have a closer look. But this looks promising.

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

static-frame (https://github.com/static-frame/static-frame)
+ static_frame/core/archive_npy.py:349: error: Argument 1 to "to_npy" of "NPYConverter" has incompatible type "_ZipWriteFile | IO[bytes]"; expected "IO[bytes]"  [arg-type]
+ static_frame/core/archive_npy.py:356: error: Argument 1 to "from_npy" of "NPYConverter" has incompatible type "ZipExtFile | IO[bytes]"; expected "IO[bytes]"  [arg-type]
+ static_frame/core/archive_npy.py:367: error: Argument 1 to "header_from_npy" of "NPYConverter" has incompatible type "ZipExtFile | IO[bytes]"; expected "IO[bytes]"  [arg-type]
+ static_frame/core/archive_npy.py:552: error: Argument 1 to "to_npy" of "NPYConverter" has incompatible type "_ZipWriteFile"; expected "IO[bytes]"  [arg-type]
+ static_frame/core/archive_npy.py:560: error: Argument 1 to "from_npy" of "NPYConverter" has incompatible type "ZipExtFile"; expected "IO[bytes]"  [arg-type]
+ static_frame/core/archive_npy.py:572: error: Argument 1 to "header_from_npy" of "NPYConverter" has incompatible type "ZipExtFile"; expected "IO[bytes]"  [arg-type]

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, as I'm on a personal crusade to eliminate IO from typeshed, I very much welcome any PR working towards this goal. Comments below.

Comment on lines +237 to +239
@overload
def open(
self, name: str | ZipInfo, mode: Literal["w"] = ..., pwd: bytes | None = None, *, force_zip64: bool = False

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

Comment on lines +241 to 244
@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]: ...

Copy link
Copy Markdown
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.

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

Copy link
Copy Markdown
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.

Comment on lines +358 to +359
@overload
def open(self, mode: Literal["w"] = ..., pwd: bytes | None = None, *, force_zip64: bool = False) -> _ZipWriteFile: ...

Copy link
Copy Markdown
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: ...

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
Copy Markdown
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: ...

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
Copy Markdown
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: ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ZipFile.open() return type

2 participants