Skip to content

Inconsistent behavior of fromisoformat methods in datetime module implementations #127260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
donBarbos opened this issue Nov 25, 2024 · 1 comment
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@donBarbos
Copy link
Contributor

donBarbos commented Nov 25, 2024

Bug report

Bug description:

1. Incorrect timezone validation in _pydatetime (solved)

As far as I understand, the documentation says that Z char should mean that tzinfo is timezone.utc, so there cannot be any time zone fields after it.
Based on this, _pydatetime implementation is incorrect, right?

>>> import _datetime, _pydatetime
>>> _pydatetime.datetime.fromisoformat('2020-01-01T00:00Z00:50')
datetime.datetime(2020, 1, 1, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=3000)))
>>> _datetime.datetime.fromisoformat('2020-01-01T00:00Z00:50')
Traceback (most recent call last):
  File "<python-input-54>", line 1, in <module>
    _datetime.datetime.fromisoformat('2020-01-01T00:00Z00:50')
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Invalid isoformat string: '2020-01-01T00:00Z00:50'

2. Miss the wrong millisecond separator in _datetime (solved)

In _pydatetime the separator for milliseconds must be either a period . or a comma ,.
Should we allow colon : as millisecond separator?

>>> import _datetime, _pydatetime
>>> _datetime.datetime.fromisoformat('2020-01-01T00:00:01:1')
datetime.datetime(2020, 1, 1, 0, 0, 1, 100000)
>>> _pydatetime.datetime.fromisoformat('2020-01-01T00:00:01:1')
Traceback (most recent call last):
  File "<python-input-119>", line 1, in <module>
    _pydatetime.datetime.fromisoformat('2020-01-01T00:00:01:1')
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../cpython/Lib/_pydatetime.py", line 1969, in fromisoformat
    "Return local time tuple compatible with time.localtime()."
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    dst = self.dst()
    ^^^^^^^^^^^^^^^^
ValueError: Invalid isoformat string: '2020-01-01T00:00:01:1'

3. The first errors caught can be different

If these errors occur separately, then both implementations are able to detect them, but when there are several problems, the methods may behave differently. In this case _pydatetime first detected an error due to the separator, and _datetime first detected an error in exceeding the limits.

>>> import _datetime, _pydatetime
>>> _pydatetime.datetime.fromisoformat('2009-04-19T03:15:45+10:90.11')
Traceback (most recent call last):
  File "<python-input-40>", line 1, in <module>
    _pydatetime.datetime.fromisoformat('2009-04-19T03:15:45+10:90.11')
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../cpython/Lib/_pydatetime.py", line 1969, in fromisoformat
            f'Invalid isoformat string: {date_string!r}') from None
    else:
ValueError: Invalid isoformat string: '2009-04-19T03:15:45+10:90.11'
>>> _datetime.datetime.fromisoformat('2009-04-19T03:15:45+10:90.11')
Traceback (most recent call last):
  File "<python-input-41>", line 1, in <module>
    _datetime.datetime.fromisoformat('2009-04-19T03:15:45+10:90.11')
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: minute must be in 0..59

Also also an issue has already been created about the fact that some errors have different output, here:

I'll send a PR.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

@donBarbos donBarbos added the type-bug An unexpected behavior, bug, or error label Nov 25, 2024
@ZeroIntensity ZeroIntensity added stdlib Python modules in the Lib dir 3.12 only security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Nov 25, 2024
@picnixz picnixz changed the title Inconsistent behavior of fromisoformat methods in datetime module implementations gh-109798: Inconsistent behavior of fromisoformat methods in datetime module implementations Nov 27, 2024
@picnixz
Copy link
Member

picnixz commented Nov 27, 2024

Ah crap I thought this one was a PR. Reverting the title.

@picnixz picnixz changed the title gh-109798: Inconsistent behavior of fromisoformat methods in datetime module implementations Inconsistent behavior of fromisoformat methods in datetime module implementations Nov 27, 2024
pganssle pushed a commit that referenced this issue Feb 18, 2025
…tations (#130134)

In the Python implementation, "Z" was allowed where only "+" or "-" should be allowed in time zone specifiers. In the C implementation, ":" was allowed as a separator between the whole and fractional portion of times (seconds). These have both been forbidden and the error messages harmonized.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Archived in project
Development

No branches or pull requests

3 participants