Skip to content

datetime.strptime no longer raises ValueError: stray % in format '%Y %' #131434

Closed

Description

@westandskif

Bug report

Bug description:

in 3.13.2 and 3.13.1 datetime.strptime no longer raises ValueError: stray % in format '%Y %'

Python 3.13.2 (main, Mar 19 2025, 01:28:14) [Clang 16.0.0 (clang-1600.0.26.6)] on Darwin
>>> from datetime import datetime
>>> datetime.strptime("2000 %", "%Y %")
datetime.datetime(2000, 1, 1, 0, 0)

as it used to do in 3.13.0:

Python 3.13.0 (main, Mar 19 2025, 01:42:48) [Clang 16.0.0 (clang-1600.0.26.6)] on darwin
>>> from datetime import datetime
>>> datetime.strptime("2000 %", "%Y %")
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    datetime.strptime("2000 %", "%Y %")
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/Users/nik/.pyenv/versions/3.13.0/lib/python3.13/_strptime.py", line 573, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
                                    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "/Users/nik/.pyenv/versions/3.13.0/lib/python3.13/_strptime.py", line 348, in _strptime
    raise ValueError("stray %% in format '%s'" % format) from None
ValueError: stray % in format '%Y %'

and e.g. 3.10:

Python 3.10.15 (main, Nov 28 2024, 23:39:17) [Clang 16.0.0 (clang-1600.0.26.4)] on Darwin
>>> from datetime import datetime
>>> datetime.strptime("2000 %", "%Y %")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/nik/.pyenv/versions/3.10.15/lib/python3.10/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/Users/nik/.pyenv/versions/3.10.15/lib/python3.10/_strptime.py", line 345, in _strptime
    raise ValueError("stray %% in format '%s'" % format) from None
ValueError: stray % in format '%Y %'

edit 1:
got the same in the latest 3.12.9 (used to work in older 3.12)

CPython versions tested on:

3.13

Operating systems tested on:

macOS

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Mar 18, 2025
  2. StanFromIreland commented on Mar 20, 2025

    @StanFromIreland
    Member

    @picnixz This is in _strptime.py

    The error does not execute, I will look into this.

    cpython/Lib/_strptime.py

    Lines 448 to 449 in b8367e7

    except IndexError:
    raise ValueError("stray %% in format '%s'" % format) from None

    I presume it was because of this commit, 5f4e5b5 I will revert it and see if it fixes it.

    Edit: I was correct, it was Serhiys commit. It will not be as simple as I though to fix as we no longer iterate over the format. @serhiy-storchaka any ideas for a fix for your old commit?

  3. added a commit that references this issue on Mar 21, 2025
  4. serhiy-storchaka commented on Mar 21, 2025

    @serhiy-storchaka
    Member

    Good catch! Indeed, the regression was introduced in #125406. There was only one test that indirectly tested this error, but it was broken, so the regression has not been noticed.

    #131568 fixes the regression for stray % at the end of the format string, and also improves error reporting for some other cases.

  5. added a commit that references this issue on Mar 21, 2025
  6. added a commit that references this issue on Mar 22, 2025
  7. duaneg commented on Mar 22, 2025

    @duaneg
    Contributor

    Sorry, I was looking at this and didn't refresh the page and notice you already had a fix :-)

  8. added a commit that references this issue on Apr 9, 2025
  9. added a commit that references this issue on Apr 9, 2025
  10. added a commit that references this issue on Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions