-class Hook(object):
- def __init__(self, *typeids):
- for typeid in typeids:
- if not isinstance(typeid, type):
- raise ValueError(typeid)
- self.__typeids = typeids
- return super().__init__()
-
- def __iter__(self):
- yield from self.__typeids
-
- def __repr__(self):
- names = []
- for typeid in self.__typeids:
- name = typeid.__qualname__
- module = typeid.__module__
- if module not in ("builtins",):
- name = f"{module}.{name}"
- names.append(name)
- return f"<{', '.join(names)}>"
-
- def __call__(self, call):
- class ConcreteHook(Hook):
- def __call__(self, dispatcher, instance):
- return call(self=dispatcher, instance=instance)
-
- return ConcreteHook(*tuple(self))
-
-
-def hook(*typeids):
- return Hook(*typeids)