Invalid `params` type (e.g. int) raises misleading "TypeError: Cannot mix str and non-str arguments"
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
Research direction
Trace how params is handled during request preparation, following the issue's path through URL preparation to urllib.parse.urlunparse. Start with the request preparation code and existing tests for query parameters or invalid inputs. Add coverage for invalid types such as an integer and confirm the resulting error names params and describes the accepted types.
Written by the indexing model from the issue text.
Description
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"
}
}
- Dominant language
- Python
- Stars
- 54.7k
- Forks
- 10.8k
- Avg merge
- 18m
- Merged PRs (30d)
- 1
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from psf/requests
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
-
`iter_lines(decode_unicode=True, delimiter=<str>)` raises `TypeError` when `Response.encoding` is `None`May be free again A pull request for this issue was closed without being merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
-
get_encoding_from_headers treats any content type containing 'text' as text/* (ISO-8859-1)Possibly taken @suyashgitt claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
OCA/account-invoicing#2529 ·
Maintainers usually reply within 1 day
-
area/ci
Difficulty 1/5 Under an hour Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
ImageGen generate-batch aborts valid JSONL jobs containing a Unicode line separator in poster textOpenbug imagen skills
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Email validation accepts a trailing newlinePossibly taken @marcello-pichini claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day