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

Đang mở Phù hợp với người mới
#7,642 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
72/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
api, backend

Hướng nghiên cứu

Theo dõi cách params được xử lý trong quá trình chuẩn bị yêu cầu, lần theo đường đi của issue qua bước chuẩn bị URL đến urllib.parse.urlunparse. Bắt đầu với mã chuẩn bị yêu cầu và các bài kiểm thử hiện có cho tham số truy vấn hoặc đầu vào không hợp lệ. Bổ sung kiểm thử cho các kiểu không hợp lệ như số nguyên, đồng thời xác nhận lỗi phát sinh có nhắc đến params và mô tả các kiểu được chấp nhận.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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"
  }
}
Ngôn ngữ chính
Python
Star
54.7k
Fork
10.8k
Merge trung bình
18 phút
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của psf/requests

Tất cả issue của psf/requests

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.