From 263a3621988e41c8a0a362e82e22fed974470193 Mon Sep 17 00:00:00 2001 From: EnesKartal Date: Tue, 24 Nov 2026 00:55:44 +0300 Subject: [PATCH 1/2] fix: exclude unregistered plugins in subset_hook_caller with O(1) lookup --- src/pluggy/_manager.py | 26 ++++++++++++++++++-------- testing/test_pluginmanager.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/pluggy/_manager.py b/src/pluggy/_manager.py index 15214676..eb514a59 100644 --- a/src/pluggy/_manager.py +++ b/src/pluggy/_manager.py @@ -159,6 +159,7 @@ def __init__(self, project_name: str) -> None: #: The project name. self.project_name: Final = project_name self._name2plugin: Final[dict[str, _Plugin]] = {} + self._plugin2name: Final[dict[_Plugin, str]] = {} self._name2dist: Final[dict[str, importlib.metadata.Distribution]] = {} #: The "hook relay", used to call a hook on all registered plugins. #: See :ref:`calling`. @@ -239,6 +240,10 @@ def register(self, plugin: _Plugin, name: str | None = None) -> str | None: hookimpls.append((hook_name, hookimpl, verified)) self._name2plugin[plugin_name] = plugin + try: + self._plugin2name[plugin] = plugin_name + except TypeError: + pass for hook_name, hookimpl, verified in hookimpls: hook = getattr(self.hook, hook_name, None) if hook is None: @@ -323,6 +328,10 @@ def unregister( if not self.is_blocked(name): self._name2plugin.pop(name, None) + try: + self._plugin2name.pop(plugin, None) + except TypeError: + pass self._name2dist.pop(name, None) return plugin @@ -404,7 +413,10 @@ def get_plugins(self) -> set[Any]: def is_registered(self, plugin: _Plugin) -> bool: """Return whether the plugin is already registered.""" - return any(plugin == val for val in self._name2plugin.values()) + try: + return plugin in self._plugin2name + except TypeError: + return any(plugin == val for val in self._name2plugin.values()) def get_canonical_name(self, plugin: _Plugin) -> str: """Return a canonical name for a plugin object. @@ -649,14 +661,12 @@ def subset_hook_caller( """Return a proxy :class:`~pluggy.HookCaller` for the named hook which calls all registered plugins except the ones from remove_plugins.""" orig: HookCaller = getattr(self.hook, name) - plugins_to_remove = set(remove_plugins) - # Optimization: discard plugins to remove which don't actually implement - # the hook, to make checks faster. - plugins_to_remove.intersection_update( - hookimpl.plugin for hookimpl in orig._hookimpls - ) + implementing = {hookimpl.plugin for hookimpl in orig._hookimpls} + plugins_to_remove = { + p for p in remove_plugins if p in implementing or not self.is_registered(p) + } # Optimization: if none of the plugins to remove actually implement the - # hook, avoid the subset hook caller overhead. + # hook or could be registered later, avoid the subset hook caller overhead. if not plugins_to_remove: return orig return _SubsetHookCaller(orig, plugins_to_remove) diff --git a/testing/test_pluginmanager.py b/testing/test_pluginmanager.py index be34efb1..28aecb2d 100644 --- a/testing/test_pluginmanager.py +++ b/testing/test_pluginmanager.py @@ -813,6 +813,38 @@ def he_method1(self): assert hc() == [2] +def test_subset_hook_caller_excludes_later_registered_plugins(pm: PluginManager) -> None: + class Hooks: + @hookspec + def he_method1(self): + pass + + pm.add_hookspecs(Hooks) + + class Plugin1: + @hookimpl + def he_method1(self): + return 1 # pragma: no cover + + class Plugin2: + @hookimpl(specname="he_method1") + def alias(self): + return 2 # pragma: no cover + + class Plugin3: + @hookimpl + def he_method1(self): + return 3 + + plugin1, plugin2, plugin3 = Plugin1(), Plugin2(), Plugin3() + pm.register(plugin3) + hc = pm.subset_hook_caller("he_method1", remove_plugins=[plugin1, plugin2]) + + pm.register(plugin1) + pm.register(plugin2) + assert hc() == [3] + + def test_get_hookimpls(pm: PluginManager) -> None: class Hooks: @hookspec From e7cb9ad292f6b21431a9d6438f6e5d4dfd704330 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 21:56:01 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- testing/test_pluginmanager.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/testing/test_pluginmanager.py b/testing/test_pluginmanager.py index 28aecb2d..d9af89f8 100644 --- a/testing/test_pluginmanager.py +++ b/testing/test_pluginmanager.py @@ -813,7 +813,9 @@ def he_method1(self): assert hc() == [2] -def test_subset_hook_caller_excludes_later_registered_plugins(pm: PluginManager) -> None: +def test_subset_hook_caller_excludes_later_registered_plugins( + pm: PluginManager, +) -> None: class Hooks: @hookspec def he_method1(self):