-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
gh-102450: Add ISO-8601 alternative for midnight to fromisoformat()
calls.
#105856
Conversation
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
The PEP that explains it was linked on the issue: #102450 (comment) Basically, (I believe) you need to implement it in both places but only the C version is used by CPython. If you add tests they should run on both implementations so you can ensure you've done things correctly. You should also be able to |
Thanks @wookie184, that makes sense 😄 I do have a query though: the tests that I added in 311f914 are actually passing ( EDIT: I've since added the Python implementation, but am still not convinced that the Python implementation is getting tested locally (seems to be here on GitHub though). |
311f914
to
b3890f4
Compare
I've also just noticed that the >>> from datetime import datetime as c_datetime
>>> c_datetime.fromisoformat("2023-01-32T24:00:00")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: day is out of range for month vs >>> from _pydatetime import datetime as py_datetime
>>> py_datetime.fromisoformat("2023-01-32T24:00:00")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/xxx/Documents/personal/cpython/Lib/_pydatetime.py", line 1906, in fromisoformat
return cls(*(date_components + time_components))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/xxx/Documents/personal/cpython/Lib/_pydatetime.py", line 1717, in __new__
year, month, day = _check_date_fields(year, month, day)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/xxx/Documents/personal/cpython/Lib/_pydatetime.py", line 545, in _check_date_fields
raise ValueError('day must be in 1..%d' % dim, day)
ValueError: ('day must be in 1..31', 32) Note how |
01c5bdf
to
5dc3d69
Compare
We can handle this separately. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! Do you want to add yourself to the ACKS file and/or to the patch name, so you get credit? (This is optional)
Also can you rebase against main?
Misc/NEWS.d/next/Library/2023-06-16-14-52-00.gh-issue-102450.MfeR6A.rst
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I like how invalid inputs (month 13, non-zero minute, etc.) are checked: it's smart (and keep the code short) :-) I also like tests which seem to cover all cases.
About the feature itself, I trust @pganssle's opinion to decide if we should implement it or not.
…n 24hrs has non-zero time component(s)
26338f5
to
1b65671
Compare
Think I've done this correctly, so hopefully should be good now 🙂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice, the code handles also well the maximum date:
$ ./python
>>> import datetime
>>> datetime.datetime.fromisoformat('9999-12-31T24:00:00')
ValueError: year 10000 is out of range
* main: (69 commits) Add "annotate" SET_FUNCTION_ATTRIBUTE bit to dis. (python#124566) pythongh-124412: Add helpers for converting annotations to source format (python#124551) pythongh-119180: Disallow instantiation of ConstEvaluator objects (python#124561) For-else deserves its own section in the tutorial (python#123946) Add 3.13 as a version option to the crash issue template (python#124560) pythongh-123242: Note that type.__annotations__ may not exist (python#124557) pythongh-119180: Make FORWARDREF format look at __annotations__ first (python#124479) pythonGH-58058: Add quick reference for `ArgumentParser` to argparse docs (pythongh-124227) pythongh-41431: Add `datetime.time.strptime()` and `datetime.date.strptime()` (python#120752) pythongh-102450: Add ISO-8601 alternative for midnight to `fromisoformat()` calls. (python#105856) pythongh-124370: Add "howto" for free-threaded Python (python#124371) pythongh-121277: Allow `.. versionadded:: next` in docs (pythonGH-121278) pythongh-119400: make_ssl_certs: update reference test data automatically, pass in expiration dates as parameters python#119400 (pythonGH-119401) pythongh-119180: Avoid going through AST and eval() when possible in annotationlib (python#124337) pythongh-124448: Update Windows builds to use Tcl/Tk 8.6.15 (pythonGH-124449) pythongh-123884 Tee of tee was not producing n independent iterators (pythongh-124490) pythongh-124378: Update test_ttk for Tcl/Tk 8.6.15 (pythonGH-124542) pythongh-124513: Check args in framelocalsproxy_new() (python#124515) pythongh-101100: Add a table of class attributes to the "Custom classes" section of the data model docs (python#124480) Doc: Use ``major.minor`` for documentation distribution archive filenames (python#124489) ...
Closes #102450.
Closes #124257.
I've updated
_datetimemodule.c
and_pydatetime.py
such that calls to Python'sdatetime.time.fromisoformat
anddatetime.datetime.fromisoformat
both allow providing the24:00
ISO-8601 alternative to midnight, but the class constructors themselves do not (as per this comment from @pganssle).Screenshots of the C code working:
I've also done the same tests for the
_pydatetime
module and everything appears to be working.Also updated the datetime tests accordingly, to ensure that
datetime.time.fromisoformat("24:00:00.000000")
is parsed asdatetime.time(0, 0, 0, 0)
etc. and the cases that should error (such asdatetime.time.fromisoformat("24:30")
) do indeed error.