Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
westandskif opened this issue Mar 18, 2025 · 3 comments
Open
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@westandskif
Copy link

westandskif commented Mar 18, 2025

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

@westandskif westandskif added the type-bug An unexpected behavior, bug, or error label Mar 18, 2025
@picnixz picnixz added stdlib Python modules in the Lib dir extension-modules C modules in the Modules dir labels Mar 18, 2025
@StanFromIreland
Copy link
Contributor

StanFromIreland commented Mar 20, 2025

@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?

@serhiy-storchaka serhiy-storchaka self-assigned this Mar 20, 2025
@encukou encukou removed the extension-modules C modules in the Modules dir label Mar 21, 2025
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Mar 21, 2025
…time()

In particularly, fix regression in detecting stray % at the end of the
format string.
@serhiy-storchaka
Copy link
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.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Mar 21, 2025
…time()

In particularly, fix regression in detecting stray % at the end of the
format string.
duaneg added a commit to duaneg/cpython that referenced this issue Mar 22, 2025
…n error

Treat trailing % at the end of a format string consistently with % followed by
spaces inside a format string. The IndexError which was previously raised when
there was a trailing % can no longer happen, so we can remove the dead code
dealing with it.

Note the ValueError's message in this case has changed from "stray % in format
'...'" to "'%' is a bad directive in format '...'". We could treat this as a
special case and retain the previous wording if desired, but ISTM that
consistency is better than strict backwards compatibility here.
@duaneg
Copy link

duaneg commented Mar 22, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

6 participants