Skip to content

Commit 3726a4b

Browse files
refactor(event_handler): add type annotations for router decorators (#5601)
* Fixed type annotation for Api Gateway Router * replaced T_route with AnyCallableT --------- Co-authored-by: Leandro Damascena <[email protected]>
1 parent 136c76f commit 3726a4b

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
TypeModelOrEnum,
8484
)
8585
from aws_lambda_powertools.shared.cookies import Cookie
86+
from aws_lambda_powertools.shared.types import AnyCallableT
8687
from aws_lambda_powertools.utilities.typing import LambdaContext
8788

8889

@@ -924,7 +925,7 @@ def route(
924925
security: list[dict[str, list[str]]] | None = None,
925926
openapi_extensions: dict[str, Any] | None = None,
926927
middlewares: list[Callable[..., Any]] | None = None,
927-
):
928+
) -> Callable[[AnyCallableT], AnyCallableT]:
928929
raise NotImplementedError()
929930

930931
def use(self, middlewares: list[Callable[..., Response]]) -> None:
@@ -984,7 +985,7 @@ def get(
984985
security: list[dict[str, list[str]]] | None = None,
985986
openapi_extensions: dict[str, Any] | None = None,
986987
middlewares: list[Callable[..., Any]] | None = None,
987-
):
988+
) -> Callable[[AnyCallableT], AnyCallableT]:
988989
"""Get route decorator with GET `method`
989990
990991
Examples
@@ -1041,7 +1042,7 @@ def post(
10411042
security: list[dict[str, list[str]]] | None = None,
10421043
openapi_extensions: dict[str, Any] | None = None,
10431044
middlewares: list[Callable[..., Any]] | None = None,
1044-
):
1045+
) -> Callable[[AnyCallableT], AnyCallableT]:
10451046
"""Post route decorator with POST `method`
10461047
10471048
Examples
@@ -1099,7 +1100,7 @@ def put(
10991100
security: list[dict[str, list[str]]] | None = None,
11001101
openapi_extensions: dict[str, Any] | None = None,
11011102
middlewares: list[Callable[..., Any]] | None = None,
1102-
):
1103+
) -> Callable[[AnyCallableT], AnyCallableT]:
11031104
"""Put route decorator with PUT `method`
11041105
11051106
Examples
@@ -1157,7 +1158,7 @@ def delete(
11571158
security: list[dict[str, list[str]]] | None = None,
11581159
openapi_extensions: dict[str, Any] | None = None,
11591160
middlewares: list[Callable[..., Any]] | None = None,
1160-
):
1161+
) -> Callable[[AnyCallableT], AnyCallableT]:
11611162
"""Delete route decorator with DELETE `method`
11621163
11631164
Examples
@@ -1214,7 +1215,7 @@ def patch(
12141215
security: list[dict[str, list[str]]] | None = None,
12151216
openapi_extensions: dict[str, Any] | None = None,
12161217
middlewares: list[Callable] | None = None,
1217-
):
1218+
) -> Callable[[AnyCallableT], AnyCallableT]:
12181219
"""Patch route decorator with PATCH `method`
12191220
12201221
Examples
@@ -1274,7 +1275,7 @@ def head(
12741275
security: list[dict[str, list[str]]] | None = None,
12751276
openapi_extensions: dict[str, Any] | None = None,
12761277
middlewares: list[Callable] | None = None,
1277-
):
1278+
) -> Callable[[AnyCallableT], AnyCallableT]:
12781279
"""Head route decorator with HEAD `method`
12791280
12801281
Examples
@@ -1950,10 +1951,10 @@ def route(
19501951
security: list[dict[str, list[str]]] | None = None,
19511952
openapi_extensions: dict[str, Any] | None = None,
19521953
middlewares: list[Callable[..., Any]] | None = None,
1953-
):
1954+
) -> Callable[[AnyCallableT], AnyCallableT]:
19541955
"""Route decorator includes parameter `method`"""
19551956

1956-
def register_resolver(func: Callable):
1957+
def register_resolver(func: AnyCallableT) -> AnyCallableT:
19571958
methods = (method,) if isinstance(method, str) else method
19581959
logger.debug(f"Adding route using rule {rule} and methods: {','.join(m.upper() for m in methods)}")
19591960

@@ -2492,8 +2493,8 @@ def route(
24922493
security: list[dict[str, list[str]]] | None = None,
24932494
openapi_extensions: dict[str, Any] | None = None,
24942495
middlewares: list[Callable[..., Any]] | None = None,
2495-
):
2496-
def register_route(func: Callable):
2496+
) -> Callable[[AnyCallableT], AnyCallableT]:
2497+
def register_route(func: AnyCallableT) -> AnyCallableT:
24972498
# All dict keys needs to be hashable. So we'll need to do some conversions:
24982499
methods = (method,) if isinstance(method, str) else tuple(method)
24992500
frozen_responses = _FrozenDict(responses) if responses else None
@@ -2598,7 +2599,7 @@ def route(
25982599
security: list[dict[str, list[str]]] | None = None,
25992600
openapi_extensions: dict[str, Any] | None = None,
26002601
middlewares: list[Callable[..., Any]] | None = None,
2601-
):
2602+
) -> Callable[[AnyCallableT], AnyCallableT]:
26022603
# NOTE: see #1552 for more context.
26032604
return super().route(
26042605
rule.rstrip("/"),

0 commit comments

Comments
 (0)