Skip to content

HTTPResponse is not recognized as a subtype of typing.IO #3108

Closed

Description

@pkch

From SO:

request = urllib.request.Request(get_file_url, headers=self.data_api_headers)

with urllib.request.urlopen(request) as response:
    with open(export_file_path, 'wb') as out_file:

        shutil.copyfileobj(response, out_file)

This fails with Argument 1 to "copyfileobj" has incompatible type "Union[HTTPResponse, BinaryIO]"; expected IO[Any] because HTTPResponse derives from io.BufferedIOBase, which (despite being a binary IO) does not derive from typing.BinaryIO (nor does io.RawIOBase; also io.IOBase does not derive from typing.IO).

When I attempted to derive them as I thought would make sense, I got errors because __enter__ and other methods are defined both in io.IOBase and in typing.BinaryIO.

Not sure how to solve it.

Activity

  1. ilevkivskyi commented on Apr 2, 2017

    @ilevkivskyi
    Member

    I think this kind of problems could be solved by protocols. I have encountered many similar issues recently. I will probably submit a PR today or tomorrow.

    (Note that extensive changes to typeshed may be required, replacing nominal types with corresponding protocols).

  2. JelleZijlstra commented on Apr 2, 2017

    @JelleZijlstra
    Member

    The IO types are especially complicated; there are lots of different "file-like" types with subtly different sets of methods. Maybe somebody will need to spend the time to carefully review all file-like types in the standard library to come up with a good set of protocols.

  3. added
    bugmypy got something wrong
    false-positivemypy gave an error on correct code
    on Jun 4, 2018
  4. emmatyping commented on Jun 4, 2018

    @emmatyping
    Member

    This came up again in python/typeshed#2188 (comment) it seems.

  5. JelleZijlstra commented on Jun 4, 2018

    @JelleZijlstra
    Member

    This seems like a typeshed bug, not a mypy bug.

  6. asottile commented on Jun 4, 2018

    @asottile
    Contributor

    I've copied this issue to typeshed: python/typeshed#2189

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

    bugmypy got something wrongfalse-positivemypy gave an error on correct code

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions