Skip to content
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

Argument 1 to "load" has incompatible type "HTTPResponse"; expected IO[str] #3007

Closed
lithammer opened this issue Mar 15, 2017 · 2 comments
Closed

Comments

@lithammer
Copy link

Python v3.6
Mypy v0.501

# http.py
import http.client
import json

conn = http.client.HTTPConnection('example.com')
conn.request('GET', '/foobar.json')
response = conn.getresponse()
json.load(response)

Since response is a "file-like object" (i.e. has a .read() method). And json.load(fp, ...) operates on those according to the documentation ("Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object") I expect this to work.

$ mypy http.py
http.py:8: error: Argument 1 to "load" has incompatible type "HTTPResponse"; expected IO[str]
@JelleZijlstra
Copy link
Member

Mypy doesn't currently support structural typing, although there is an active proposal to add it (see python/typing#11 and python/peps#224). Once that's implemented we could add a Protocol that defines the read method, but until then I'm not sure there's any better workaround than using cast() or # type: ignore.

@ilevkivskyi
Copy link
Member

The original example now passes without errors, so I assume it was fixed in typeshed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants