classification
Title: Make Calendar.itermonthdates() behave consistently in edge cases
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: 26650 28253 Superseder:
Assigned To: belopolsky Nosy List: belopolsky, jiangping.li, rhettinger, serhiy.storchaka, xiang.zhang
Priority: normal Keywords: patch

Created on 2016-09-28 03:01 by belopolsky, last changed 2017-10-24 17:17 by belopolsky. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 4079 merged belopolsky, 2017-10-23 03:34
Messages (9)
msg277577 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2016-09-28 03:01
The Calendar.itermonthdates() method is defined as follows:

"Return an iterator for one month. The iterator will yield datetime.date values and will always iterate through complete weeks, so it will yield dates outside the specified month."

However, for the two months at the extremes of datetime.date range, 0001-01 and 9999-12, the dates outside the specified month may not be representable as datetime.date instances.

The current implementation is inconsistent: itermonthdates(1, 1) may raise an OverflowError (see #26650), while itermonthdates(9999, 12) may yield an incomplete week (see #28253.)

This issue supersedes #26650 and #28253.
msg277584 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-09-28 05:24
Possible solutions (in any case the documentation needs changes):

1. Raise OverflowError at both ends (revert issue15421patch). The method becomes unusable for two extreme months.

2. Yield an incomplete week at both ends (apply the patch from issue26650). This can cause silent producing incorrect result in third-party programs.

3. Yield None for unrepresentable dates. This is more useful than raising OverflowError, and noisily fails, but third-party code should be changed to support extreme cases.

4. Yield dummy date instance for unrepresentable dates (apply the patch from issue28253). If we are lucky, the third-party code will just work, without any changes. If we are not lucky, this makes debugging harder.

5. Make datetime.date supporting a date outside current limits. This is a can of worms (numbering years B.D., output and parsing dates with negative and more than 4-digit years).

Examples of the usage of itermonthdates():

https://github.com/sunlightlabs/django-locksmith/blob/master/locksmith/hub/dataviews.py
https://github.com/quandyfactory/Quandy/blob/master/quandy.py
https://github.com/takanory/plone.app.event/blob/master/plone/app/event/portlets/portlet_calendar.py
https://github.com/gerow/gnome-shell-google-calendar/blob/master/gnome-shell-google-calendar.py
https://bitbucket.org/benallard/msgboard/src/1c08fa3ba040f8151d0e28130b01b30e0595e448/msgboard/controller.py?at=default
msg277644 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2016-09-28 16:26
I would like to take the #1 approach, and also implement an itermonthdays3() generator that would be like  itermonthdates(), but return 3-tuples (year, month, day) instead of datetime.date objects.

The name "itermonthdays3" is subject to discussion, but it fits with the existing "itermonthdays" and "itermonthdays2" that yield integers and 2-tuples respectively.  An alternative, but slightly longer name would be "itermonthdatetuples".

itermonthdates() can then be reimplemented as

def itermonthdates(self, year, month):
    for y, m, d in self.itermonthdays3(year, month):
        yield datetime.date(y, m, d)

with the obvious out of bounds behavior.
msg277662 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-09-28 21:23
LGTM. Maybe add also itermonthdays4() that yields 4-tuples (year, month, day, day_of_week)? I seen a code that filters out weekends from itermonthdates().
msg304735 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-22 08:23
Do you mind to create a pull request Alexander?
msg304775 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2017-10-23 02:10
Let me look into this.  It's been a while ...
msg304776 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2017-10-23 02:25
> 1. Raise OverflowError at both ends (revert issue15421patch). The method becomes unusable for two extreme months.

The issue 15421 patch was committed in 85710a40e7e9eab86060bedc3762ccf9ca8d26ca.
msg304777 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2017-10-23 03:40
I submitted PR 4079. Serhiy, please review the logic and if this matches what we discussed a year ago, I will add the docs and NEWS entries.
msg304930 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2017-10-24 17:17
New changeset fdd9b217c60b454ac6a82f02c8b0b551caeac88b by Alexander Belopolsky in branch 'master':
Closes bpo-28292: Implemented Calendar.itermonthdays3() and itermonthdays4(). (#4079)
https://github.com/python/cpython/commit/fdd9b217c60b454ac6a82f02c8b0b551caeac88b
History
Date User Action Args
2017-10-24 17:17:14belopolskysetstatus: open -> closed
resolution: fixed
messages: + msg304930

stage: patch review -> resolved
2017-10-23 03:40:53belopolskysetmessages: + msg304777
2017-10-23 03:34:00belopolskysetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request4049
2017-10-23 02:25:32belopolskysetmessages: + msg304776
2017-10-23 02:10:31belopolskysetmessages: + msg304775
2017-10-22 08:23:04serhiy.storchakasetmessages: + msg304735
components: + Library (Lib)
stage: test needed -> needs patch
2016-09-28 21:23:39serhiy.storchakasetmessages: + msg277662
2016-09-28 16:26:28belopolskysetmessages: + msg277644
2016-09-28 05:24:06serhiy.storchakasetmessages: + msg277584
2016-09-28 03:05:06belopolskysetdependencies: + calendar: OverflowErrors for year == 1 and firstweekday > 0, calendar.prcal(9999) output has a problem
2016-09-28 03:04:31belopolskylinkissue26650 superseder
2016-09-28 03:02:52belopolskylinkissue28253 superseder
2016-09-28 03:01:58belopolskycreate