Skip to content

Commit 51e8df2

Browse files
committed
proposed fix for issue #358
1 parent 70fde45 commit 51e8df2

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/pluggy/_hooks.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,29 @@ def varnames(func: object) -> Tuple[Tuple[str, ...], Tuple[str, ...]]:
245245
return (), ()
246246

247247
try: # func MUST be a function or method here or we won't parse any args
248-
spec = inspect.getfullargspec(func)
248+
sig = inspect.signature(func.__func__ if inspect.ismethod(func) else func)
249249
except TypeError:
250250
return (), ()
251251

252-
args, defaults = tuple(spec.args), spec.defaults
252+
_valid_param_kinds = (
253+
inspect.Parameter.POSITIONAL_ONLY,
254+
inspect.Parameter.POSITIONAL_OR_KEYWORD,
255+
)
256+
_valid_params = {
257+
name: param
258+
for name, param in sig.parameters.items()
259+
if param.kind in _valid_param_kinds
260+
}
261+
args = tuple(_valid_params)
262+
defaults = (
263+
tuple(
264+
param.default
265+
for param in _valid_params.values()
266+
if param.default is not param.empty
267+
)
268+
or None
269+
)
270+
253271
if defaults:
254272
index = -len(defaults)
255273
args, kwargs = args[:index], tuple(args[index:])

testing/test_helpers.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from functools import wraps
2+
from typing import Callable
13
from pluggy._hooks import varnames
24
from pluggy._manager import _formatdef
35

@@ -82,3 +84,17 @@ def function4(arg1, *args, **kwargs):
8284
pass
8385

8486
assert _formatdef(function4) == "function4(arg1, *args, **kwargs)"
87+
88+
89+
def test_varnames_decorator() -> None:
90+
def my_decorator(func: Callable) -> Callable:
91+
@wraps(func)
92+
def wrapper(*args, **kwargs):
93+
return func(*args, **kwargs)
94+
return wrapper
95+
96+
@my_decorator
97+
def example(a, b=123) -> None:
98+
pass
99+
100+
assert varnames(example) == (("a",), ("b",))

0 commit comments

Comments
 (0)