Registering the same Pluggy hook multiple times in a single file

I found myself wanting to register more than one instance of a Pluggy plugin hook inside a single module.

Hooks are usually registered like this:

@hookimpl
def filters_from_request(request, database, datasette):
    # ...

Where filters_from_request matches the name of a registered plugin hook.

It turns out you can do this instead:

@hookimpl(specname="filters_from_request")
def filters_from_request_1(request, database, datasette):
    # ...

    
@hookimpl(specname="filters_from_request")
def filters_from_request_2(request, database, datasette):
    # ...

Which allows you to write more than one plugin implementation function in the same Python module file.

Note that the specname feature requires Pluggy 1.0.0 or higher.

Created 2021-12-16T15:16:45-08:00, updated 2021-12-19T17:46:19-08:00 · History · Edit