-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
datetime.fromisoformat() accepts invalid ISO 8601 timestamps #115783
Comments
I'd like to extend the list of accepted invalid timestamps:
All these should be rejected by |
I'm a beginner contributor and I'd like to work on this issue. How can I move forward? |
It is worth noting, the test_datetime defined within datetimetester.py test assumes inconsistent typing is acceptable. For example, '20250102T03:04:05,6' is considered an acceptable string to the datetime.isoformat() function. |
We have been accepting several "wrong" formats for a while (inconstent expanding, though we check a few cases; missing separator; separator other than ["T"] etc.)... This will require deprecation if we even wish to implement it.
It's a long list... (Not including other "wrong" cases)cpython/Lib/test/datetimetester.py Line 3394 in e82c2ca
cpython/Lib/test/datetimetester.py Line 3396 in e82c2ca
cpython/Lib/test/datetimetester.py Line 3481 in e82c2ca
cpython/Lib/test/datetimetester.py Line 3484 in e82c2ca
cpython/Lib/test/datetimetester.py Line 3487 in e82c2ca
cpython/Lib/test/datetimetester.py Line 3497 in e82c2ca
cpython/Lib/test/datetimetester.py Line 3501 in e82c2ca
cpython/Lib/test/datetimetester.py Line 3505 in e82c2ca
cpython/Lib/test/datetimetester.py Line 3507 in e82c2ca
Seeing as we seem to have been accepting these for so long and we even consider them valid in our tests I think this should be considered a feature request rather than a bug. I guess the decision is up to @pganssle, do you think we should restrict the function? I am happy to implement it. |
The current documented scope of The options we have going forward are to document further extensions we support (i.e. mixed and matched separators), issue a deprecation cycle for invalid formats or just go ahead and break this. Despite the fact that we have been supporting this for a while, I would be surprised if it is in particularly widespread use unless there is something out there generating these formats by default. They don't seem like the kind of format that you would choose to generate, unless maybe you are storing your strings in a place that can't accept : but can accept - or vice versa? In any case, deprecation is probably the safest option. The argument for being strict is that you are supposed to be able to use |
I agree.
It should also do what is in the name ;-) isoformat, not anyrandomformat
We should do this. I can assign myself to this, I also propose extending the docs to be more specific and up to date with 8601:2
Is this really that common an occurrence? They are warnings and not errors, most people do not check warnings. They will bother when it is no longer deprecated. |
Bug report
Bug description:
Since Python 3.11,
datetime.fromisoformat(...)
accepts invalid timestamps. The same code was tested in many Python versions via GitHub Actions:Wrong format:
2024-01-17T15:21:00-0800
Fixed format:
2024-01-17T15:21:00-08:00
Python code to check:
ISO 8601 defines 2 formats:
When checking the EBNF rules, all parts of the timestamp must be with or without separators. A mix of formats is not allowed.
I haven't checked for other basic and extended format mixes.

My CI checked many Python versions and platforms. Here a failed CI pipeline, shows a correct implementation. A successful pipeline denotes a buggy Python version.
Other resources:
datetime.datetime.fromisoformat
#107791CPython versions tested on:
3.8, 3.9, 3.10, 3.11, 3.12, 3.13
Operating systems tested on:
Linux, macOS, Windows
Linked PRs
datetime.fromisoformat
#131522The text was updated successfully, but these errors were encountered: