def method_dispatch(func: Callable[..., T]) -> Callable[..., T]:
"""
Single-dispatch class method decorator
Works like functools.singledispatch for none-static class methods.
"""
dispatcher = functools.singledispatch(func)
@functools.wraps(func)
def wrapper(*args: Any, **_: Any) -> T:
return dispatcher.dispatch(args[1].__class__)(*args, **_)
# issue: https://github.com/python/mypy/issues/708
wrapper.register = dispatcher.register # type: ignore
return wrapper
评论列表
文章目录