coady / multimethod

Multiple argument dispatching.
https://coady.github.io/multimethod
Other
277 stars 24 forks source link

Dispatch on empty list does not work #52

Closed adam-urbanczyk closed 3 years ago

adam-urbanczyk commented 3 years ago

The following does result in a DispatchError:

from multimethod import multimethod
from typing import List

class Dummy:

    @multimethod
    def dummy(self, a: float,b : List[float]):

        pass

Dummy().dummy(1.0, [1.0])
Dummy().dummy(1.0, [])

Is it intended?

adam-urbanczyk commented 3 years ago

BTW: If you are OK with solving this (by e.g. adding another key to the dispatch dict; first come first served in the case of ambiguities) it'd be great to have it before #50 ...

coady commented 3 years ago

This has come up before: #31.

The problem is handling ambiguity. I think that [] should match List[bool] over List[int], but also suspect that that would be surprising and perceived as a bug to users.

adam-urbanczyk commented 3 years ago

Thanks for the reference. I'm not sure what you mean with matching bool over int. I was just hoping for a solution that matches any list that was specified in the signature. In the case of ambiguities matching first (or last) defined would be perfectly fine.

Otherwise, do you have any suggestions regarding working around this issue?

coady commented 3 years ago

Ok, I'm willing to try it, as long as it's marked as provisional. Maybe there won't be any issues filed over ambiguous dispatch. Also, [] can be used be as a literal now.

adam-urbanczyk commented 3 years ago

Great, current solution works perfectly for my use case.