Invalid `params` type (e.g. int) raises misleading "TypeError: Cannot mix str and non-str arguments"

Open Beginner friendly
#7,642 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
api, backend

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from psf/requests

All issues in psf/requests

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.