'gzip.GzipFile' appears to be missing 'IO[...]' #2580
Copy link
Copy link
Closed
Labels
stubs: false positiveType checkers report false errorsType checkers report false errorstopic: ioI/O related issuesI/O related issues
Description
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]"
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
stubs: false positiveType checkers report false errorsType checkers report false errorstopic: ioI/O related issuesI/O related issues
Activity
srittau commentedon 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.
micimize commentedon Oct 11, 2020
A comment in this related stackoverflow post references #4146 as either solving this or making it solvable.
Temporary workaround is to just
cast:srittau commentedon Oct 12, 2020
Closing this here. I suggest using protocols in your own code. If
GzipFileisn'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 derivingGzipFilefromIO, I'd be open to review such a PR, but no need to keep the issue open.