Skip to content

Commit 2ccfc03

Browse files
authored
chore: Use ParamSpec from PEP 612 (#2891)
1 parent 7ed3b09 commit 2ccfc03

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

samtranslator/internal/deprecation_control.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,25 @@
1313
from functools import wraps
1414
from typing import Callable, Optional, TypeVar
1515

16+
from typing_extensions import ParamSpec
17+
18+
PT = ParamSpec("PT") # parameters
1619
RT = TypeVar("RT") # return type
1720

1821

1922
def _make_message(message: str, replacement: Optional[str]) -> str:
2023
return f"{message}, please use {replacement}" if replacement else message
2124

2225

23-
def deprecated(replacement: Optional[str]) -> Callable[[Callable[..., RT]], Callable[..., RT]]:
26+
def deprecated(replacement: Optional[str]) -> Callable[[Callable[PT, RT]], Callable[PT, RT]]:
2427
"""
2528
Mark a function/method as deprecated.
2629
2730
The warning is shown by default when triggered directly
2831
by code in __main__.
2932
"""
3033

31-
def decorator(func: Callable[..., RT]) -> Callable[..., RT]:
34+
def decorator(func: Callable[PT, RT]) -> Callable[PT, RT]:
3235
@wraps(func)
3336
def wrapper(*args, **kwargs) -> RT: # type: ignore
3437
warning_message = _make_message(

samtranslator/metrics/method_decorator.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
from datetime import datetime
77
from typing import Callable, Optional, TypeVar, Union, overload
88

9+
from typing_extensions import ParamSpec
10+
911
from samtranslator.metrics.metrics import DummyMetricsPublisher, Metrics
1012
from samtranslator.model import Resource
1113

1214
LOG = logging.getLogger(__name__)
1315

16+
_PT = ParamSpec("_PT") # parameters
1417
_RT = TypeVar("_RT") # return value
1518

1619

@@ -81,18 +84,18 @@ def _send_cw_metric(prefix, name, execution_time_ms, func, args): # type: ignor
8184
@overload
8285
def cw_timer(
8386
*, name: Optional[str] = None, prefix: Optional[str] = None
84-
) -> Callable[[Callable[..., _RT]], Callable[..., _RT]]:
87+
) -> Callable[[Callable[_PT, _RT]], Callable[_PT, _RT]]:
8588
...
8689

8790

8891
@overload
89-
def cw_timer(_func: Callable[..., _RT], name: Optional[str] = None, prefix: Optional[str] = None) -> Callable[..., _RT]:
92+
def cw_timer(_func: Callable[_PT, _RT], name: Optional[str] = None, prefix: Optional[str] = None) -> Callable[_PT, _RT]:
9093
...
9194

9295

9396
def cw_timer(
94-
_func: Optional[Callable[..., _RT]] = None, name: Optional[str] = None, prefix: Optional[str] = None
95-
) -> Union[Callable[..., _RT], Callable[[Callable[..., _RT]], Callable[..., _RT]]]:
97+
_func: Optional[Callable[_PT, _RT]] = None, name: Optional[str] = None, prefix: Optional[str] = None
98+
) -> Union[Callable[_PT, _RT], Callable[[Callable[_PT, _RT]], Callable[_PT, _RT]]]:
9699
"""
97100
A method decorator, that will calculate execution time of the decorated method, and store this information as a
98101
metric in CloudWatch by calling the metrics singleton instance.
@@ -105,7 +108,7 @@ def cw_timer(
105108
If prefix is defined, it will be added in the beginning of what is been generated above
106109
"""
107110

108-
def cw_timer_decorator(func: Callable[..., _RT]) -> Callable[..., _RT]:
111+
def cw_timer_decorator(func: Callable[_PT, _RT]) -> Callable[_PT, _RT]:
109112
@functools.wraps(func)
110113
def wrapper_cw_timer(*args, **kwargs) -> _RT: # type: ignore[no-untyped-def]
111114
start_time = datetime.now()

0 commit comments

Comments
 (0)