Multimethod union types don't support overloading correctly

315 Views Asked by At

I'm using the multimethod package (1.4) to overload methods. I need to have Unioned types (or TypeVar) as the input types; however, this causes an issue. Example:

from typing import Union
from multimethod import multimethod


@multimethod
def func(x: int):
    print(x)

@multimethod
def func(x: int, y: int):
    print(x, y)

func(1)
func(2, 3)

This works fine, and outputs

1
2 3

As expected. However, this code crashes:

from typing import Union
from multimethod import multimethod


@multimethod
def func(x: int):
    print(x)

@multimethod
def func(x: Union[int, float], y: int):
    print(x, y)

func(1)
func(2, 3)

Output:

1
Traceback (most recent call last):
  File "C:\Users\...\testing.py", line 14, in <module>
    func(2, 3)
  File "C:\Users\...\.conda\envs\...\lib\site-packages\multimethod\__init__.py", line 184, in __call__
    return self[tuple(map(self.get_type, args))](*args, **kwargs)
TypeError: func() takes 1 positional argument but 2 were given

Is there a way to have union types and varying numbers of arguments? Thanks!

0

There are 0 best solutions below