python / python/cpython

strptime: use %r in error messages, not %s

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

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

stdlib type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:

Consider the following example, say I'm parsing a date string read from an external source, and I forgot to call .strip():

from datetime import datetime

datetime.strptime("2001-02-03\n", "%Y-%m-%d")

The output is a little confusing:

$ python3 strptime_example.py
Traceback (most recent call last):
  File "strptime_example.py", line 3, in <module>
    datetime.strptime("2025-11-14\n", "%Y-%m-%d")
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_strptime.py", line 789, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
                                    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_strptime.py", line 558, in _strptime
    raise ValueError("unconverted data remains: %s" %
                      data_string[found.end():])
ValueError: unconverted data remains: 

The error message is using %s, when using %r would give a more unambiguous description of the extra data – similar with strings quoted manually using ' in the error message just above:

https://github.com/python/cpython/blob/a486d452c78a7dfcd42561f6c151bf1fef0a756e/Lib/_strptime.py#L567-L570

CPython versions tested on:

3.13

Operating systems tested on:

macOS

Linked PRs
  • gh-141541
  • gh-141712

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

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

はじめの一歩

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

調査の方向性

Lib/_strptime.py の、報告で示されている行にある未変換データのエラー付近から始め、末尾に改行を含む例を再現します。残りのデータが、近くにあるエラーメッセージと一貫した repr 形式のフォーマットで曖昧さなく示されれば、作業は完了です。

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

評価

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

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

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