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

Aperta Adatta ai principianti
#7,642 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
api, backend

Direzione di ricerca

Segui come viene gestito params durante la preparazione della richiesta, seguendo il percorso dell’issue attraverso la preparazione dell’URL fino a urllib.parse.urlunparse. Inizia dal codice di preparazione della richiesta e dai test esistenti per i parametri di query o gli input non validi. Aggiungi test per tipi non validi, come un intero, e conferma che l’errore risultante menzioni params e descriva i tipi accettati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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"
  }
}
Lingua principale
Python
Stelle
54.7k
Fork
10.8k
Merge medio
18m
PR unite (30g)
1

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di psf/requests

Tutte le issue di psf/requests

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.