|
| 1 | +from functools import wraps |
| 2 | +from typing import Any, Callable, TypeVar, cast |
1 | 3 | from pluggy._hooks import varnames |
2 | 4 | from pluggy._manager import _formatdef |
3 | 5 |
|
@@ -82,3 +84,29 @@ def function4(arg1, *args, **kwargs): |
82 | 84 | pass |
83 | 85 |
|
84 | 86 | assert _formatdef(function4) == "function4(arg1, *args, **kwargs)" |
| 87 | + |
| 88 | + |
| 89 | +def test_varnames_decorator() -> None: |
| 90 | + F = TypeVar("F", bound=Callable[..., Any]) |
| 91 | + |
| 92 | + def my_decorator(func: F) -> F: |
| 93 | + @wraps(func) |
| 94 | + def wrapper(*args, **kwargs): |
| 95 | + return func(*args, **kwargs) |
| 96 | + |
| 97 | + return cast(F, wrapper) |
| 98 | + |
| 99 | + @my_decorator |
| 100 | + def example(a, b=123) -> None: |
| 101 | + pass |
| 102 | + |
| 103 | + class Example: |
| 104 | + @my_decorator |
| 105 | + def example_method(self, x, y=1) -> None: |
| 106 | + pass |
| 107 | + |
| 108 | + ex_inst = Example() |
| 109 | + |
| 110 | + assert varnames(example) == (("a",), ("b",)) |
| 111 | + assert varnames(Example.example_method) == (("x",), ("y",)) |
| 112 | + assert varnames(ex_inst.example_method) == (("x",), ("y",)) |
0 commit comments