Skip to content

Commit 6a417de

Browse files
add coverage for optional hook validation and error cases
1 parent cfa7ed1 commit 6a417de

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

src/pluggy/_manager.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -378,15 +378,16 @@ def check_pending(self) -> None:
378378
hook specification are optional, otherwise raise
379379
:exc:`PluginValidationError`."""
380380
for name in self.hook.__dict__:
381-
if name[0] != "_":
382-
hook: HookCaller = getattr(self.hook, name)
383-
if not hook.has_spec():
384-
for hookimpl in hook.get_hookimpls():
385-
if not hookimpl.optionalhook:
386-
raise PluginValidationError(
387-
hookimpl.plugin,
388-
f"unknown hook {name!r} in plugin {hookimpl.plugin!r}",
389-
)
381+
if name[0] == "_":
382+
continue
383+
hook: HookCaller = getattr(self.hook, name)
384+
if not hook.has_spec():
385+
for hookimpl in hook.get_hookimpls():
386+
if not hookimpl.optionalhook:
387+
raise PluginValidationError(
388+
hookimpl.plugin,
389+
f"unknown hook {name!r} in plugin {hookimpl.plugin!r}",
390+
)
390391

391392
def load_setuptools_entrypoints(self, group: str, name: str | None = None) -> int:
392393
"""Load modules from querying the specified setuptools ``group``.

testing/test_pluginmanager.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,31 @@ def configure(self) -> int:
771771
assert result == [4, 5, 3, 2, 1, 6]
772772

773773

774+
def test_check_pending_skips_underscore(pm: PluginManager) -> None:
775+
# todo: determine what we want to do with the namespace
776+
class Plugin:
777+
@hookimpl
778+
def _problem(self):
779+
pass
780+
781+
pm.register(Plugin())
782+
pm.hook._problem()
783+
pm.check_pending()
784+
785+
786+
def test_check_pending_optionalhook(
787+
pm: PluginManager,
788+
) -> None:
789+
class Plugin:
790+
@hookimpl(optionalhook=True)
791+
def a_hook(self, param):
792+
pass
793+
794+
pm.register(Plugin())
795+
pm.hook.a_hook(param=1)
796+
pm.check_pending()
797+
798+
774799
def test_check_pending_nonspec_hook(
775800
pm: PluginManager,
776801
) -> None:
@@ -784,3 +809,8 @@ def a_hook(self, param):
784809
pm.register(Plugin())
785810
with pytest.raises(HookCallError, match="hook call must provide argument 'param'"):
786811
pm.hook.a_hook()
812+
813+
with pytest.raises(
814+
PluginValidationError, match="unknown hook 'a_hook' in plugin .*"
815+
):
816+
pm.check_pending()

0 commit comments

Comments
 (0)