Skip to content

Commit

Permalink
Merge pull request #379 from vitek/spec-conflict
Browse files Browse the repository at this point in the history
Detailed error message on hook registration conflict
  • Loading branch information
bluetech authored Jun 21, 2023
2 parents 8103269 + 33a6936 commit 926084e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/pluggy/_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,11 @@ def set_specification(
specmodule_or_class: _Namespace,
spec_opts: _HookSpecOpts,
) -> None:
assert not self.has_spec()
if self.spec is not None:
raise ValueError(
f"Hook {self.spec.name!r} is already registered "
f"within namespace {self.spec.namespace}"
)
self.spec = HookSpec(specmodule_or_class, self.name, spec_opts)
if spec_opts.get("historic"):
self._call_history = []
Expand Down
20 changes: 20 additions & 0 deletions testing/test_hookcaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,23 @@ def hello(self, arg: int) -> int:
pm.register(Plugin2())
with pytest.raises(PluginValidationError):
pm.check_pending()


def test_hook_conflict(pm: PluginManager) -> None:
class Api1:
@hookspec
def conflict(self) -> None:
"""Api1 hook"""

class Api2:
@hookspec
def conflict(self) -> None:
"""Api2 hook"""

pm.add_hookspecs(Api1)
with pytest.raises(ValueError) as exc:
pm.add_hookspecs(Api2)
assert str(exc.value) == (
"Hook 'conflict' is already registered within namespace "
"<class 'test_hookcaller.test_hook_conflict.<locals>.Api1'>"
)

0 comments on commit 926084e

Please sign in to comment.