Skip to content

Pure-Python datetime.fromisoformat() accepts a trailing date-time separator #160850

Closed

Description

@Krish0703-irl

Bug report

Title: Pure-Python datetime.fromisoformat() accepts a trailing date-time separator

Bug report

Bug description:

The pure-Python datetime.fromisoformat() (Lib/_pydatetime.py) accepts a string that ends with the date-time separator and has no time after it, and returns midnight. The C implementation raises ValueError:

>>> import datetime, _pydatetime
>>> datetime.datetime.fromisoformat('2009-04-19T')
Traceback (most recent call last):
  ...
ValueError: Invalid isoformat string: '2009-04-19T'
>>> _pydatetime.datetime.fromisoformat('2009-04-19T')
datetime.datetime(2009, 4, 19, 0, 0)
>>> _pydatetime.datetime.fromisoformat('2009-04-19 ')
datetime.datetime(2009, 4, 19, 0, 0)

The same happens with basic and week dates ('20090419T', '2009-W16-7T').

fromisoformat() splits the string at the separator and then only parses the time part if tstr:, so an empty time part makes it fall back to the date-only path. The C version (datetime_fromisoformat()) only takes the date-only path when nothing follows the date (len > separator_location) and otherwise passes the empty time to the time parser, which rejects it.

This matters for PyPy and other implementations that use _pydatetime.

CPython versions tested on:

3.14, 3.15, CPython main branch

Operating systems tested on:

Linux

CPython versions tested on:

3.14, 3.15, CPython main branch

Operating systems tested on:

Linux

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

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions