1
1
import functools
2
+ import sys
2
3
from timeit import default_timer
3
4
from types import TracebackType
4
5
from typing import (
5
- Any , Callable , Literal , Optional , Tuple , Type , TYPE_CHECKING , TypeVar ,
6
+ Callable , Literal , Optional , Tuple , Type , TYPE_CHECKING , TypeVar ,
6
7
Union ,
7
8
)
9
+ if sys .version_info >= (3 , 10 ):
10
+ from typing import ParamSpec
11
+ else :
12
+ from typing_extensions import ParamSpec
8
13
9
14
if TYPE_CHECKING :
10
15
from . import Counter
11
- F = TypeVar ("F" , bound = Callable [..., Any ])
16
+
17
+ TParam = ParamSpec ("TParam" )
18
+ TResult = TypeVar ("TResult" )
12
19
13
20
14
21
class ExceptionCounter :
@@ -24,9 +31,9 @@ def __exit__(self, typ: Optional[Type[BaseException]], value: Optional[BaseExcep
24
31
self ._counter .inc ()
25
32
return False
26
33
27
- def __call__ (self , f : "F" ) -> "F" :
34
+ def __call__ (self , f : Callable [ TParam , TResult ] ) -> Callable [ TParam , TResult ] :
28
35
@functools .wraps (f )
29
- def wrapped (* args : Any , ** kwargs : Any ) -> Any :
36
+ def wrapped (* args : TParam . args , ** kwargs : TParam . kwargs ) -> TResult :
30
37
with self :
31
38
return f (* args , ** kwargs )
32
39
return wrapped # type: ignore
@@ -42,9 +49,9 @@ def __enter__(self):
42
49
def __exit__ (self , typ , value , traceback ):
43
50
self ._gauge .dec ()
44
51
45
- def __call__ (self , f : "F" ) -> "F" :
52
+ def __call__ (self , f : Callable [ TParam , TResult ] ) -> Callable [ TParam , TResult ] :
46
53
@functools .wraps (f )
47
- def wrapped (* args : Any , ** kwargs : Any ) -> Any :
54
+ def wrapped (* args : TParam . args , ** kwargs : TParam . kwargs ) -> TResult :
48
55
with self :
49
56
return f (* args , ** kwargs )
50
57
return wrapped # type: ignore
@@ -71,9 +78,9 @@ def __exit__(self, typ, value, traceback):
71
78
def labels (self , * args , ** kw ):
72
79
self ._metric = self ._metric .labels (* args , ** kw )
73
80
74
- def __call__ (self , f : "F" ) -> "F" :
81
+ def __call__ (self , f : Callable [ TParam , TResult ] ) -> Callable [ TParam , TResult ] :
75
82
@functools .wraps (f )
76
- def wrapped (* args : Any , ** kwargs : Any ) -> Any :
83
+ def wrapped (* args : TParam . args , ** kwargs : TParam . kwargs ) -> TResult :
77
84
# Obtaining new instance of timer every time
78
85
# ensures thread safety and reentrancy.
79
86
with self ._new_timer ():
0 commit comments