Repository: pytest-dev/pluggy
Pull request #471 (2024-01-13 08:41:57)

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.


Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index b7ae00f..2ce5732 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -148,7 +148,7 @@ Features
 --------
 
 - `#282 <https://github.com/pytest-dev/pluggy/issues/282>`_: When registering a hookimpl which is declared as ``hookwrapper=True`` but whose
-  function is not a generator function, a :class:`~pluggy.PluginValidationError` exception is
+  function is not a generator function, a ``PluggyValidationError`` exception is
   now raised.
 
   Previously this problem would cause an error only later, when calling the hook.
@@ -158,7 +158,7 @@ Features
 
   .. code-block:: python
 
-      def my_hook_implementation(arg):
+      def my_hook_real_implementation(arg):
           print("before")
           yield
           print("after")
diff --git a/docs/index.rst b/docs/index.rst
index 42fb8f4..5bc90b7 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -416,11 +416,9 @@ of the hook thus far, or, if the previous calls raised an exception, it is
 :py:meth:`thrown <python:generator.throw>` the exception.
 
 The function should do one of two things:
-
-- Return a value, which can be the same value as received from the ``yield``, or something else entirely.
-
+- Return a value, which can be the same value as received from the ``yield``, or
+something else entirely.
 - Raise an exception.
-
 The return value or exception propagate to further hook wrappers, and finally
 to the hook caller.
 
diff --git a/src/pluggy/_manager.py b/src/pluggy/_manager.py
index a984d1d..ce1e107 100644
--- a/src/pluggy/_manager.py
+++ b/src/pluggy/_manager.py
@@ -235,6 +235,16 @@ class PluginManager:
         """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``.
 


Test patch:
diff --git a/testing/test_pluginmanager.py b/testing/test_pluginmanager.py
index 81b86b6..7f09d9b 100644
--- a/testing/test_pluginmanager.py
+++ b/testing/test_pluginmanager.py
@@ -110,6 +110,13 @@ def test_set_blocked(pm: PluginManager) -> None:
     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: