Skip to content

time.fromisoformat() and datetime.fromisoformat() accept a fraction without a decimal mark in the basic format #161017

Closed as duplicate

Description

@arxhr007

Bug report

Bug description:

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:

>>> import datetime, _pydatetime
>>> datetime.time.fromisoformat("12304512")
datetime.time(12, 30, 45, 120000)
>>> _pydatetime.time.fromisoformat("12304512")
Traceback (most recent call last):
  ...
ValueError: Invalid microsecond separator
>>> datetime.datetime.fromisoformat("20090419T12304512")
datetime.datetime(2009, 4, 19, 12, 30, 45, 120000)
>>> _pydatetime.datetime.fromisoformat("20090419T12304512")
ValueError: Invalid microsecond separator

"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.

Linked PRs

Activity

  1. picnixz commented on Oct 31, 2026

    @picnixz
    Member

    I would prefer accepting rather than rejecting. The Python implementation is usually not the one being used.

    cc @StanFromIreland @pganssle

  2. picnixz commented on Oct 31, 2026

    @picnixz
    Member

    Related (indirectly): #118948

  3. StanFromIreland commented on Oct 31, 2026

    @StanFromIreland
    Member

    This can be handled/discussed with #155175.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions