Skip to content
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

Return type of zipfile.Path.open is not correct when using non-binary mode #10847

Closed
FeldrinH opened this issue Oct 6, 2023 · 1 comment · Fixed by #10861
Closed

Return type of zipfile.Path.open is not correct when using non-binary mode #10847

FeldrinH opened this issue Oct 6, 2023 · 1 comment · Fixed by #10861
Labels
stubs: false positive Type checkers report false errors

Comments

@FeldrinH
Copy link

FeldrinH commented Oct 6, 2023

Example:

import zipfile

zip_path = zipfile.Path('example.zip', at='example.txt')
with zip_path.open(mode='r') as f:
    content = f.read()
    assert isinstance(content, str)

In this example the code functions as expected, and prints <class 'str'>, but the inferred type for f is IO[bytes] and the inferred type for content is consequently bytes, which is incorrect. The signature of zipfile.Path.open seems to assume a binary mode no matter what the mode argument actually is.

Python version: 3.10.11

PS: I am aware of #9966. I believe that that is a separate issue, because the issue there is about zipfile.Path.open arguments, whereas this issue is about the return value.

@srittau srittau added the stubs: false positive Type checkers report false errors label Oct 8, 2023
srittau added a commit to srittau/typeshed that referenced this issue Oct 8, 2023
* Add `CompleteDirs` and `FastLookup` (Python 3.8+).
* Add `Path.root` (Python 3.8+).
* Use concrete signature for `Path.open()` (Python 3.8).
* Fix signature of `Path.open()` for Python 3.9+:
   + Add overloads for text and binary modes with fixed return types.
   + Disallow extra arguments for binary modes.
   + Replace `*args` and `**kwargs` with accepted arguments for
     text modes.

Closes: python#10847
@srittau
Copy link
Collaborator

srittau commented Oct 8, 2023

Thanks for the report. Our Path.open() signature was broken in several ways. I opened #10861 to fix it.

JelleZijlstra pushed a commit that referenced this issue Oct 8, 2023
* Add `CompleteDirs` and `FastLookup` (Python 3.8+).
* Add `Path.root` (Python 3.8+).
* Use concrete signature for `Path.open()` (Python 3.8).
* Fix signature of `Path.open()` for Python 3.9+:
   + Add overloads for text and binary modes with fixed return types.
   + Disallow extra arguments for binary modes.
   + Replace `*args` and `**kwargs` with accepted arguments for
     text modes.

Closes: #10847
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: false positive Type checkers report false errors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants