Is it safe to multiply relativedelta
objects? I'm seeing some weird and inconsistent behaviour, and can't find it documented what sorts of arithmetic are supported by this class (if any)
>>> from datetime import datetime
>>> from dateutil.relativedelta import relativedelta
>>> datetime.now() + relativedelta(days=2)
datetime.datetime(2014, 5, 30, 12, 24, 59, 173941)
>>> datetime.now() + relativedelta(days=1) * 2
# TypeError: integer argument expected, got float
On the other hand:
>>> relativedelta(days=2) == relativedelta(days=1) * 2
True
Full traceback (with python
2.7.5 and dateutil
1.5):
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/dateutil/relativedelta.py", line 261, in __radd__
day = min(calendar.monthrange(year, month)[1],
File "/usr/lib/python2.7/calendar.py", line 121, in monthrange
day1 = weekday(year, month, 1)
File "/usr/lib/python2.7/calendar.py", line 113, in weekday
return datetime.date(year, month, day).weekday()
TypeError: integer argument expected, got float
You've run into a known bug in
relativedelta
's handling of multiplication, since fixed. It only affects Python 2.7 or newer (call signatures of certain functions were tightened).Upgrade your
python-dateutils
package to version 2.1 or newer.Don't be put off by the 2.0-is-Python-3-only misinformation on the project documentation; 2.1 and 2.2 are Python 2 and 3 cross-compatible.