mypy warns about invalid types for json argument
Author: hmoffattCreated May 14, 2026Updated Aug 16, 2026
I have warnings about invalid types for the json argument, that didn't occur when using types-requests.
The changes in v2.34.1 changed the errors, but did not fix them.
typecheck.py:6: error: Argument "json" to "post" has incompatible type "dict[str, Collection[str]]"; expected "JsonType" [arg-type]
typecheck.py:9: error: Argument "json" to "post" has incompatible type "dict[str, object]"; expected "JsonType" [arg-type]
Reproduction Steps
import requests
def fn(d: dict[str, str]) -> None:
j = {"foo": d, "bar": "hi"}
requests.post("https://example.com", json=j)
k = {"foo": d, "bool": True}
requests.post("https://example.com", json=k)
It seems ok if a literal is used without the intermediate variable:
requests.post("https://example.com", json={"foo": d, "bar": "hi"})
requests.post("https://example.com", json={"foo": d, "bool": True})
System Information
{
"chardet": {
"version": null
},
"charset_normalizer": {
"version": "3.4.7"
},
"cryptography": {
"version": ""
},
"idna": {
"version": "3.15"
},
"implementation": {
"name": "CPython",
"version": "3.13.5"
},
"platform": {
"release": "6.12.74+deb13+1-amd64",
"system": "Linux"
},
"pyOpenSSL": {
"openssl_version": "",
"version": null
},
"requests": {
"version": "2.34.1"
},
"system_ssl": {
"version": "30500050"
},
"urllib3": {
"version": "2.7.0"
},
"using_charset_normalizer": true,
"using_pyopenssl": false
}
Source: psf/requests