lballabio / QuantLib-SWIG

QuantLib wrappers to other languages
Other
342 stars 285 forks source link

QuantLib.Date arithmetic with tuples #609

Open eltoder opened 9 months ago

eltoder commented 9 months ago

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.

lballabio commented 9 months ago

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 commented 9 months ago

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

eltoder commented 9 months ago

Thank you.