hookspecs: add `plugin_name` parameter to the `pytest_plugin_registered` hook

We have a use case for this in the next commit.

The name can be obtained by using `manager.get_name(plugin)`, however
this is currently O(num plugins) in pluggy, which would be good to
avoid. Besides, it seems generally useful.
This commit is contained in:
Ran Benita 2024-01-17 14:47:07 +02:00
parent 6b9bba2edb
commit 0f5ecd83c4
3 changed files with 14 additions and 6 deletions

View File

@ -0,0 +1 @@
The :hook:`pytest_plugin_registered` hook has a new ``plugin_name`` parameter containing the name by which ``plugin`` is registered.

View File

@ -490,15 +490,19 @@ class PytestPluginManager(PluginManager):
)
)
return None
ret: Optional[str] = super().register(plugin, name)
if ret:
plugin_name = super().register(plugin, name)
if plugin_name is not None:
self.hook.pytest_plugin_registered.call_historic(
kwargs=dict(plugin=plugin, manager=self)
kwargs=dict(
plugin=plugin,
plugin_name=plugin_name,
manager=self,
)
)
if isinstance(plugin, types.ModuleType):
self.consider_module(plugin)
return ret
return plugin_name
def getplugin(self, name: str):
# Support deprecated naming because plugins (xdist e.g.) use it.

View File

@ -63,12 +63,15 @@ def pytest_addhooks(pluginmanager: "PytestPluginManager") -> None:
@hookspec(historic=True)
def pytest_plugin_registered(
plugin: "_PluggyPlugin", manager: "PytestPluginManager"
plugin: "_PluggyPlugin",
plugin_name: str,
manager: "PytestPluginManager",
) -> None:
"""A new pytest plugin got registered.
:param plugin: The plugin module or instance.
:param manager: pytest plugin manager.
:param plugin_name: The name by which the plugin is registered.
:param manager: The pytest plugin manager.
.. note::
This hook is incompatible with hook wrappers.