Ignore `str-format` error when object implements `__format__()` with custom type
オープン
まだ誰も着手していません。
bug
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Bug Report
I have inherited a project, where one class implements __format__(self, format_spec: str) -> str to implement a custom type for formatting. Running mypy on that code throws a false-positive error:
mypy-format.py:5 error: Unsupported format character "l" [str-format]
To Reproduce
class MyType:
def __format__(self, format_spec: str) -> str:
return "a" if format_spec == "l" else str(self)
print("{:l}".format(MyType()))
Expected Behavior
For a type implementing __format__ mypy/checkstrformat.py should not output any error.
Actual Behavior
mypy-format.py:5 error: Unsupported format character "l" [str-format]
Your Environment
- mypy 1.11.2 (compiled: yes)
- Python 3.11.2
Links
- #17714
- #11723
- How does type-checking work for
datetime.datetime, which implements a rich set of custom formatters? - string - Format Mini Language
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供された Python の再現コードを mypy 1.11.2 で実行し、次に str-format エラーが報告される mypy/checkstrformat.py を調べます。オブジェクトの format メソッドがどのように考慮されるかを追跡し、例で unsupported-format エラーが発生しないことを確認してください。再現コードで動作を確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 55/100