I have noticed that in the seemingly common situation of passing **kwargs to json.dump or json.dumps, mypy fails the type checks unless the kwargs are explicitly annotated as Dict[str, Any]. A simple example:
import json
json_kwargs = dict(indent=2)
json.dumps({}, **json_kwargs)
Which results in:
$ mypy test.py
test.py:6: error: Argument 2 to "dumps" has incompatible type "**Dict[str, int]"; expected "bool"
test.py:6: error: Argument 2 to "dumps" has incompatible type "**Dict[str, int]"; expected "Optional[Type[JSONEncoder]]"
test.py:6: error: Argument 2 to "dumps" has incompatible type "**Dict[str, int]"; expected "Optional[Tuple[str, str]]"
test.py:6: error: Argument 2 to "dumps" has incompatible type "**Dict[str, int]"; expected "Optional[Callable[[Any], Any]]"
Found 4 errors in 1 file (checked 1 source file)
If I change the example to this:
import json
json_kwargs = dict(indent=2, separators=(",", ";"))
json.dumps({}, **json_kwargs)
The errors are the same, except the inferred type changes to **Dict[str, object]. I can work around it like so:
import json
from typing import Any, Dict
json_kwargs : Dict[str, Any] = dict(indent=2, separators=(",", ";"))
json.dumps({}, **json_kwargs)
Using Python 3.8 and mypy == 0.770 on Arch Linux. The error is still present when using the current master (commit 2a3de7b).
I have also tested this with pytype and pytype does not have the same failure.
I have noticed that in the seemingly common situation of passing
**kwargstojson.dumporjson.dumps,mypyfails the type checks unless the kwargs are explicitly annotated asDict[str, Any]. A simple example:Which results in:
If I change the example to this:
The errors are the same, except the inferred type changes to
**Dict[str, object]. I can work around it like so:Using Python 3.8 and
mypy == 0.770on Arch Linux. The error is still present when using the current master (commit 2a3de7b).I have also tested this with
pytypeandpytypedoes not have the same failure.