Remove singledispatch wrapper

This commit is contained in:
Arjan Molenaar 2020-10-23 18:14:35 +02:00
parent 9a0f23360d
commit 88ce9f5f6c
2 changed files with 1 additions and 80 deletions

View File

@ -2,39 +2,13 @@
Aspects form intermediate items between tools and items.
"""
import warnings
from functools import singledispatch as real_singledispatch
from functools import singledispatch
from gi.repository import Gdk
from gaphas.item import Element
def singledispatch(func):
"""Wrapper around singledispatch(), with an extra compatibility function so
code will not break when upgrading from 1.0 to 1.1."""
wrapped = real_singledispatch(func)
def when_type(*types):
if not types:
raise TypeError("should provide at least one type")
warnings.warn(
"when_type: is deprecated, use `register` instead",
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 # type: ignore[attr-defined]
return wrapped
class ItemFinder:
"""Find an item on the canvas."""

View File

@ -1,53 +0,0 @@
import pytest
from gaphas.aspect import singledispatch
class Custom:
pass
class Another:
pass
@singledispatch
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