python / python/cpython

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

Open
#141,540 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
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:

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.