Possible to tell which hook answered a call?
I have some code that looks like this:
opinions = []
# Every plugin is consulted for their opinion
for check in pm.hook.permission_allowed(
datasette=self,
actor=actor,
action=action,
resource=resource,
):
check = await await_me_maybe(check)
if check is not None:
opinions.append(check)
result = None
# If any plugin said False it's false - the veto rule
if any(not r for r in opinions):
result = False
elif any(r for r in opinions):
# Otherwise, if any plugin said True it's true
result = True
This is for a plugin-based permission model.
For easier debugging of complex situations (where more than one plugin might have contributed an opinion) I'd like to be able to tell which plugin returned which values.
Is there a way I can do this with Pluggy? If not, could I request it as a feature?
The details are not passed back, however hook tracing should be able to help debug
I also need this ability for similar reasons, I'm using plugins to implement a lot of extractors that consume URLs and produce various outputs.
It would be very helpful to see which extractor plugin produced a given output, without having to include it manually in every response.
This would be possible if there were a function available like:
PluginManager.get_hookimpl(plugin_name, hook_name, fallback)
Then you could prepare a dict of {plugin: handler} ahead of time, and then call each handler manually:
url = 'https://example.com'
hook_to_call = 'extract_url'
handlers_by_plugin = {
plugin_name: pm.get_hookimpl(plugin, hook_to_call, None)
for plugin_name in pm.get_plugins()
}
results_by_plugin = {
plugin_name: handler(url)
for plugin_name, handler in handlers_by_plugin.items()
if handler is not None
}
Can I submit a PR to add HookCaller.get_hookimpl to the PluginManager? (with the difference being that it accepts a plugin ID + hook name as args)
This could kill two birds with one stone and provide a way to control call ordering at runtime too: https://github.com/pytest-dev/pluggy/issues/250#issuecomment-2378634364
# example of customizing call order using handlers_by_plugin from above
preferred_order = ['wget', 'singlefile', 'dom']
results = {}
# process ones with a defined order first
for plugin in preferred_order:
results[plugin] = handlers_by_plugin.pop(plugin)(url)
# then process all other handlers
for plugin in handlers_by_plugin.values():
results[plugin] = handlers_by_plugin.pop(plugin)(url)
Alternatively this could also be another proxied HookCaller like https://github.com/pytest-dev/pluggy/issues/250#issuecomment-2378426205 that responded with a dict of {'pluginname': response, 'pluginname2': response, ...}. (like PluginManager.subset_hook_caller())
i strongly want to avoid adding a way to return the plugin in a normal way - as from the design standpoint needing the plugin to be returned to do something sensible means the hook is not engineered correctly
i want to reduce the ways one can call/interpret hooks as historic hooks already create quite a problem in terms of structures (i need to take some time to write down a design document for a replacment for historic hooks)
@simonw superseeding this with #588 - will land after async