Skip to content

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

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

Closed
pkch opened this issue Apr 2, 2017 · 5 comments
Closed

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

pkch opened this issue Apr 2, 2017 · 5 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code

Comments

@pkch
Copy link
Contributor

pkch commented Apr 2, 2017

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.

@ilevkivskyi
Copy link
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).

@JelleZijlstra
Copy link
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.

@emmatyping emmatyping added bug mypy got something wrong false-positive mypy gave an error on correct code labels Jun 4, 2018
@emmatyping
Copy link
Member

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

@JelleZijlstra
Copy link
Member

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

@asottile
Copy link
Contributor

asottile commented Jun 4, 2018

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
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code
Projects
None yet
Development

No branches or pull requests

5 participants