strptime: use %r in error messages, not %s
未关闭
还没有人认领这个 Issue。
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:
CPython versions tested on:
3.13
Operating systems tested on:
macOS
Linked PRs
- gh-141541
- gh-141712
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 Lib/_strptime.py 中报告所引用行附近的未转换数据错误开始,并复现带有末尾换行符的示例。当剩余数据以与附近错误消息一致的 repr 风格格式明确显示时,工作即完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 1/5
- 预计耗时
- 1 小时以内
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100