Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(event_handler): add type annotations for router decorators #5601

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -898,6 +898,7 @@ def build(self, event: ResponseEventT, cors: CORSConfig | None = None) -> dict[s
**event.header_serializer().serialize(headers=self.response.headers, cookies=self.response.cookies),
}

T_route = TypeVar("T_route", bound=Callable[..., Any])

class BaseRouter(ABC):
current_event: BaseProxyEvent
Expand All @@ -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[[T_route], T_route]:
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[[T_route], T_route]:
"""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[[T_route], T_route]:
"""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[[T_route], T_route]:
"""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[[T_route], T_route]:
"""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[[T_route], T_route]:
"""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[[T_route], T_route]:
"""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[[T_route], T_route]:
"""Route decorator includes parameter `method`"""

def register_resolver(func: Callable):
def register_resolver(func: T_route) -> T_route:
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[[T_route], T_route]:
def register_route(func: T_route) -> T_route:
# 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[[T_route], T_route]:
# NOTE: see #1552 for more context.
return super().route(
rule.rstrip("/"),
Expand Down
Loading