Inconsistent relativedelta args checking

Bug #1374022 reported by Marc Aymerich
12
This bug affects 2 people
Affects Status Importance Assigned to Milestone
dateutil
Fix Committed
Undecided
Unassigned

Bug Description

current relativedelta.__init__() method performs type checking mixing isinstance() and type() == type() checks leading to inconsistent results. I've been bitten by a bug when comparing a datetime.date with a FakeDatetime from python-freezegun:

File "/usr/local/lib/python2.7/dist-packages/dateutil/relativedelta.py", line 146, in __init__
    if dt1 < dt2:
TypeError: can't compare FakeDatetime to datetime.date

current checking is

if (not isinstance(dt1, datetime.date)) or (not isinstance(dt2, datetime.date)):
    raise TypeError("relativedelta only diffs datetime/date")
if not type(dt1) == type(dt2) # isinstance(dt2, type(dt1)):
    if not isinstance(dt1, datetime.datetime):
        dt1 = datetime.datetime.fromordinal(dt1.toordinal())
    if not isinstance(dt2, datetime.datetime):
        dt2 = datetime.datetime.fromordinal(dt2.toordinal())

I think a more consistent one, using always isinstance(), could be:

if (not isinstance(dt1, datetime.date)) or (not isinstance(dt2, datetime.date)):
    raise TypeError("relativedelta only diffs datetime/date")
if not isinstance(dt1, type(dt2)):
    if not isinstance(dt1, datetime.datetime):
        dt1 = datetime.datetime.fromordinal(dt1.toordinal())
    else:
        dt2 = datetime.datetime.fromordinal(dt2.toordinal())

Revision history for this message
jarondl (jarondl) wrote :

Hi,
I believe I fixed this in current 'master' (which will be released some time AFTER 2.3)

https://github.com/dateutil/dateutil/commit/db29c6d669e3809993b1a973a1671246f81fcb0a

Changed in dateutil:
status: New → Fix Committed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.