Fix formatting

This commit is contained in:
Arjan Molenaar 2019-06-13 21:49:28 +02:00
parent 45166e2d3a
commit 93f4914a6d
2 changed files with 13 additions and 1 deletions

View File

@ -33,10 +33,12 @@ def singledispatch(func):
category=DeprecationWarning,
stacklevel=2,
)
def wrapper_for_types(func):
for cls in types:
wrapped.register(cls, func)
return func
return wrapper_for_types
wrapped.when_type = when_type

View File

@ -1,9 +1,11 @@
import pytest
from gaphas.aspect import singledispatch
class Custom:
pass
class Another:
pass
@ -12,31 +14,39 @@ class Another:
def f(o):
return object, o
@f.register(str)
def _str_dispatcher(s):
return str, s
@f.when_type(int)
def _int_dispatcher(i):
return int, i
@f.when_type(Custom, float)
def _bool_float_dispatcher(i):
return Custom, i
def test_singledispatch_with_registered_function():
assert (str, "abc") == f("abc")
def test_singledispatch_with_when_type_function():
assert (int, 3) == f(3)
def test_singledispatch_with_when_type_function_and_multiple_types():
custom = Custom()
assert (Custom, 3.0) == f(3.0)
assert (Custom, custom) == f(custom)
def test_singledispatch_with_when_type_and_no_types():
with pytest.raises(TypeError):
@f.when_type()
def errorous():
pass
pass