From 13b3661080cd3f6dcae1dedbceac6814b740c6dc Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 13 Jan 2024 10:39:11 +0200 Subject: [PATCH] Add `PluginManager.unblock` method to unblock a name Pytest currently accesses internals to do this: https://github.com/pytest-dev/pytest/blob/1b78de4e21d55983422e7327a51304ef0fc61679/src/_pytest/config/__init__.py#L740-L746 Let's provide a proper API for this. --- src/pluggy/_manager.py | 10 ++++++++++ testing/test_pluginmanager.py | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/src/pluggy/_manager.py b/src/pluggy/_manager.py index a984d1d7..ce1e107a 100644 --- a/src/pluggy/_manager.py +++ b/src/pluggy/_manager.py @@ -235,6 +235,16 @@ def is_blocked(self, name: str) -> bool: """Return whether the given plugin name is blocked.""" return name in self._name2plugin and self._name2plugin[name] is None + def unblock(self, name: str) -> bool: + """Unblocks a name. + + Returns whether the name was actually blocked. + """ + if self._name2plugin.get(name, -1) is None: + del self._name2plugin[name] + return True + return False + def add_hookspecs(self, module_or_class: _Namespace) -> None: """Add new hook specifications defined in the given ``module_or_class``. diff --git a/testing/test_pluginmanager.py b/testing/test_pluginmanager.py index 81b86b65..7f09d9b4 100644 --- a/testing/test_pluginmanager.py +++ b/testing/test_pluginmanager.py @@ -110,6 +110,13 @@ class A: pm.unregister(name="somename") assert pm.is_blocked("somename") + # Unblock. + assert not pm.unblock("someothername") + assert pm.unblock("somename") + assert not pm.is_blocked("somename") + assert not pm.unblock("somename") + assert pm.register(A(), "somename") + def test_register_mismatch_method(he_pm: PluginManager) -> None: class hello: