Skip to content

Commit 189718e

Browse files
Merge branch 'v3' into parser_annotations
2 parents a1baf63 + 689072f commit 189718e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1395
-1312
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+287-313
Large diffs are not rendered by default.

aws_lambda_powertools/event_handler/appsync.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
from __future__ import annotations
2+
13
import logging
2-
from typing import Any, Callable, Optional, Type, TypeVar
4+
from typing import TYPE_CHECKING, Any, Callable, TypeVar
35

46
from aws_lambda_powertools.utilities.data_classes import AppSyncResolverEvent
5-
from aws_lambda_powertools.utilities.typing import LambdaContext
7+
8+
if TYPE_CHECKING:
9+
from aws_lambda_powertools.utilities.typing import LambdaContext
610

711
logger = logging.getLogger(__name__)
812

@@ -17,7 +21,7 @@ class BaseRouter:
1721
def __init__(self):
1822
self._resolvers: dict = {}
1923

20-
def resolver(self, type_name: str = "*", field_name: Optional[str] = None):
24+
def resolver(self, type_name: str = "*", field_name: str | None = None):
2125
"""Registers the resolver for field_name
2226
2327
Parameters
@@ -83,7 +87,7 @@ def resolve(
8387
self,
8488
event: dict,
8589
context: LambdaContext,
86-
data_model: Type[AppSyncResolverEvent] = AppSyncResolverEvent,
90+
data_model: type[AppSyncResolverEvent] = AppSyncResolverEvent,
8791
) -> Any:
8892
"""Resolve field_name
8993
@@ -189,12 +193,12 @@ def __call__(
189193
self,
190194
event: dict,
191195
context: LambdaContext,
192-
data_model: Type[AppSyncResolverEvent] = AppSyncResolverEvent,
196+
data_model: type[AppSyncResolverEvent] = AppSyncResolverEvent,
193197
) -> Any:
194198
"""Implicit lambda handler which internally calls `resolve`"""
195199
return self.resolve(event, context, data_model)
196200

197-
def include_router(self, router: "Router") -> None:
201+
def include_router(self, router: Router) -> None:
198202
"""Adds all resolvers defined in a router
199203
200204
Parameters

aws_lambda_powertools/event_handler/bedrock_agent.py

+47-42
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from re import Match
2-
from typing import Any, Callable, Dict, List, Optional
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, Any, Callable
34

45
from typing_extensions import override
56

@@ -9,8 +10,12 @@
910
ProxyEventType,
1011
ResponseBuilder,
1112
)
12-
from aws_lambda_powertools.event_handler.openapi.types import OpenAPIResponse
13-
from aws_lambda_powertools.utilities.data_classes import BedrockAgentEvent
13+
14+
if TYPE_CHECKING:
15+
from re import Match
16+
17+
from aws_lambda_powertools.event_handler.openapi.types import OpenAPIResponse
18+
from aws_lambda_powertools.utilities.data_classes import BedrockAgentEvent
1419

1520

1621
class BedrockResponseBuilder(ResponseBuilder):
@@ -21,7 +26,7 @@ class BedrockResponseBuilder(ResponseBuilder):
2126
"""
2227

2328
@override
24-
def build(self, event: BedrockAgentEvent, *args) -> Dict[str, Any]:
29+
def build(self, event: BedrockAgentEvent, *args) -> dict[str, Any]:
2530
"""Build the full response dict to be returned by the lambda"""
2631
self._route(event, None)
2732

@@ -91,16 +96,16 @@ def get( # type: ignore[override]
9196
self,
9297
rule: str,
9398
description: str,
94-
cors: Optional[bool] = None,
99+
cors: bool | None = None,
95100
compress: bool = False,
96-
cache_control: Optional[str] = None,
97-
summary: Optional[str] = None,
98-
responses: Optional[Dict[int, OpenAPIResponse]] = None,
101+
cache_control: str | None = None,
102+
summary: str | None = None,
103+
responses: dict[int, OpenAPIResponse] | None = None,
99104
response_description: str = _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION,
100-
tags: Optional[List[str]] = None,
101-
operation_id: Optional[str] = None,
105+
tags: list[str] | None = None,
106+
operation_id: str | None = None,
102107
include_in_schema: bool = True,
103-
middlewares: Optional[List[Callable[..., Any]]] = None,
108+
middlewares: list[Callable[..., Any]] | None = None,
104109
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
105110
security = None
106111

@@ -126,16 +131,16 @@ def post( # type: ignore[override]
126131
self,
127132
rule: str,
128133
description: str,
129-
cors: Optional[bool] = None,
134+
cors: bool | None = None,
130135
compress: bool = False,
131-
cache_control: Optional[str] = None,
132-
summary: Optional[str] = None,
133-
responses: Optional[Dict[int, OpenAPIResponse]] = None,
136+
cache_control: str | None = None,
137+
summary: str | None = None,
138+
responses: dict[int, OpenAPIResponse] | None = None,
134139
response_description: str = _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION,
135-
tags: Optional[List[str]] = None,
136-
operation_id: Optional[str] = None,
140+
tags: list[str] | None = None,
141+
operation_id: str | None = None,
137142
include_in_schema: bool = True,
138-
middlewares: Optional[List[Callable[..., Any]]] = None,
143+
middlewares: list[Callable[..., Any]] | None = None,
139144
):
140145
security = None
141146

@@ -161,16 +166,16 @@ def put( # type: ignore[override]
161166
self,
162167
rule: str,
163168
description: str,
164-
cors: Optional[bool] = None,
169+
cors: bool | None = None,
165170
compress: bool = False,
166-
cache_control: Optional[str] = None,
167-
summary: Optional[str] = None,
168-
responses: Optional[Dict[int, OpenAPIResponse]] = None,
171+
cache_control: str | None = None,
172+
summary: str | None = None,
173+
responses: dict[int, OpenAPIResponse] | None = None,
169174
response_description: str = _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION,
170-
tags: Optional[List[str]] = None,
171-
operation_id: Optional[str] = None,
175+
tags: list[str] | None = None,
176+
operation_id: str | None = None,
172177
include_in_schema: bool = True,
173-
middlewares: Optional[List[Callable[..., Any]]] = None,
178+
middlewares: list[Callable[..., Any]] | None = None,
174179
):
175180
security = None
176181

@@ -196,16 +201,16 @@ def patch( # type: ignore[override]
196201
self,
197202
rule: str,
198203
description: str,
199-
cors: Optional[bool] = None,
204+
cors: bool | None = None,
200205
compress: bool = False,
201-
cache_control: Optional[str] = None,
202-
summary: Optional[str] = None,
203-
responses: Optional[Dict[int, OpenAPIResponse]] = None,
206+
cache_control: str | None = None,
207+
summary: str | None = None,
208+
responses: dict[int, OpenAPIResponse] | None = None,
204209
response_description: str = _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION,
205-
tags: Optional[List[str]] = None,
206-
operation_id: Optional[str] = None,
210+
tags: list[str] | None = None,
211+
operation_id: str | None = None,
207212
include_in_schema: bool = True,
208-
middlewares: Optional[List[Callable]] = None,
213+
middlewares: list[Callable] | None = None,
209214
):
210215
security = None
211216

@@ -231,16 +236,16 @@ def delete( # type: ignore[override]
231236
self,
232237
rule: str,
233238
description: str,
234-
cors: Optional[bool] = None,
239+
cors: bool | None = None,
235240
compress: bool = False,
236-
cache_control: Optional[str] = None,
237-
summary: Optional[str] = None,
238-
responses: Optional[Dict[int, OpenAPIResponse]] = None,
241+
cache_control: str | None = None,
242+
summary: str | None = None,
243+
responses: dict[int, OpenAPIResponse] | None = None,
239244
response_description: str = _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION,
240-
tags: Optional[List[str]] = None,
241-
operation_id: Optional[str] = None,
245+
tags: list[str] | None = None,
246+
operation_id: str | None = None,
242247
include_in_schema: bool = True,
243-
middlewares: Optional[List[Callable[..., Any]]] = None,
248+
middlewares: list[Callable[..., Any]] | None = None,
244249
):
245250
security = None
246251

@@ -261,10 +266,10 @@ def delete( # type: ignore[override]
261266
)
262267

263268
@override
264-
def _convert_matches_into_route_keys(self, match: Match) -> Dict[str, str]:
269+
def _convert_matches_into_route_keys(self, match: Match) -> dict[str, str]:
265270
# In Bedrock Agents, all the parameters come inside the "parameters" key, not on the apiPath
266271
# So we have to search for route parameters in the parameters key
267-
parameters: Dict[str, str] = {}
272+
parameters: dict[str, str] = {}
268273
if match.groupdict() and self.current_event.parameters:
269274
parameters = {parameter["name"]: parameter["value"] for parameter in self.current_event.parameters}
270275
return parameters

aws_lambda_powertools/event_handler/lambda_function_url.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
from typing import Callable, Dict, List, Optional, Pattern, Union
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, Callable, Pattern
24

3-
from aws_lambda_powertools.event_handler import CORSConfig
45
from aws_lambda_powertools.event_handler.api_gateway import (
56
ApiGatewayResolver,
67
ProxyEventType,
78
)
8-
from aws_lambda_powertools.utilities.data_classes import LambdaFunctionUrlEvent
9+
10+
if TYPE_CHECKING:
11+
from aws_lambda_powertools.event_handler import CORSConfig
12+
from aws_lambda_powertools.utilities.data_classes import LambdaFunctionUrlEvent
913

1014

1115
class LambdaFunctionUrlResolver(ApiGatewayResolver):
@@ -48,10 +52,10 @@ def lambda_handler(event, context):
4852

4953
def __init__(
5054
self,
51-
cors: Optional[CORSConfig] = None,
52-
debug: Optional[bool] = None,
53-
serializer: Optional[Callable[[Dict], str]] = None,
54-
strip_prefixes: Optional[List[Union[str, Pattern]]] = None,
55+
cors: CORSConfig | None = None,
56+
debug: bool | None = None,
57+
serializer: Callable[[dict], str] | None = None,
58+
strip_prefixes: list[str | Pattern] | None = None,
5559
enable_validation: bool = False,
5660
):
5761
super().__init__(

aws_lambda_powertools/event_handler/middlewares/base.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
from __future__ import annotations
2+
13
from abc import ABC, abstractmethod
2-
from typing import Generic, Protocol
4+
from typing import TYPE_CHECKING, Generic, Protocol
35

4-
from aws_lambda_powertools.event_handler.api_gateway import Response
56
from aws_lambda_powertools.event_handler.types import EventHandlerInstance
67

8+
if TYPE_CHECKING:
9+
from aws_lambda_powertools.event_handler.api_gateway import Response
10+
711

812
class NextMiddleware(Protocol):
913
def __call__(self, app: EventHandlerInstance) -> Response:

0 commit comments

Comments
 (0)