`iter_lines(decode_unicode=True, delimiter=<str>)` raises `TypeError` when `Response.encoding` is `None`
まだ誰も着手していません。
- #1 @draarivpatel-ui による — マージされずにクローズ
評価
調査の方向性
障害は 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
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
psf/requests のほかの issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
-
get_encoding_from_headers treats any content type containing 'text' as text/* (ISO-8859-1)対応中かも @suyashgitt が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
Invalid `params` type (e.g. int) raises misleading "TypeError: Cannot mix str and non-str arguments"オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
OCA/account-invoicing#2529 ·
メンテナーはふだん 1 日以内に返信
-
area/ci
難易度 1/5 1時間未満 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
-
ImageGen generate-batch aborts valid JSONL jobs containing a Unicode line separator in poster textオープンbug imagen skills
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
Email validation accepts a trailing newline対応中かも @marcello-pichini が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信