Skip to content

Question about default values in hook implementation #442

Open
@krassowski

Description

I encountered a somewhat confusing behaviour in python-lsp/python-lsp-server#439 were I observe that the hook implementation default arguments always take precedence over the values passed to the hook at runtime. This can be reproduced using the following snippet:

import pluggy

hookspec = pluggy.HookspecMarker("test")
hookimpl = pluggy.HookimplMarker("test")

class MySpec:

    @hookspec
    def myhook(self, arg1, arg2):
        pass

class Plugin_1:

    @hookimpl
    def myhook(self, arg1, arg2=0):
        return arg1 + arg2

pm = pluggy.PluginManager("test")
pm.add_hookspecs(MySpec)
pm.register(Plugin_1())
results = pm.hook.myhook(arg1=1, arg2=2)
print(results)

which prints [1] rather than [3] which I would expect. I see this issue in Python 3.8 and 3.11.

I do not see any explanation of this behaviour, which appears highly unexpected on https://pluggy.readthedocs.io/en/latest/ but I also do not see any hook implementations with default values there either. As such I am confused as to whether this is intended behaviour or not. Which is it?

Activity

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

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