Skip to content

Commit

Permalink
refactor(event_handler): add type annotations for router decorators (#…
Browse files Browse the repository at this point in the history
…5601)

* Fixed type annotation for Api Gateway Router

* replaced T_route with AnyCallableT

---------

Co-authored-by: Leandro Damascena <[email protected]>
  • Loading branch information
rafrafek and leandrodamascena authored Nov 24, 2024
1 parent 136c76f commit 3726a4b
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
TypeModelOrEnum,
)
from aws_lambda_powertools.shared.cookies import Cookie
from aws_lambda_powertools.shared.types import AnyCallableT
from aws_lambda_powertools.utilities.typing import LambdaContext


Expand Down Expand Up @@ -924,7 +925,7 @@ def route(
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
middlewares: list[Callable[..., Any]] | None = None,
):
) -> Callable[[AnyCallableT], AnyCallableT]:
raise NotImplementedError()

def use(self, middlewares: list[Callable[..., Response]]) -> None:
Expand Down Expand Up @@ -984,7 +985,7 @@ def get(
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
middlewares: list[Callable[..., Any]] | None = None,
):
) -> Callable[[AnyCallableT], AnyCallableT]:
"""Get route decorator with GET `method`
Examples
Expand Down Expand Up @@ -1041,7 +1042,7 @@ def post(
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
middlewares: list[Callable[..., Any]] | None = None,
):
) -> Callable[[AnyCallableT], AnyCallableT]:
"""Post route decorator with POST `method`
Examples
Expand Down Expand Up @@ -1099,7 +1100,7 @@ def put(
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
middlewares: list[Callable[..., Any]] | None = None,
):
) -> Callable[[AnyCallableT], AnyCallableT]:
"""Put route decorator with PUT `method`
Examples
Expand Down Expand Up @@ -1157,7 +1158,7 @@ def delete(
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
middlewares: list[Callable[..., Any]] | None = None,
):
) -> Callable[[AnyCallableT], AnyCallableT]:
"""Delete route decorator with DELETE `method`
Examples
Expand Down Expand Up @@ -1214,7 +1215,7 @@ def patch(
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
middlewares: list[Callable] | None = None,
):
) -> Callable[[AnyCallableT], AnyCallableT]:
"""Patch route decorator with PATCH `method`
Examples
Expand Down Expand Up @@ -1274,7 +1275,7 @@ def head(
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
middlewares: list[Callable] | None = None,
):
) -> Callable[[AnyCallableT], AnyCallableT]:
"""Head route decorator with HEAD `method`
Examples
Expand Down Expand Up @@ -1950,10 +1951,10 @@ def route(
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
middlewares: list[Callable[..., Any]] | None = None,
):
) -> Callable[[AnyCallableT], AnyCallableT]:
"""Route decorator includes parameter `method`"""

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

Expand Down Expand Up @@ -2492,8 +2493,8 @@ def route(
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
middlewares: list[Callable[..., Any]] | None = None,
):
def register_route(func: Callable):
) -> Callable[[AnyCallableT], AnyCallableT]:
def register_route(func: AnyCallableT) -> AnyCallableT:
# All dict keys needs to be hashable. So we'll need to do some conversions:
methods = (method,) if isinstance(method, str) else tuple(method)
frozen_responses = _FrozenDict(responses) if responses else None
Expand Down Expand Up @@ -2598,7 +2599,7 @@ def route(
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
middlewares: list[Callable[..., Any]] | None = None,
):
) -> Callable[[AnyCallableT], AnyCallableT]:
# NOTE: see #1552 for more context.
return super().route(
rule.rstrip("/"),
Expand Down

0 comments on commit 3726a4b

Please sign in to comment.