Issue1950

classification
Title: datetime.date.__add__() should return NotImplemented instead of raising TypeError
Type: Severity: normal
Components: Versions: Jython 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Arfrever, fwierzbicki, zyasoft
Priority: Keywords:

Created on 2012-07-25.00:57:36 by Arfrever, last changed 2014-05-22.10:32:02 by Arfrever.

Messages
msg7335 (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) Date: 2012-07-25.00:57:35
datetime.date.__add__() should return NotImplemented instead of raising TypeError, as is already mentioned in source code.
This change fixes errors in test suite of python-dateutil (http://pypi.python.org/pypi/python-dateutil).

--- Lib/datetime.py
+++ Lib/datetime.py
@@ -911,8 +911,7 @@
             self._checkOverflow(t.year)
             result = date(t.year, t.month, t.day)
             return result
-        raise TypeError
-        # XXX Should be 'return NotImplemented', but there's a bug in 2.2...
+        return NotImplemented
 
     __radd__ = __add__
msg7347 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2012-08-03.16:33:26
But, this is the behavior of CPython 2.7 (I think it is fixed in 3.3)


Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import date
>>> x = date(1,2,3)
>>> y = date(4,5,6)
>>> x + y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'datetime.date' and 'datetime.date'
msg7348 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2012-08-03.16:35:07
Marking this as deferred in hopes that we remember to fix this in 3.3.
msg7351 (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) Date: 2012-08-04.05:36:03
The suggested change in Lib/datetime.py is still correct, but apparently Jython incorrectly handles NotImplemented (issue #1955).
msg8497 (view) Author: Jim Baker (zyasoft) Date: 2014-05-21.23:10:10
Is dateutil using the datetime internals in some way?

Re support in 3.x, let's not use bugs here to remind us of that sort of functionality, hopefully it's in a unit test.
msg8563 (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) Date: 2014-05-22.10:32:01
python-dateutil defines class dateutil.relativedelta.relativedelta with __add__() method, which allows addition operation between instances of datetime.date and dateutil.relativedelta.relativedelta.

$ python2.7 -c 'import datetime, dateutil.relativedelta; print(datetime.date(2014, 5, 22) + dateutil.relativedelta.relativedelta(months=3))'
2014-08-22
$ jython2.7 -c 'import datetime, dateutil.relativedelta; print(datetime.date(2014, 5, 22) + dateutil.relativedelta.relativedelta(months=3))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/share/jython-2.7/Lib/datetime.py", line 925, in __add__
    raise TypeError
TypeError
History
Date User Action Args
2014-05-22 10:32:02Arfreversetmessages: + msg8563
2014-05-21 23:10:10zyasoftsetnosy: + zyasoft
messages: + msg8497
2013-02-19 18:46:14fwierzbickisetversions: + Jython 2.7, - Deferred
2012-08-04 05:36:04Arfreversetmessages: + msg7351
2012-08-03 16:35:08fwierzbickisetmessages: + msg7348
versions: + Deferred
2012-08-03 16:33:26fwierzbickisetmessages: + msg7347
2012-07-25 00:57:36Arfrevercreate