pytest-dev / pytest-dev/pluggy

Question about default values in hook implementation

Open
#442 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.7k
Forks
160
Avg merge
21h 4m
Merged PRs (30d)
6

Description

I encountered a somewhat confusing behaviour in https://github.com/python-lsp/python-lsp-server/issues/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?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the MySpec and Plugin_1 reproduction in the issue, then review the hook invocation and the linked pluggy documentation. Confirm the intended behavior with maintainers; done means the default-argument behavior and its expected documentation treatment are clearly established.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.