Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Inconsistent behavior offromisoformat methods indatetime module implementations #127260

Closed
Labels
3.12only security fixes3.13bugs and security fixes3.14bugs and security fixesstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error
@donBarbos

Description

@donBarbos

Bug report

Bug description:

1. Incorrect timezone validation in_pydatetime (solved)

As far as I understand, thedocumentation says thatZ char should mean thattzinfo istimezone.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 (mostrecentcalllast):File"<python-input-54>",line1,in<module>_datetime.datetime.fromisoformat('2020-01-01T00:00Z00:50')~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^ValueError:Invalidisoformatstring:'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 (mostrecentcalllast):File"<python-input-119>",line1,in<module>_pydatetime.datetime.fromisoformat('2020-01-01T00:00:01:1')~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^File".../cpython/Lib/_pydatetime.py",line1969,infromisoformat"Return local time tuple compatible with time.localtime()."^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^dst=self.dst()^^^^^^^^^^^^^^^^ValueError:Invalidisoformatstring:'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 (mostrecentcalllast):File"<python-input-40>",line1,in<module>_pydatetime.datetime.fromisoformat('2009-04-19T03:15:45+10:90.11')~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File".../cpython/Lib/_pydatetime.py",line1969,infromisoformatf'Invalid isoformat string:{date_string!r}')fromNoneelse:ValueError:Invalidisoformatstring:'2009-04-19T03:15:45+10:90.11'>>>_datetime.datetime.fromisoformat('2009-04-19T03:15:45+10:90.11')Traceback (mostrecentcalllast):File"<python-input-41>",line1,in<module>_datetime.datetime.fromisoformat('2009-04-19T03:15:45+10:90.11')~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ValueError:minutemustbein0..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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixes3.14bugs and security fixesstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp