Closed
Description
Bug report
Bug description:
>>> datetime.fromisoformat('2020-01-01T00:00+00:90')
datetime.datetime(2020, 1, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=5400)))
expected result: ValueError
(90 minutes is out of range 00-59)
I wasn't able to find a definitive paragraph of the ISO8601 to quote (it's not an open standard), but it appears datetime libraries in other languages (Temporal, NodaTime) do enforce this. Also, RFC3339 does explicitly forbid offset minutes outside the 00-59 range.
What are your thoughts?
CPython versions tested on:
3.13
Operating systems tested on:
macOS
Linked PRs
Metadata
Metadata
Assignees
Labels
Projects
Status
Done
Activity
donBarbos commentedon Nov 20, 2024
I understand that the solution to this problem still requires discussion, as it may break backward compatibility.
But I decided to save some information about this module here for now.
When we get this problem:
time.fromisoformat()
datetime.fromisoformat()
The
fromisoformat()
functions returnscls
so afterfromisoformat()
we call__new__
We have function to check input time data:
_check_time_fields(hour, minute, second, microsecond, fold)
, but this function is called when creating a new instance in__new__
methods (in classestime
anddatetime
). And we can't call_check_time_fields
for tzinfo.Inside the
__new__
method we have access to the already formedtzinfo
object of thetimedelta
class (it can also beNone
, but this is not our case). This object is formed when returning from_parse_isoformat_time
which we call in thefromisoformat
method. Also,tzinfo
only stores information aboutdays
,seconds
andmilliseconds
, the rest of the fields have not been saved since the object was created.So we can start checking time fields inside
_parse_isoformat_time
(at this moment we know tz info fields) but then we will getValueError(f'Invalid isoformat string: {time_string!r}')
every time the fields go beyond their range because all exceptions from_parse_isoformat_time
are intercepted andValueError(f'Invalid isoformat string: {time_string!r}')
is returned.And if no one has revivals, I'd like to implement patch.
donBarbos commentedon Nov 23, 2024
I have a suggestion to call
_check_time_fields(hour, minute, second, microsecond, fold)
insidetimedelta.__new__
(as it is done in thetime
,datetime
classes).But this will have consequences. There will be restrictions for
timedelta
object:ariebovenberg commentedon Nov 24, 2024
@donBarbos we'd have to think of something else.
timedelta(minutes=90)
is perfectly valid and should remain so. The restrictions are limited to the parsing of UTC offsets. Sounds likeparse_isoformat_time
is where the fix should be?donBarbos commentedon Nov 24, 2024
@ariebovenberg sorry for my inattention but we need to look at the C implementation to solve this problem. And the one in
Lib/_pydatetime.py
needs to be tidied up so there are no discrepancies. I'll soon open an issue to draw attention to the Python implementation, as I've noticed that the Python and C implementations of the library have differences and_pydatetime.py
contains a lot of legacy that is poorly maintained, unlike the main implementation (this is inModules/_datetimemodule.c
)Regarding where to check time fields, I completely agree with you, as it is more logical.
datetime.fromisoformat
#127242gh-126883: Add check that timezone fields are in range for `datetime.…
donBarbos commentedon May 19, 2025
i think we can close this issue
cc @pganssle
2 remaining items