diff --git a/changelog/774.removal.rst b/changelog/774.removal.rst new file mode 100644 index 00000000..3cf5f3e1 --- /dev/null +++ b/changelog/774.removal.rst @@ -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. diff --git a/src/pluggy/_manager.py b/src/pluggy/_manager.py index 572e5b71..6caff827 100644 --- a/src/pluggy/_manager.py +++ b/src/pluggy/_manager.py @@ -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() @@ -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) diff --git a/testing/test_details.py b/testing/test_details.py index 8e4c35a9..754d1e33 100644 --- a/testing/test_details.py +++ b/testing/test_details.py @@ -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).