Skip to content

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

Closed

Description

@software-opal

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]"

Activity

  1. srittau commented on Nov 2, 2018

    @srittau
    Collaborator

    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.

  2. micimize commented on Oct 11, 2020

    @micimize

    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))
  3. srittau commented on Oct 12, 2020

    @srittau
    Collaborator

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions