Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/827.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :meth:`~pluggy.PluginManager.subset_hook_caller` calling a plugin from ``remove_plugins`` that was registered after the subset hook caller was created, a regression in 1.7.0.
16 changes: 10 additions & 6 deletions src/pluggy/_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,12 +642,16 @@ 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}
# Optimization: discard plugins to remove which are registered but don't
# implement the hook, to make checks faster. A plugin that isn't
# registered yet is kept, since the proxy is live and must still exclude
# it if it gets registered later.
plugins_to_remove = {
plugin
for plugin in remove_plugins
if plugin in implementing or not self.is_registered(plugin)
}
# Optimization: if none of the plugins to remove actually implement the
# hook, avoid the subset hook caller overhead.
if not plugins_to_remove:
Expand Down
33 changes: 33 additions & 0 deletions testing/test_pluginmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,39 @@ def he_method1(self):
assert hc() == [2]


def test_subset_hook_caller_excludes_plugin_registered_later(pm: PluginManager) -> None:
"""A plugin to remove that is registered after the proxy exists stays excluded."""

class Hooks:
@hookspec
def he_method1(self):
pass

pm.add_hookspecs(Hooks)

class Plugin:
def __init__(self, name: str) -> None:
self.name = name

@hookimpl
def he_method1(self):
return self.name

early, late, kept = Plugin("early"), Plugin("late"), Plugin("kept")
pm.register(early)

# Only `early` is registered when the proxies are created.
hc_late_only = pm.subset_hook_caller("he_method1", remove_plugins=[late])
hc_both = pm.subset_hook_caller("he_method1", remove_plugins=[early, late])

pm.register(late)
pm.register(kept)

assert pm.hook.he_method1() == ["kept", "late", "early"]
assert hc_late_only() == ["kept", "early"]
assert hc_both() == ["kept"]


def test_get_hookimpls(pm: PluginManager) -> None:
class Hooks:
@hookspec
Expand Down
Loading