You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HTTPResponse is not recognized as a subtype of typing.IO #3108
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.
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).
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.
Activity
ilevkivskyi commentedon Apr 2, 2017
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 commentedon Apr 2, 2017
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 commentedon Jun 4, 2018
This came up again in python/typeshed#2188 (comment) it seems.
JelleZijlstra commentedon Jun 4, 2018
This seems like a typeshed bug, not a mypy bug.
asottile commentedon Jun 4, 2018
I've copied this issue to typeshed: python/typeshed#2189