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
time.fromisoformat() and datetime.fromisoformat() accept a fraction without a decimal mark in the basic format #161017
In the basic (no separator) format, time.fromisoformat() and datetime.fromisoformat() accept digits after the seconds as a fractional second even when there is no decimal mark (. or ,). The C implementation and the pure-Python _pydatetime disagree:
"123045123456" is read as 12:30:45.123456 too, and with seven or more digits the extra ones are silently dropped. The colon format is already strict about this: "12:30:4512" raises ValueError, and so does "12:30:45:12".
I would expect ValueError in all of these, as _pydatetime does. The documentation only describes a fractional second after a decimal mark, and a basic time such as "12304512" is more likely a typo or a different format than 12:30:45.12.
The C code in parse_hh_mm_ss_ff() (Modules/_datetimemodule.c) steps back one character (--p) after each component when there is no separator, so the next component can be parsed. After the seconds it then continues straight into the fractional part, without checking that the character there is a . or ,. The separator case returns an error ("Malformed microsecond separator") in the equivalent place.
I found this by comparing the C implementation with _pydatetime using Hypothesis. I have a small fix (reject it in the C parser, plus test_fromisoformat_fails cases for time and datetime; the existing tests run against both implementations) and will open a PR.
CPython versions tested on:
3.11.18, 3.12.3, 3.13.18, 3.14.10, CPython main branch (3.16.0a1+dev)
Operating systems tested on:
Linux
The report and fix were prepared with the help of an AI coding assistant (Devin CLI). I ran the examples above on the interpreters listed, and the test_datetime suite on a build of main before and after the change.
Activity
picnixz commentedon Oct 31, 2026
I would prefer accepting rather than rejecting. The Python implementation is usually not the one being used.
cc @StanFromIreland @pganssle
picnixz commentedon Oct 31, 2026
Related (indirectly): #118948
StanFromIreland commentedon Oct 31, 2026
This can be handled/discussed with #155175.