QuantLib-SWIG icon indicating copy to clipboard operation
QuantLib-SWIG copied to clipboard

QuantLib.Date arithmetic with tuples

Open eltoder opened this issue 1 year ago • 3 comments

I've found the following code in the Python SWIG bindigs:

%pythoncode %{
Date._old___add__ = Date.__add__
Date._old___sub__ = Date.__sub__
def Date_new___add__(self,x):
    if type(x) is tuple and len(x) == 2:
        return self._old___add__(Period(x[0],x[1]))
    else:
        return self._old___add__(x)
def Date_new___sub__(self,x):
    if type(x) is tuple and len(x) == 2:
        return self._old___sub__(Period(x[0],x[1]))
    else:
        return self._old___sub__(x)
Date.__add__ = Date_new___add__
Date.__sub__ = Date_new___sub__
%}

This allows adding/subtracting tuples of size 2 to/from Dates as a shortcut for using Period class. As far as I can tell, this is not documented and not tested. It's also not any faster than using Periods, since it is implemented in python. In fact, this is slowing down normal operations and is one of the blockers for using SWIG -builtin option.

Is this feature really needed? If it is desired, it can be implemented more generally and efficiently using typemaps. It seems rather dubious to me though, so if possible I'd vote to remove it altogther.

eltoder avatar Jan 27 '24 18:01 eltoder

Huh. That's weird. Git logs tell me that that code is from 2004, but give me no hint about why we wrote it. I'd deprecate it for a release or two and then remove it.

lballabio avatar Jan 29 '24 09:01 lballabio

Deprecation done in #612. Let's circle back in a while.

lballabio avatar Jan 29 '24 14:01 lballabio

Thank you.

eltoder avatar Jan 29 '24 14:01 eltoder