How to use the multimethod.DispatchError function in multimethod

To help you get started, we’ve selected a few multimethod examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github coady / multimethod / tests / test_all.py View on Github external
def test_strict():
    @multimethod(int, object)
    @multimethod(object, int, strict=True)
    def func(x, y):
        pass
    assert func.__name__ == 'func'
    assert func(0, None) is func(None, 0) is None
    with pytest.raises(DispatchError):
        func(0, 0)
github coady / multimethod / tests / test_dispatch.py View on Github external
def test_roshambo():
    assert roshambo.__name__ == 'roshambo'
    r, p, s = rock(), paper(), scissors()
    assert len(roshambo) == 7
    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)
github coady / multimethod / tests / test_methods.py View on Github external
def test_annotations():
    obj = cls()
    assert obj.method(0.0) == (cls, float)  # run first to check exact match post-evaluation
    assert obj.method(0) == (object, int)
    assert cls.method(None, 0) == (object, int)
    with pytest.raises(DispatchError):
        cls.method(None, 0.0)
github coady / multimethod / tests / test_methods.py View on Github external
def func(x):
        return x

    @overload
    def func(x: isa(int, float)):
        return -x

    @func.register
    def _(x: isa(str)):
        return x.upper()

    assert func(None) is None
    assert func(1) == -1
    assert func('hi') == 'HI'
    del func[next(iter(func))]
    with pytest.raises(DispatchError):
        func(None)
github coady / multimethod / tests / test_all.py View on Github external
def test_roshambo():
    r, p, s = rock(), paper(), scissors()
    assert len(roshambo) == 7
    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[object, object]
    del roshambo[rock, paper]
    assert len(roshambo) == 5
    with pytest.raises(DispatchError):
        roshambo(r, r)
github coady / multimethod / tests / test_dispatch.py View on Github external
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"):
        cls.method('', '')
    cls.method[object, Iterable] = cls.method[Iterable, object]
    assert cls.method('', '') == 'left'