Bug report
Checklist
A clear and concise description of the bug
This is a regression in Python 3.11 from 3.10
try:
print(datetime.datetime.fromisoformat('20230808120000Z'))
assert 0, "failed"
except ValueError:
print("PASS")
Since Python 3.11, datetime.fromisoformat invokes _find_isoformat_datetime_separator which tries to find where the separator is between the date and time, e.g. "2023-08-08 12:00:00" which is in position 10.
For strings without a date element separator (the dash) the function just outright returns 8. For the example above, that results in the string being parsed as datetime(2023, 8, 8, 2, 0, 0). This is incorrect.
Suggestion to fix:
- First alternative: the
_find_isoformat_datetime_separator must always validate that the returned index must not be an ascii digit, that for all cases and branches inside the function.
- Second alternative:
datetime.fromisoformat must check if separator_location points to an ascii digit or not, and if not complain about invalid date
Your environment
- CPython versions tested on: 3.11.4
- Operating system and architecture: Windows
Linked PRs
Bug report
Checklist
A clear and concise description of the bug
This is a regression in Python 3.11 from 3.10
Since Python 3.11,
datetime.fromisoformatinvokes_find_isoformat_datetime_separatorwhich tries to find where the separator is between the date and time, e.g. "2023-08-08 12:00:00" which is in position 10.For strings without a date element separator (the dash) the function just outright returns 8. For the example above, that results in the string being parsed as
datetime(2023, 8, 8, 2, 0, 0). This is incorrect.Suggestion to fix:
_find_isoformat_datetime_separatormust always validate that the returned index must not be an ascii digit, that for all cases and branches inside the function.datetime.fromisoformatmust check ifseparator_locationpoints to an ascii digit or not, and if not complain about invalid dateYour environment
Linked PRs
datetime.datetime.fromisoformat#107791