`iter_lines(decode_unicode=True, delimiter=<str>)` raises `TypeError` when `Response.encoding` is `None`

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

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

  • #1 @draarivpatel-ui による — マージされずにクローズ

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
62/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
python
領域
api

調査の方向性

障害は src/requests/models.py の iter_lines にあります。Response.encoding が None のため chunk は bytes なのに、chunk.split(delimiter) には str の区切り文字が渡されています。Issue の再現スニペットを実行して TypeError を確認し、次に decode_unicode=True のとき iter_lines が chunk をどのようにデコードしているかを調べてください。呼び出しが str の行を返すか、わかりやすいエラーを送出すれば完了で、既存の iter_lines のテストの隣に テストを追加します。クローズされmarge されていない関連 pull request には、以前の試みが含まれているかもしれないので、先に読んでおく価値があります。

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

説明

Expected Result

Either return lines, or raise a clear error message

I spotted this while investigating whether Pyrefly could run on the Requests source code. (happy to make a PR to add Pyrefly if you'd be interested in it by the way, it may avert future issues like this one? looks like only collaboratos can make PRs, so maybe not)

Actual Result

Unexpected error message:

[b'line one', b'line two', b'line three']
Traceback (most recent call last):
  File "/home/mgorelli/scratch/t.py", line 15, in <module>
    print(list(make_response().iter_lines(decode_unicode=True, delimiter="\n")))
          ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mgorelli/scratch/.venv/lib/python3.14/site-packages/requests/models.py", line 1016, in iter_lines
    lines = chunk.split(delimiter)  # type: ignore[arg-type]
TypeError: a bytes-like object is required, not 'str'

Reproduction Steps

import io

import requests


def make_response():
    r = requests.models.Response()
    r.status_code = 200
    r.raw = io.BytesIO(b"line one\nline two\nline three")
    r.encoding = None  # e.g. a Content-Type with no charset
    return r


print(list(make_response().iter_lines(decode_unicode=True)))
print(list(make_response().iter_lines(decode_unicode=True, delimiter="\n")))

System Information

$ python -m requests.help
{
  "chardet": {
    "version": null
  },
  "charset_normalizer": {
    "version": "3.5.2"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "3.20"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.14.4"
  },
  "platform": {
    "release": "6.6.87.2-microsoft-standard-WSL2",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.34.2"
  },
  "system_ssl": {
    "version": "30500060"
  },
  "urllib3": {
    "version": "2.8.0"
  },
  "using_charset_normalizer": true,
  "using_pyopenssl": false
}
主要言語
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 を短くまとめたダイジェスト。