Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@multimethod(object, bracket)
def join(seq, sep):
return sep[0] + join(seq, sep[1] + sep[0]) + sep[1]
@multimethod(paper, rock)
def roshambo(left, right):
return 'paper covers rock'
@multimethod(tree, object)
def join(seq, sep):
return join(seq.walk(), sep)
@multimethod(rock, scissors)
def roshambo(left, right):
return 'rock smashes scissors'
@multimethod(scissors, rock)
@multimethod(rock, scissors)
def roshambo(left, right):
return 'rock smashes scissors'
def test_get_type():
method = multimethod(lambda: None)
assert method.get_type is type
@method.register
def _(x: Union[int, type(None)]):
pass
assert method.get_type is type
@method.register
def _(x: List[int]):
pass
assert method.get_type is get_type
@multimethod(object, object)
def roshambo(left, right):
return 'tie'
@multimethod
def func(arg: bool):
return bool
assert roshambo(r, p) == 'paper covers rock'
assert roshambo(p, r) == 'paper covers rock'
assert roshambo(r, s) == 'rock smashes scissors'
assert roshambo(p, s) == 'scissors cut paper'
assert roshambo(r, r) == 'tie'
assert len(roshambo) == 8
del roshambo[()]
del roshambo[rock, paper]
assert len(roshambo) == 5
with pytest.raises(DispatchError, match="0 methods"):
roshambo(r, r)
# methods
class cls(object):
method = multidispatch(lambda self, other: None)
@method.register(Iterable, object)
def _(self, other):
return 'left'
@method.register(object, Iterable)
def _(self, other):
return 'right'
def test_cls():
obj = cls()
assert obj.method(None) is cls.method(None, None) is None
assert obj.method('') == 'right'
assert cls.method('', None) == 'left'
with pytest.raises(DispatchError, match="2 methods"):
@multidispatch
def func(arg):
return type(arg)