|
11 | 11 | from typing import Protocol
|
12 | 12 | from typing import TypeVar
|
13 | 13 |
|
| 14 | +from typing_extensions import ParamSpec |
| 15 | + |
14 | 16 | from .warning_types import PytestDeprecationWarning
|
15 | 17 |
|
16 | 18 |
|
@@ -80,18 +82,22 @@ def __init__(
|
80 | 82 | # We need a callable protocol to add attributes, for discussion see
|
81 | 83 | # https://github.com/python/mypy/issues/2087.
|
82 | 84 |
|
83 |
| -_F = TypeVar("_F", bound=Callable[..., object]) |
| 85 | +_P = ParamSpec("_P") |
| 86 | +_R = TypeVar("_R", covariant=True) |
84 | 87 | _ET = TypeVar("_ET", bound=type[BaseException])
|
85 | 88 |
|
86 | 89 |
|
87 |
| -class _WithException(Protocol[_F, _ET]): |
| 90 | +class _WithException(Protocol[_P, _R, _ET]): |
88 | 91 | Exception: _ET
|
89 |
| - __call__: _F |
| 92 | + |
| 93 | + def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R: ... |
90 | 94 |
|
91 | 95 |
|
92 |
| -def _with_exception(exception_type: _ET) -> Callable[[_F], _WithException[_F, _ET]]: |
93 |
| - def decorate(func: _F) -> _WithException[_F, _ET]: |
94 |
| - func_with_exception = cast(_WithException[_F, _ET], func) |
| 96 | +def _with_exception( |
| 97 | + exception_type: _ET, |
| 98 | +) -> Callable[[Callable[_P, _R]], _WithException[_P, _R, _ET]]: |
| 99 | + def decorate(func: Callable[_P, _R]) -> _WithException[_P, _R, _ET]: |
| 100 | + func_with_exception = cast(_WithException[_P, _R, _ET], func) |
95 | 101 | func_with_exception.Exception = exception_type
|
96 | 102 | return func_with_exception
|
97 | 103 |
|
|
0 commit comments