strptime: use %r in error messages, not %s
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in Lib/_strptime.py around the unconverted-data error at the lines cited in the report, and reproduce the example with the trailing newline. The work is done when the remaining data is shown unambiguously using repr-style formatting, consistent with the nearby error message.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100