From 3f9c827b01a44899c5e164e5223a3f47132e80ef Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Sun, 18 Jun 2023 21:34:46 +0300 Subject: [PATCH] dispatcher: support arbitrary callables --- src/mdis/dispatcher.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/mdis/dispatcher.py b/src/mdis/dispatcher.py index 6f6b9cd..7c41785 100644 --- a/src/mdis/dispatcher.py +++ b/src/mdis/dispatcher.py @@ -6,7 +6,7 @@ import types as _types class Hook(object): def __init__(self, *typeids): for typeid in typeids: - if not isinstance(typeid, type): + if not callable(typeid): raise ValueError(typeid) self.__typeids = typeids return super().__init__() @@ -63,7 +63,13 @@ class DispatcherMeta(type): return super().__new__(metacls, name, bases, ns) def dispatch(cls, typeid=object): - return cls.__hooks__.get(typeid) + hook = cls.__hooks__.get(typeid) + if hook is not None: + return hook + for (checker, hook) in cls.__hooks__.items(): + if not isinstance(checker, type) and checker(typeid): + return hook + return None class Dispatcher(metaclass=DispatcherMeta): -- 2.30.2