Skip to content

Point the missing-argument warning at the code making the hook call - #727

Open
dylanpulver wants to merge 2 commits into
pytest-dev:mainfrom
dylanpulver:fix/missing-arg-warning-stacklevel
Open

Point the missing-argument warning at the code making the hook call#727
dylanpulver wants to merge 2 commits into
pytest-dev:mainfrom
dylanpulver:fix/missing-arg-warning-stacklevel

Conversation

@dylanpulver

Copy link
Copy Markdown

The warning for hookspec arguments missing from a hook call is attributed to pluggy's own frame instead of to the code that made the call:

pm.hook.hello(arg1=1)     # user_code.py:22, arg2 missing from the spec
attributed to : _hooks.py:539     <- HookCaller.__call__
expected      : user_code.py:22

e77787b ("hooks: also verify kwargs are per spec in call_historic") moved the warnings.warn out of HookCaller.__call__ and into the new _verify_all_args_are_provided so call_historic could share it, and carried stacklevel=2 over unchanged. Inside __call__ that level was the caller; inside the helper it is __call__ itself. All three entry points call the helper directly, so all three are off by the same one:

call path before after
__call__ _hooks.py:539 caller
call_historic _hooks.py:559 caller
call_extra _hooks.py:580 caller

stacklevel=3 restores the pre-e77787b behaviour for __call__ and extends it to the two paths that never had it.

test_not_all_arguments_are_provided_issues_a_warning already covers all three paths, but only matches the message, so the attribution regressed silently. The new test asserts record[0].filename == __file__ for each path; with stacklevel=2 restored it fails and nothing else does.

pytest testing/: 145 passed (144 passed / 1 failed on the reverted source). pre-commit run --files src/pluggy/_hooks.py testing/test_details.py: ruff check, ruff format, autoflake, pyupgrade, blacken-docs and mypy all pass.

Happy to add a changelog/<PR>.bugfix.rst fragment once this has a number, or to drop the test if you would rather keep the assertion to the message only.

🤖 Generated with Claude Code

https://claude.ai/code/session_011M5uTyCU4WcNTsPvGrErDo

dylanpulver and others added 2 commits September 9, 2026 00:03
The warning for hookspec arguments missing from a hook call was moved out
of HookCaller.__call__ and into _verify_all_args_are_provided in e77787b,
so that call_historic could share it, but it kept stacklevel=2. Inside
the helper that level is __call__ itself, so every such warning is
attributed to pluggy's own frame in _hooks.py rather than to the caller.

All three entry points reach the helper at the same depth, so
stacklevel=3 restores the pre-e77787b attribution for __call__ and
extends it to call_historic and call_extra.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011M5uTyCU4WcNTsPvGrErDo
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011M5uTyCU4WcNTsPvGrErDo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant