coady / multimethod

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

Better support for variadic parameters. #36

Closed coady closed 3 years ago

coady commented 3 years ago

Issue for Pull #35. The dispatch resolution may match types even though it's clear the parameter parity is mismatched.

@multimethod
def temp(x: int, y: float):
    return "int, float"

@multimethod
def temp(x: bool):
    return "bool"

assert temp(True, 1.0) == "int, float"  # DispatchError
coady commented 3 years ago

In this scenario (bool,) matches even though calling it will raise a TypeError. I think the only perfect solution would be too slow: call the equivalent of inspect.Signature.bind on every candidate. There's a variant that almost as good though: call inspect.Signature.bind_partial on candidates after a cache miss.

That would catch every case where too many positional arguments are supplied. While also still allowing cases like def temp(x: bool, y=0) to work as always.

Like discussion #12 , it always seems to circle back to keyword arguments. Supporting keywords in the dispatch makes caching impractical; not supporting keywords always creates edge cases.

coady commented 3 years ago

Implemented in a778748.