You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
datetime.strptime no longer raises ValueError: stray % in format '%Y %' #131434
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)
raiseValueError("stray %% in format '%s'"%format) fromNone
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?
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.
Activity
StanFromIreland commentedon Mar 20, 2025
@picnixz This is in
_strptime.pyThe error does not execute, I will look into this.
cpython/Lib/_strptime.py
Lines 448 to 449 in b8367e7
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?
pythongh-131434: Improve error reporting for incorrect format in strp…
serhiy-storchaka commentedon Mar 21, 2025
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.
pythongh-131434: Improve error reporting for incorrect format in strp…
pythongh-131434: trailing % in strptime format strings should raise a…
duaneg commentedon Mar 22, 2025
Sorry, I was looking at this and didn't refresh the page and notice you already had a fix :-)
gh-131434: Improve error reporting for incorrect format in strptime() (…
pythongh-131434: Improve error reporting for incorrect format in strp…
[3.13] gh-131434: Improve error reporting for incorrect format in str…