Skip to content

Commit baa68a2

Browse files
Merge pull request #844 from arieleven/fix/staticmethod-self-cls-param
Keep a staticmethod's first parameter when it is named cls
2 parents a0e4747 + 9d42e9d commit baa68a2

6 files changed

Lines changed: 66 additions & 6 deletions

File tree

‎changelog/844.bugfix.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A :class:`staticmethod` hook specification or implementation whose first parameter is named ``cls`` no longer loses that parameter (`#844 <https://github.com/pytest-dev/pluggy/pull/844>`_).

‎src/pluggy/_decorators.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def setattr_hookimpl_opts(func: _F) -> _F:
246246

247247

248248
def varnames(
249-
func: object, *, legacy_noself: bool = False
249+
func: object, *, legacy_noself: bool = False, is_staticmethod: bool = False
250250
) -> tuple[tuple[str, ...], tuple[str, ...]]:
251251
"""Return tuple of positional and keyword parameter names for a callable.
252252
@@ -260,6 +260,9 @@ def varnames(
260260
If ``True``, support hookspec classes whose methods omit ``self``.
261261
When the function looks like a class method but has no implicit first
262262
parameter, a :class:`DeprecationWarning` is emitted.
263+
:param is_staticmethod:
264+
If ``True``, ``func`` was obtained from a :class:`staticmethod`, so its
265+
first parameter is never implicit, whatever its name.
263266
"""
264267
is_bound = False
265268
if inspect.isclass(func):
@@ -308,7 +311,7 @@ def varnames(
308311
# means it's a class method (e.g. "MyClass.method" or
309312
# "func.<locals>.MyClass.method"), not just a nested function.
310313
_tail = qualname.rsplit("<locals>.", maxsplit=1)[-1]
311-
_is_class_method = "." in _tail
314+
_is_class_method = "." in _tail and not is_staticmethod
312315
if args:
313316
if is_bound or (_is_class_method and args[0] in _IMPLICIT_NAMES):
314317
args = args[1:]
@@ -343,11 +346,13 @@ def __init__(self, namespace: _Namespace, name: str, opts: HookspecOpts) -> None
343346
self.namespace = namespace
344347
self.name = name
345348
self.function: Callable[..., object] = getattr(namespace, name)
346-
legacy_noself = inspect.isclass(namespace) and not isinstance(
349+
is_staticmethod = isinstance(
347350
inspect.getattr_static(namespace, name), staticmethod
348351
)
349352
self.argnames, self.kwargnames = varnames(
350-
self.function, legacy_noself=legacy_noself
353+
self.function,
354+
legacy_noself=inspect.isclass(namespace) and not is_staticmethod,
355+
is_staticmethod=is_staticmethod,
351356
)
352357
defaults = inspect.unwrap(self.function).__defaults__
353358
self.kwargdefaults = dict(zip(self.kwargnames, defaults or ()))

‎src/pluggy/_impl.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ def __init__(
4646
plugin_name: str,
4747
function: _HookImplFunction[object],
4848
hook_impl_opts: HookimplOpts,
49+
*,
50+
is_staticmethod: bool = False,
4951
) -> None:
5052
""":meta private:"""
5153
#: The hook implementation function.
5254
self.function: Final = function
53-
argnames, kwargnames = varnames(self.function)
55+
argnames, kwargnames = varnames(self.function, is_staticmethod=is_staticmethod)
5456
#: The positional parameter names of ``function``.
5557
self.argnames: Final = argnames
5658
#: The keyword parameter names of ``function`` which declare defaults.

‎src/pluggy/_manager.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,13 @@ def register(self, plugin: _Plugin, name: str | None = None) -> str | None:
229229
f"{plugin!r}.{attr_name} is not a hookable attribute"
230230
)
231231
method: _HookImplFunction[object] = found[1]
232-
hookimpl = HookImpl(plugin, plugin_name, method, hookimpl_opts)
232+
hookimpl = HookImpl(
233+
plugin,
234+
plugin_name,
235+
method,
236+
hookimpl_opts,
237+
is_staticmethod=isinstance(found[0], staticmethod),
238+
)
233239
hook_name = hookimpl_opts.get("specname") or attr_name
234240
hook: HookCaller | None = getattr(self.hook, hook_name, None)
235241
verified = False

‎testing/test_helpers.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ def sm(x, y=1) -> None:
168168
assert varnames(MyClass().sm) == (("x",), ("y",))
169169

170170

171+
def test_varnames_staticmethod_first_param_named_cls() -> None:
172+
class MyClass:
173+
@staticmethod
174+
def sm(cls, y=1) -> None: # noqa: PLW0211
175+
pass # pragma: no cover
176+
177+
assert varnames(MyClass.sm, is_staticmethod=True) == (("cls",), ("y",))
178+
179+
171180
def test_varnames_hookspec_without_self() -> None:
172181
"""Hookspec-style class methods without self/cls preserve all parameters.
173182

‎testing/test_pluginmanager.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,43 @@ def he_method1(arg: object) -> list[object]:
214214
assert he_pm.hook.he_method1(arg=1) == [[1]]
215215

216216

217+
def test_staticmethod_first_param_named_cls(pm: PluginManager) -> None:
218+
"""A staticmethod's first parameter is a hook argument even if it is
219+
named ``cls``, in both the hookspec and the hookimpl."""
220+
221+
class Spec:
222+
@staticmethod
223+
@hookspec
224+
def he_method1(cls: object, arg: object) -> None: ... # pragma: no cover # noqa: PLW0211
225+
226+
class Plugin:
227+
@staticmethod
228+
@hookimpl
229+
def he_method1(cls: object, arg: object) -> tuple[object, object]: # noqa: PLW0211
230+
return (cls, arg)
231+
232+
pm.add_hookspecs(Spec)
233+
assert pm.hook.he_method1.spec is not None
234+
assert pm.hook.he_method1.spec.argnames == ("cls", "arg")
235+
pm.register(Plugin())
236+
assert pm.hook.he_method1(cls=1, arg=2) == [(1, 2)]
237+
238+
239+
def test_staticmethod_first_param_named_cls_instance_namespace(
240+
pm: PluginManager,
241+
) -> None:
242+
"""Same as above, with the hookspecs given as an instance."""
243+
244+
class Spec:
245+
@staticmethod
246+
@hookspec
247+
def he_method1(cls: object, arg: object) -> None: ... # pragma: no cover # noqa: PLW0211
248+
249+
pm.add_hookspecs(Spec()) # type: ignore[arg-type]
250+
assert pm.hook.he_method1.spec is not None
251+
assert pm.hook.he_method1.spec.argnames == ("cls", "arg")
252+
253+
217254
def test_register_instance_attribute_function_is_not_bound(
218255
he_pm: PluginManager,
219256
) -> None:

0 commit comments

Comments
 (0)