Header access methods in EmailMessage return strings, not header objects

オープン
#101,090 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
42/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
python

調査の方向性

issueで説明されているEmailMessageのヘッダーアクセスメソッドと、Message基底クラスのget_param()のドキュメントから始めます。ドキュメントに記載されたparamsベースのアクセスと、現在の文字列の戻り値を比較し、その後、関連するパラメーター処理の動作を調査します。選択したAPIの動作がコード、ドキュメント、テストで一貫していれば完了とします。

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

説明

stdlib topic-email type-bug

Summary

The behavior of the EmailMessage class does not match what the documentation says it will do when code using the class tries to fetch a header parameter value.

The class has a get_param() method, but that's defined in a base class, and the documentation says that the base class (Message) should only be used when maintaining legacy code. Instead, we're told:

Note that "existing parameter values of headers may be accessed through the params attribute of the header value (for example, msg['Content-Type'].params['charset']).

The problem is that msg['Content-Type'] returns a string, not a header object, which Python correctly complains "has no attribute 'params'."

It's not obvious (to me, anyway) why set_param() (also defined in the same base class) is OK to use, but not get_param() (which, by the way, actually works, in contrast with the approach given in the documentation). The docstring for get_param() (which I used to think of as the "canonical documentation" until one of the core devs gently set me straight) gives no indication that using get_param() is frowned upon, but the really canonical documentation says

This is a legacy method. On the EmailMessage class its functionality is replaced by the params property of the individual header objects returned by the header access methods.

I suppose this ticket could go either way: a bug in the code (which should be returning objects instead of strings for the header access methods) or a bug in the documentation. I went with the first option, since if the documentation is wrong and the header access methods really are supposed to return strings, then something will still need to be done with the code to expose the needed functionality. There's a comment on _get_params_preserve() (which get_params() and get_param() both use to do their thing) asking if it should be part of the public interface, so that would be one solution (though I'd still want to know why get_param() shouldn't be used).

Environment

  • Python 3.11.1 (v3.11.1:a7a450f84a, Dec 6 2022, 15:24:06) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin

Context

I ended up in this module when I noticed that the cgi module has been deprecated.

Deprecated since version 3.11, will be removed in version 3.13: The cgi module is deprecated (see PEP 594 for details and alternatives).

The FieldStorage class can typically be replaced with urllib.parse.parse_qsl() for GET and HEAD requests, and the email.message module or multipart for POST and PUT. Most utility functions have replacements.

That's about it for guidance on how to make the transition. I feel a little bit like as if I'm being thrown back to my experiences as a young dad trying to assemble the toys on Christmas Eve with instructions written in a foreign language. 😅

主要言語
Python
スター
77.2k
フォーク
36k
平均マージ
1日 9時間
マージ済み PR(30日)
558

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

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

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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