-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
bpo-24416: Return a IsoCalendarDate from date.isocalendar() #15633
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
Closed
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e1c5b00
bpo-24416: Return a namedtuple from date.isocalendar()
corona10 309b1f1
bpo-24416: Apply code review
corona10 bf168c9
bpo-24416: Apply code review
corona10 f3c02ad
bpo-24416: Update docstrings
corona10 9ae1886
bpo-24416: Apply third's codereview
corona10 c592159
bpo-24416: Revert to support pickle
corona10 87ad62c
bpo-24416: Update misc/news
corona10 63d1ed9
bpo-24416: Update __new__
corona10 752aad8
Change pickling behavior of isocalendar() objects
pganssle 4b2dc8b
Rename IsoCalendarDate to _IsoCalendarDate
pganssle 205b6c5
bpo-24416: Update datetime.rst
corona10 32cc8c1
bpo-24416: Update unittest
corona10 41057ff
Revert "Rename IsoCalendarDate to _IsoCalendarDate"
pganssle 9c98a61
Revert "Change pickling behavior of isocalendar() objects"
pganssle d4ce1e7
bpo-24416: Add pickle test
corona10 2987fdc
bpo-24416: Update C API to subclass tuple
corona10 8584bf6
bpo-24416: Change pickling behavior of isocalendar() objects
corona10 17104d5
bpo-24416: Update __new__
corona10 f04b6bc
bpo-24416: Update whatsnew
corona10 0b04d45
bpo-24416: Apply vstinner's review
corona10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -1095,7 +1095,7 @@ def isoweekday(self): | |||||||
return self.toordinal() % 7 or 7 | ||||||||
|
||||||||
def isocalendar(self): | ||||||||
"""Return a 3-tuple containing ISO year, week number, and weekday. | ||||||||
"""Return a named tuple containing ISO year, week number, and weekday. | ||||||||
|
||||||||
The first ISO week of the year is the (Mon-Sun) week | ||||||||
containing the year's first Thursday; everything else derives | ||||||||
|
@@ -1120,7 +1120,7 @@ def isocalendar(self): | |||||||
if today >= _isoweek1monday(year+1): | ||||||||
year += 1 | ||||||||
week = 0 | ||||||||
return year, week+1, day+1 | ||||||||
return _IsoCalendarDate(year, week+1, day+1) | ||||||||
|
||||||||
# Pickle support. | ||||||||
|
||||||||
|
@@ -1210,6 +1210,36 @@ def __reduce__(self): | |||||||
else: | ||||||||
return (self.__class__, args, state) | ||||||||
|
||||||||
|
||||||||
class IsoCalendarDate(tuple): | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pppery Can you explain the reason about this suggestion? |
||||||||
def __new__(cls, year, week, weekday): | ||||||||
return super().__new__(cls, (year, week, weekday)) | ||||||||
|
||||||||
@property | ||||||||
def year(self): | ||||||||
return self[0] | ||||||||
|
||||||||
@property | ||||||||
def week(self): | ||||||||
return self[1] | ||||||||
|
||||||||
@property | ||||||||
def weekday(self): | ||||||||
return self[2] | ||||||||
|
||||||||
def __reduce__(self): | ||||||||
# This code is intended to pickle the object without making the | ||||||||
# class public. See https://bugs.python.org/msg352381 | ||||||||
return (tuple, (tuple(self),)) | ||||||||
corona10 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
def __repr__(self): | ||||||||
return (f'{self.__class__.__name__}' | ||||||||
f'(year={self[0]}, week={self[1]}, weekday={self[2]})') | ||||||||
|
||||||||
|
||||||||
_IsoCalendarDate = IsoCalendarDate | ||||||||
del IsoCalendarDate | ||||||||
_tzinfo_class = tzinfo | ||||||||
|
||||||||
class time: | ||||||||
|
@@ -1559,6 +1589,7 @@ def __reduce__(self): | |||||||
time.max = time(23, 59, 59, 999999) | ||||||||
time.resolution = timedelta(microseconds=1) | ||||||||
|
||||||||
|
||||||||
class datetime(date): | ||||||||
"""datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]]) | ||||||||
|
||||||||
|
@@ -2514,7 +2545,7 @@ def _name_from_offset(delta): | |||||||
_format_time, _format_offset, _is_leap, _isoweek1monday, _math, | ||||||||
_ord2ymd, _time, _time_class, _tzinfo_class, _wrap_strftime, _ymd2ord, | ||||||||
_divide_and_round, _parse_isoformat_date, _parse_isoformat_time, | ||||||||
_parse_hh_mm_ss_ff) | ||||||||
_parse_hh_mm_ss_ff, _IsoCalendarDate) | ||||||||
# XXX Since import * above excludes names that start with _, | ||||||||
# docstring does not get overwritten. In the future, it may be | ||||||||
# appropriate to maintain a single module level docstring and | ||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2019-09-01-15-17-49.bpo-24416.G8Ww1U.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
The ``isocalendar()`` methods of :class:`datetime.date` and | ||
:class:`datetime.datetime` now return a :term:`named tuple` | ||
instead of a :class:`tuple`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.