Skip to content

Get plugin corresponding to return values when calling hook #235

Open

Description

@youngmit

Perhaps this is already possible, and I am just missing something, but is it possible to determine which plugin returned each value from a hook call? This would be valuable information for providing error messages and the like in situations where one and only one plugin should return a value under certain circumstances. Being able to report which plugins returned which values would be nice. Perhaps something like an alternative hook calling mechanism, which would return a dictionary (keyed on plugin namespace), rather than a list?

Activity

  1. simonw commented on Oct 30, 2020

    @simonw
    Contributor

    I'm interested in this too, also for debugging purposes. I'd like to display an HTML comment on a page that shows which plugin was responsible for returning a specific piece of information.

  2. fschulze commented on Jul 7, 2024

    @fschulze
    Contributor

    I'm also in need to trace which plugin returned the result, so I wrote this:

    def traced_pluggy_call(hook, **caller_kwargs):
        firstresult = hook.spec.opts.get("firstresult", False) if hook.spec else False
        results = []
        plugin_names = []
        hookimpls = hook._hookimpls if hasattr(hook, '_hookimpls') else hook.get_hookimpls()
        for hook_impl in reversed(hookimpls):
            args = [caller_kwargs[argname] for argname in hook_impl.argnames]
            res = hook_impl.function(*args)
            if res is not None:
                results.append(res)
                plugin_names.append(hook_impl.plugin_name)
                if firstresult:
                    break
        if firstresult:
            results = results[0] if results else None
        return (results, plugin_names)

    Would be great if such functionality could be added somehow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions