-class TypeidHook(object):
+class Hook(object):
def __init__(self, *typeids):
for typeid in typeids:
if not isinstance(typeid, type):
return f"<{', '.join(names)}>"
def __call__(self, call):
- if not callable(call):
- raise ValueError(call)
- return CallHook(typeids=self, call=call)
+ class ConcreteHook(Hook):
+ def __call__(self, dispatcher, instance):
+ return call(self=dispatcher, instance=instance)
-
-class CallHook(object):
- def __init__(self, typeids, call):
- if not isinstance(typeids, TypeidHook):
- raise ValueError(typeids)
- if not callable(call):
- raise ValueError(call)
- self.__typeids = typeids
- self.__call = call
- return super().__init__()
-
- def __iter__(self):
- yield from self.__typeids
-
- def __repr__(self):
- return repr(self.__typeids)
-
- def __call__(self, dispatcher, instance):
- return self.__call(dispatcher, instance)
+ return ConcreteHook(*tuple(self))
def hook(*typeids):
- return TypeidHook(*typeids)
+ return Hook(*typeids)
class DispatcherMeta(type):
+ __hooks__ = {}
+
def __new__(metacls, name, bases, ns):
hooks = {}
- ishook = lambda member: isinstance(member, _core.CallHook)
+ ishook = lambda member: isinstance(member, _core.Hook)
for basecls in reversed(bases):
members = _inspect.getmembers(basecls, predicate=ishook)