dispatcher: provide better conflicts diagnostics
authorDmitry Selyutin <ghostmansd@gmail.com>
Tue, 13 Jun 2023 22:38:43 +0000 (01:38 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Tue, 13 Jun 2023 22:38:43 +0000 (01:38 +0300)
src/mdis/dispatcher.py

index 009f946bc6888749c55b1f46e8acd2064af6d018..c2c85f483cfb1fcfdf4a54021e90c11a1112345e 100644 (file)
@@ -1,3 +1,4 @@
+import collections as _collections
 import inspect as _inspect
 import operator as _operator
 
@@ -6,18 +7,19 @@ from . import core as _core
 
 class DispatcherMeta(type):
     def __new__(metacls, name, bases, ns):
-        hooks = {}
-
+        conflicts = _collections.defaultdict(list)
         for (key, value) in tuple(ns.items()):
             if not isinstance(value, _core.CallHook):
                 continue
             hook = value
             for typeid in hook:
-                if typeid in hooks:
-                    raise ValueError(f"conflicting hook: {typeid!r}")
-                hooks[typeid] = hook
+                conflicts[typeid].append(key)
             ns[key] = hook
 
+        for (typeid, keys) in conflicts.items():
+            if len(keys) > 1:
+                raise ValueError(f"dispatch conflict: {keys!r}")
+
         return super().__new__(metacls, name, bases, ns)
 
     def __init__(cls, name, bases, ns):