Invalid `params` type (e.g. int) raises misleading "TypeError: Cannot mix str and non-str arguments"
The error raised when params has an invalid type (for example an int) is misleading: TypeError: Cannot mix str and non-str arguments comes from urllib.parse and doesn't mention params, so it is hard to tell what the caller did wrong.
Expected Result
An error that names the invalid argument, for example:
TypeError: params must be a dict, a list of tuples, or a str/bytes, not int
I'm not asking for new functionality, only that the existing error identify the invalid argument.
Actual Result
TypeError: Cannot mix str and non-str arguments
The error is raised from urllib.parse._coerce_args, called from urlunparse while the URL is being prepared, so nothing in the message points back to params. A similar report, #2844 (params=b"..."), was treated as a bug and fixed in 2015. This report is about other invalid types.
I ran this in a browser-based Python (Emscripten/Pyodide), but the error is raised from urllib.parse, which is pure Python, so I'd expect it on any platform.
Reproduction Steps
import requests
print(requests.__version__)
requests.Request("GET", "http://example.com/path", params=5).prepare().url
Reproduced on requests 2.31.0 with params=5. The same TypeError also occurs with params=3.14, params=True and params=object() (these three were tested on requests 2.33.1 on Linux, Python 3.12).
System Information
$ python -m requests.help
{
"platform": {
"system": "Emscripten",
"release": "3.1.58"
},
"implementation": {
"name": "CPython",
"version": "3.12.7"
},
"system_ssl": {
"version": "1010117f"
},
"using_pyopenssl": false,
"using_charset_normalizer": true,
"pyOpenSSL": {
"version": null,
"openssl_version": ""
},
"urllib3": {
"version": "2.2.3"
},
"chardet": {
"version": null
},
"charset_normalizer": {
"version": "3.3.2"
},
"cryptography": {
"version": ""
},
"idna": {
"version": "3.7"
},
"requests": {
"version": "2.31.0"
}
}
Here is the cause and a fix for this:
Cause
In prepare_url (src/requests/models.py), when params is not None and not str or bytes, self._encode_params(params) is called.
In _encode_params:
- It checks
isinstance(data, (str, bytes)) - It checks
_t.has_read(data) - It checks
hasattr(data, "__iter__") - In the
else:branch, it returnsdatauntouched (# unreachable for valid _t.DataType).
When an invalid scalar type like 5, 3.14, True, or an arbitrary object is passed, _encode_params returns it directly. It is then placed into query and passed to urlunparse(). Inside urllib.parse._coerce_args, it raises:
TypeError: Cannot mix str and non-str arguments which comes from urllib.parse without identifying params.
Fix
In prepare_url, validate that non-None params is either str, bytes, or an iterable/mapping (hasattr(params, "__iter__")), raising an informative TypeError naming the parameter:
if params is not None:
if not isinstance(params, (str, bytes)) and not hasattr(params, "__iter__"):
raise TypeError(
f"params must be a dict, a list of tuples, or a str/bytes, not {type(params).__name__}"
)
enc_params = self._encode_params(params)
I've tested this with unit tests on branch: https://github.com/X6M-Work/requests/tree/fix-invalid-params-type-7642
Full diff comparison against main: https://github.com/psf/requests/compare/main...X6M-Work:requests:fix-invalid-params-type-7642
(Since PR creation is restricted to collaborators on this repository, feel free to cherry-pick from the branch or let me know if you'd like an invite to submit the PR directly!)