Skip to content

Commit 025ce72

Browse files
author
Brian Villemarette
committed
fix(event_handler): Update typing of route and exception handling to clarify them as returning decorators
1 parent 4e93031 commit 025ce72

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@
8181

8282
ResponseEventT = TypeVar("ResponseEventT", bound=BaseProxyEvent)
8383
ResponseT = TypeVar("ResponseT")
84+
P = TypeVar("P")
85+
T = TypeVar("T")
86+
SimpleFunction = Callable[P, T]
87+
DecoratorFunction = Callable[[SimpleFunction], SimpleFunction]
8488

8589
if TYPE_CHECKING:
8690
from aws_lambda_powertools.event_handler.openapi.compat import (
@@ -951,7 +955,7 @@ def route(
951955
security: Optional[List[Dict[str, List[str]]]] = None,
952956
openapi_extensions: Optional[Dict[str, Any]] = None,
953957
middlewares: Optional[List[Callable[..., Any]]] = None,
954-
):
958+
) -> DecoratorFunction:
955959
raise NotImplementedError()
956960

957961
def use(self, middlewares: List[Callable[..., Response]]) -> None:
@@ -1011,7 +1015,7 @@ def get(
10111015
security: Optional[List[Dict[str, List[str]]]] = None,
10121016
openapi_extensions: Optional[Dict[str, Any]] = None,
10131017
middlewares: Optional[List[Callable[..., Any]]] = None,
1014-
):
1018+
) -> DecoratorFunction:
10151019
"""Get route decorator with GET `method`
10161020
10171021
Examples
@@ -1068,7 +1072,7 @@ def post(
10681072
security: Optional[List[Dict[str, List[str]]]] = None,
10691073
openapi_extensions: Optional[Dict[str, Any]] = None,
10701074
middlewares: Optional[List[Callable[..., Any]]] = None,
1071-
):
1075+
) -> DecoratorFunction:
10721076
"""Post route decorator with POST `method`
10731077
10741078
Examples
@@ -1126,7 +1130,7 @@ def put(
11261130
security: Optional[List[Dict[str, List[str]]]] = None,
11271131
openapi_extensions: Optional[Dict[str, Any]] = None,
11281132
middlewares: Optional[List[Callable[..., Any]]] = None,
1129-
):
1133+
) -> DecoratorFunction:
11301134
"""Put route decorator with PUT `method`
11311135
11321136
Examples
@@ -1184,7 +1188,7 @@ def delete(
11841188
security: Optional[List[Dict[str, List[str]]]] = None,
11851189
openapi_extensions: Optional[Dict[str, Any]] = None,
11861190
middlewares: Optional[List[Callable[..., Any]]] = None,
1187-
):
1191+
) -> DecoratorFunction:
11881192
"""Delete route decorator with DELETE `method`
11891193
11901194
Examples
@@ -1241,7 +1245,7 @@ def patch(
12411245
security: Optional[List[Dict[str, List[str]]]] = None,
12421246
openapi_extensions: Optional[Dict[str, Any]] = None,
12431247
middlewares: Optional[List[Callable]] = None,
1244-
):
1248+
) -> DecoratorFunction:
12451249
"""Patch route decorator with PATCH `method`
12461250
12471251
Examples
@@ -1301,7 +1305,7 @@ def head(
13011305
security: Optional[List[Dict[str, List[str]]]] = None,
13021306
openapi_extensions: Optional[Dict[str, Any]] = None,
13031307
middlewares: Optional[List[Callable]] = None,
1304-
):
1308+
) -> DecoratorFunction:
13051309
"""Head route decorator with HEAD `method`
13061310
13071311
Examples
@@ -1528,7 +1532,7 @@ def __init__(
15281532
self._dynamic_routes: List[Route] = []
15291533
self._static_routes: List[Route] = []
15301534
self._route_keys: List[str] = []
1531-
self._exception_handlers: Dict[Type, Callable] = {}
1535+
self._exception_handlers: Dict[Type, DecoratorFunction] = {}
15321536
self._cors = cors
15331537
self._cors_enabled: bool = cors is not None
15341538
self._cors_methods: Set[str] = {"OPTIONS"}
@@ -1988,7 +1992,7 @@ def route(
19881992
security: Optional[List[Dict[str, List[str]]]] = None,
19891993
openapi_extensions: Optional[Dict[str, Any]] = None,
19901994
middlewares: Optional[List[Callable[..., Any]]] = None,
1991-
):
1995+
) -> DecoratorFunction:
19921996
"""Route decorator includes parameter `method`"""
19931997

19941998
def register_resolver(func: Callable):
@@ -2345,7 +2349,7 @@ def not_found(self, func: Optional[Callable] = None):
23452349
return self.exception_handler(NotFoundError)
23462350
return self.exception_handler(NotFoundError)(func)
23472351

2348-
def exception_handler(self, exc_class: Union[Type[Exception], List[Type[Exception]]]):
2352+
def exception_handler(self, exc_class: Union[Type[Exception], List[Type[Exception]]]) -> DecoratorFunction:
23492353
def register_exception_handler(func: Callable):
23502354
if isinstance(exc_class, list): # pragma: no cover
23512355
for exp in exc_class:
@@ -2356,7 +2360,7 @@ def register_exception_handler(func: Callable):
23562360

23572361
return register_exception_handler
23582362

2359-
def _lookup_exception_handler(self, exp_type: Type) -> Optional[Callable]:
2363+
def _lookup_exception_handler(self, exp_type: Type) -> Optional[DecoratorFunction]:
23602364
# Use "Method Resolution Order" to allow for matching against a base class
23612365
# of an exception
23622366
for cls in exp_type.__mro__:
@@ -2506,12 +2510,12 @@ def _get_fields_from_routes(routes: Sequence[Route]) -> List["ModelField"]:
25062510
class Router(BaseRouter):
25072511
"""Router helper class to allow splitting ApiGatewayResolver into multiple files"""
25082512

2509-
def __init__(self):
2513+
def __init__(self) -> "Router":
25102514
self._routes: Dict[tuple, Callable] = {}
25112515
self._routes_with_middleware: Dict[tuple, List[Callable]] = {}
25122516
self.api_resolver: Optional[BaseRouter] = None
25132517
self.context = {} # early init as customers might add context before event resolution
2514-
self._exception_handlers: Dict[Type, Callable] = {}
2518+
self._exception_handlers: Dict[Type, DecoratorFunction] = {}
25152519

25162520
def route(
25172521
self,
@@ -2530,7 +2534,7 @@ def route(
25302534
security: Optional[List[Dict[str, List[str]]]] = None,
25312535
openapi_extensions: Optional[Dict[str, Any]] = None,
25322536
middlewares: Optional[List[Callable[..., Any]]] = None,
2533-
):
2537+
) -> DecoratorFunction:
25342538
def register_route(func: Callable):
25352539
# All dict keys needs to be hashable. So we'll need to do some conversions:
25362540
methods = (method,) if isinstance(method, str) else tuple(method)
@@ -2636,7 +2640,7 @@ def route(
26362640
security: Optional[List[Dict[str, List[str]]]] = None,
26372641
openapi_extensions: Optional[Dict[str, Any]] = None,
26382642
middlewares: Optional[List[Callable[..., Any]]] = None,
2639-
):
2643+
) -> DecoratorFunction:
26402644
# NOTE: see #1552 for more context.
26412645
return super().route(
26422646
rule.rstrip("/"),

0 commit comments

Comments
 (0)