1
+ from __future__ import annotations
2
+
1
3
import datetime
2
4
import logging
3
5
from copy import deepcopy
4
- from typing import Any , Callable , Dict , Optional , Tuple
6
+ from typing import TYPE_CHECKING , Any , Callable
5
7
6
- from aws_lambda_powertools .utilities .idempotency .config import (
7
- IdempotencyConfig ,
8
- )
9
8
from aws_lambda_powertools .utilities .idempotency .exceptions import (
10
9
IdempotencyAlreadyInProgressError ,
11
10
IdempotencyInconsistentStateError ,
15
14
IdempotencyPersistenceLayerError ,
16
15
IdempotencyValidationError ,
17
16
)
18
- from aws_lambda_powertools .utilities .idempotency .persistence .base import (
19
- BasePersistenceLayer ,
20
- )
21
17
from aws_lambda_powertools .utilities .idempotency .persistence .datarecord import (
22
18
STATUS_CONSTANTS ,
23
19
DataRecord ,
24
20
)
25
- from aws_lambda_powertools .utilities .idempotency .serialization .base import (
26
- BaseIdempotencySerializer ,
27
- )
28
21
from aws_lambda_powertools .utilities .idempotency .serialization .no_op import (
29
22
NoOpSerializer ,
30
23
)
31
24
25
+ if TYPE_CHECKING :
26
+ from aws_lambda_powertools .utilities .idempotency .config import (
27
+ IdempotencyConfig ,
28
+ )
29
+ from aws_lambda_powertools .utilities .idempotency .persistence .base import (
30
+ BasePersistenceLayer ,
31
+ )
32
+ from aws_lambda_powertools .utilities .idempotency .serialization .base import (
33
+ BaseIdempotencySerializer ,
34
+ )
35
+
32
36
MAX_RETRIES = 2
33
37
logger = logging .getLogger (__name__ )
34
38
@@ -69,9 +73,9 @@ def __init__(
69
73
function_payload : Any ,
70
74
config : IdempotencyConfig ,
71
75
persistence_store : BasePersistenceLayer ,
72
- output_serializer : Optional [ BaseIdempotencySerializer ] = None ,
73
- function_args : Optional [ Tuple ] = None ,
74
- function_kwargs : Optional [ Dict ] = None ,
76
+ output_serializer : BaseIdempotencySerializer | None = None ,
77
+ function_args : tuple | None = None ,
78
+ function_kwargs : dict | None = None ,
75
79
):
76
80
"""
77
81
Initialize the IdempotencyHandler
@@ -84,12 +88,12 @@ def __init__(
84
88
Idempotency Configuration
85
89
persistence_store : BasePersistenceLayer
86
90
Instance of persistence layer to store idempotency records
87
- output_serializer: Optional[ BaseIdempotencySerializer]
91
+ output_serializer: BaseIdempotencySerializer | None
88
92
Serializer to transform the data to and from a dictionary.
89
93
If not supplied, no serialization is done via the NoOpSerializer
90
- function_args: Optional[Tuple]
94
+ function_args: tuple | None
91
95
Function arguments
92
- function_kwargs: Optional[Dict]
96
+ function_kwargs: dict | None
93
97
Function keyword arguments
94
98
"""
95
99
self .function = function
@@ -150,7 +154,7 @@ def _process_idempotency(self):
150
154
151
155
return self ._get_function_response ()
152
156
153
- def _get_remaining_time_in_millis (self ) -> Optional [ int ] :
157
+ def _get_remaining_time_in_millis (self ) -> int | None :
154
158
"""
155
159
Tries to determine the remaining time available for the current lambda invocation.
156
160
@@ -160,7 +164,7 @@ def _get_remaining_time_in_millis(self) -> Optional[int]:
160
164
161
165
Returns
162
166
-------
163
- Optional[ int]
167
+ int | None
164
168
Remaining time in millis, or None if the remaining time cannot be determined.
165
169
"""
166
170
@@ -169,7 +173,7 @@ def _get_remaining_time_in_millis(self) -> Optional[int]:
169
173
170
174
return None
171
175
172
- def _get_idempotency_record (self ) -> Optional [ DataRecord ] :
176
+ def _get_idempotency_record (self ) -> DataRecord | None :
173
177
"""
174
178
Retrieve the idempotency record from the persistence layer.
175
179
@@ -198,7 +202,7 @@ def _get_idempotency_record(self) -> Optional[DataRecord]:
198
202
199
203
return data_record
200
204
201
- def _handle_for_status (self , data_record : DataRecord ) -> Optional [ Any ] :
205
+ def _handle_for_status (self , data_record : DataRecord ) -> Any | None :
202
206
"""
203
207
Take appropriate action based on data_record's status
204
208
@@ -208,7 +212,7 @@ def _handle_for_status(self, data_record: DataRecord) -> Optional[Any]:
208
212
209
213
Returns
210
214
-------
211
- Optional[ Any]
215
+ Any | None
212
216
Function's response previously used for this idempotency key, if it has successfully executed already.
213
217
In case an output serializer is configured, the response is deserialized.
214
218
@@ -235,7 +239,7 @@ def _handle_for_status(self, data_record: DataRecord) -> Optional[Any]:
235
239
f"Execution already in progress with idempotency key: "
236
240
f"{ self .persistence_store .event_key_jmespath } ={ data_record .idempotency_key } " ,
237
241
)
238
- response_dict : Optional [ dict ] = data_record .response_json_as_dict ()
242
+ response_dict : dict | None = data_record .response_json_as_dict ()
239
243
if response_dict is not None :
240
244
serialized_response = self .output_serializer .from_dict (response_dict )
241
245
if self .config .response_hook is not None :
0 commit comments