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

'gzip.GzipFile' appears to be missing 'IO[...]' #2580

Closed
software-opal opened this issue Nov 2, 2018 · 3 comments
Closed

'gzip.GzipFile' appears to be missing 'IO[...]' #2580

software-opal opened this issue Nov 2, 2018 · 3 comments
Labels
stubs: false positive Type checkers report false errors topic: io I/O related issues

Comments

@software-opal
Copy link
Contributor

I'm not sure if this is a bug in MyPy or the typeshed, however tryngi to use a gzip.GzipFile where a file should be produces an error:

test.py

import gzip, typing
def my_test_writer(file: typing.IO[bytes]) : ...
my_test_writer(gzip.GzipFile("rb"))

Running mypy test.py (I ran from /tmp to ensure no config files were causing problems) gives:

test.py:4: error: Argument 1 to "my_test_writer" has incompatible type "GzipFile"; expected "IO[bytes]"
@srittau
Copy link
Collaborator

srittau commented Nov 2, 2018

Unfortunately, the IO classes are not in the best shape in typeshed. Our long-term plan is to change arguments requiring IO classes to use protocols. I recommend to do the same in custom code-bases, i.e. write protocols for functions and classes that take IO classes as arguments. See python/typing#564 for more information.

@srittau srittau added topic: io I/O related issues stubs: false positive Type checkers report false errors labels Nov 29, 2018
@micimize
Copy link

A comment in this related stackoverflow post references #4146 as either solving this or making it solvable.

Temporary workaround is to just cast:

    with gzip.open(input_file, "rb") as unzipped_file:
      return pickle.load(cast(IO[bytes], unzipped_file))

@srittau
Copy link
Collaborator

srittau commented Oct 12, 2020

Closing this here. I suggest using protocols in your own code. If GzipFile isn't accepted somewhere in the standard library, where it works, please file a separate bug report, or even better, submit a PR that changes the function in question to use protocols. If someone wants to experiment with deriving GzipFile from IO, I'd be open to review such a PR, but no need to keep the issue open.

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 topic: io I/O related issues
Projects
None yet
Development

No branches or pull requests

3 participants