From c815dad789488759ebc146322146c6defbe9373a Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Wed, 14 Jun 2023 01:38:43 +0300 Subject: [PATCH] dispatcher: provide better conflicts diagnostics --- src/mdis/dispatcher.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/mdis/dispatcher.py b/src/mdis/dispatcher.py index 009f946..c2c85f4 100644 --- a/src/mdis/dispatcher.py +++ b/src/mdis/dispatcher.py @@ -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): -- 2.30.2