Mypy: False warning when passing parameters to requests

Created on 14 Apr 2017  路  1Comment  路  Source: python/mypy

Here's an example:

import requests

params = {
    'something': 'cool',
    'a number': 1,
}

res = requests.get('http://www.google.com', params)
print(res.status_code, res.text)

And here's the output:

test_requests.py:8: error: Argument 2 has incompatible type Dict[str, object]; expected union type (7 items)

Most helpful comment

Thanks for reporting this!
This is related to how mypy infers types. It (on purpose) tries to be naive. You could use an explicit annotation in such cases to specify a more precise type:

params: Dict[str, Union[int, str]] = {
    'something': 'cool',
    'a number': 1,
}

I just tried it on master, and it works with the above annotation.

>All comments

Thanks for reporting this!
This is related to how mypy infers types. It (on purpose) tries to be naive. You could use an explicit annotation in such cases to specify a more precise type:

params: Dict[str, Union[int, str]] = {
    'something': 'cool',
    'a number': 1,
}

I just tried it on master, and it works with the above annotation.

Was this page helpful?
0 / 5 - 0 ratings