Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/774.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Positional-only parameters in hook implementations are deprecated and now emit a :class:`DeprecationWarning` when the implementation is verified against its specification.
Hook arguments are matched by name, so declare them as regular parameters. This will become an error in pluggy 2.0.
28 changes: 28 additions & 0 deletions src/pluggy/_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ def _warn_for_function(warning: Warning, function: Callable[..., object]) -> Non
)


def _positional_only_names(
function: Callable[..., object], names: Sequence[str]
) -> tuple[str, ...]:
"""Return the names among ``names`` which ``function`` declares as
positional-only."""
func = inspect.unwrap(function)
code = getattr(getattr(func, "__func__", func), "__code__", None)
if code is None: # pragma: no cover
return ()
posonly = code.co_varnames[: code.co_posonlyargcount]
return tuple(name for name in posonly if name in names)


_ABSENT: Final = object()


Expand Down Expand Up @@ -464,6 +477,21 @@ def _verify_hook(self, hook: HookCaller, hookimpl: HookImpl) -> None:
"can not be found in the hookspec",
)

posonly = _positional_only_names(
hookimpl.function, hookimpl.argnames + hookimpl.kwargnames
)
if posonly:
_warn_for_function(
DeprecationWarning(
f"Plugin {hookimpl.plugin_name!r} for hook {hook.name!r}\n"
f"hookimpl definition: {_formatdef(hookimpl.function)}\n"
f"Positional-only parameter(s) {posonly} are deprecated, "
"hook arguments are matched by name. "
"This will become an error in pluggy 2.0."
),
hookimpl.function,
)

if hook.spec.warn_on_impl_args:
for hookimpl_argname in hookimpl.argnames + hookimpl.kwargnames:
argname_warning = hook.spec.warn_on_impl_args.get(hookimpl_argname)
Expand Down
45 changes: 45 additions & 0 deletions testing/test_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,51 @@ def foo(self, old=None, new=None):
assert record.lineno == Plugin.foo.__code__.co_firstlineno


@pytest.mark.parametrize("spec_first", [True, False])
def test_warn_on_positional_only_impl_args(spec_first: bool) -> None:
class Spec:
@hookspec
def foo(self, arg, other=None):
raise NotImplementedError()

class Plugin:
@hookimpl
def foo(self, arg, /, other=None):
raise NotImplementedError()

pm = PluginManager(hookspec.project_name)
if spec_first:
pm.add_hookspecs(Spec)
with pytest.warns(DeprecationWarning, match="Positional-only") as records:
pm.register(Plugin(), name="plugin")
else:
pm.register(Plugin(), name="plugin")
with pytest.warns(DeprecationWarning, match="Positional-only") as records:
pm.add_hookspecs(Spec)
(record,) = records
assert "('arg',)" in str(record.message)
assert record.filename == Plugin.foo.__code__.co_filename
assert record.lineno == Plugin.foo.__code__.co_firstlineno


def test_no_warning_for_positional_only_self(recwarn) -> None:
class Spec:
@hookspec
def foo(self, arg):
raise NotImplementedError()

class Plugin:
@hookimpl
def foo(self, /, arg):
return arg

pm = PluginManager(hookspec.project_name)
pm.add_hookspecs(Spec)
pm.register(Plugin())
assert pm.hook.foo(arg=1) == [1]
assert not recwarn.list


def test_plugin_getattr_raises_errors() -> None:
"""Pluggy must be able to handle plugins which raise weird exceptions
when getattr() gets called (#11).
Expand Down