get_encoding_from_headers treats any content type containing 'text' as text/* (ISO-8859-1)

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

@suyashgitt がすでに取り組んでいます。

2026年11月13日 から。

評価

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

調査の方向性

src/requests/utils.pyを読み、get_encoding_from_headersを調べてから、この関数を対象とするテストを探してください。application/vnd.context+xml、application/x-context、application/jsonxのメディアタイプで報告された動作を再現してください。実際のtext/*タイプだけがISO-8859-1を受け取り、一致しないタイプはNoneを返し、各ケースがテストでカバーされていれば完了です。

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

説明

Summary

requests.utils.get_encoding_from_headers uses substring checks on the media type, so non-text/* types that merely contain the letters "text" are treated as text and get ISO-8859-1.

In src/requests/utils.py:

if "text" in content_type:
    return "ISO-8859-1"

Repro

from requests.utils import get_encoding_from_headers
from requests.structures import CaseInsensitiveDict as C

get_encoding_from_headers(C({"content-type": "application/vnd.context+xml"}))  # 'ISO-8859-1'
get_encoding_from_headers(C({"content-type": "application/x-context"}))        # 'ISO-8859-1'

Neither is a text/* type, so Response.encoding should be None (letting content-based detection run), not Latin-1. The same substring pattern is used for "application/json" in content_type, which would also match e.g. application/jsonx.

Suggested fix

Compare against the parsed media type, e.g. content_type.startswith("text/") and content_type == "application/json". Happy to open a PR with tests.

Checked on current main; I found no existing issue covering this.

主要言語
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 を短くまとめたダイジェスト。