Skip to content

Commit 926084e

Browse files
authored
Merge pull request #379 from vitek/spec-conflict
Detailed error message on hook registration conflict
2 parents 8103269 + 33a6936 commit 926084e

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/pluggy/_hooks.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,11 @@ def set_specification(
357357
specmodule_or_class: _Namespace,
358358
spec_opts: _HookSpecOpts,
359359
) -> None:
360-
assert not self.has_spec()
360+
if self.spec is not None:
361+
raise ValueError(
362+
f"Hook {self.spec.name!r} is already registered "
363+
f"within namespace {self.spec.namespace}"
364+
)
361365
self.spec = HookSpec(specmodule_or_class, self.name, spec_opts)
362366
if spec_opts.get("historic"):
363367
self._call_history = []

testing/test_hookcaller.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,23 @@ def hello(self, arg: int) -> int:
428428
pm.register(Plugin2())
429429
with pytest.raises(PluginValidationError):
430430
pm.check_pending()
431+
432+
433+
def test_hook_conflict(pm: PluginManager) -> None:
434+
class Api1:
435+
@hookspec
436+
def conflict(self) -> None:
437+
"""Api1 hook"""
438+
439+
class Api2:
440+
@hookspec
441+
def conflict(self) -> None:
442+
"""Api2 hook"""
443+
444+
pm.add_hookspecs(Api1)
445+
with pytest.raises(ValueError) as exc:
446+
pm.add_hookspecs(Api2)
447+
assert str(exc.value) == (
448+
"Hook 'conflict' is already registered within namespace "
449+
"<class 'test_hookcaller.test_hook_conflict.<locals>.Api1'>"
450+
)

0 commit comments

Comments
 (0)