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

オープン 初心者向け
#7,642 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
api, backend

調査の方向性

リクエストの準備中に params がどのように処理されるかを追い、issue が示す経路に沿って URL の準備から urllib.parse.urlunparse まで確認してください。リクエスト準備コードと、クエリパラメーターまたは無効な入力に関する既存のテストから始めてください。整数などの無効な型のテストを追加し、発生するエラーが params に言及し、受け付ける型を説明していることを確認してください。

索引モデルが issue の本文から書いたものです。

説明

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"
  }
}
主要言語
Python
スター
54.7k
フォーク
10.8k
平均マージ
18分
マージ済み PR(30日)
1

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

psf/requests のほかの issue

psf/requests の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。