A weird thing or a bug with @typedispatch?

@typedispatch
def add(x:int, y:int):
    return x+y
@typedispatch
def add(x:int, y:str):
    return x + int(y)

… produces the following:

print(add(1,2))   #=> 3
print(add(1,'2')) #=> 3

BUT, if I pass these in using named arguments the type dispatch mechanism fails:

print(add(x=1,y=2))

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-54-5d350863b395> in <module>
----> 1 add(x=1,y=2)

~/development/projects/blurr/_libs/fastcore/fastcore/dispatch.py in __call__(self, *args, **kwargs)
     94         ts = L(args).map(type)[:2]
     95         f = self[tuple(ts)]
---> 96         if not f: return args[0]
     97         if self.inst is not None: f = MethodType(f, self.inst)
     98         return f(*args, **kwargs)

IndexError: tuple index out of range

Maybe this is documented somewhere but it took me a few hours or wrestling with this before I figured it out, and so … here it is to ease other’s pain and/or see if this is a bug or python feature :slight_smile:

1 Like

We haven’t tried using with named argument so I guess this means it does not work and should be documented…

2 Likes