Skip to content

datetime.fromisoformat incorrectly accepts and parses strings without date-time separator #107779

Description

@joaoe

Bug report

Checklist

  • I am confident this is a bug in CPython, not a bug in a third-party project
  • I have searched the CPython issue tracker, and am confident this bug has not been reported before

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:

  1. 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.
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions