1+ from __future__ import annotations
2+
13import datetime
24import logging
35from copy import deepcopy
4- from typing import Any , Callable , Dict , Optional , Tuple
6+ from typing import TYPE_CHECKING , Any , Callable
57
6- from aws_lambda_powertools .utilities .idempotency .config import (
7- IdempotencyConfig ,
8- )
98from aws_lambda_powertools .utilities .idempotency .exceptions import (
109 IdempotencyAlreadyInProgressError ,
1110 IdempotencyInconsistentStateError ,
1514 IdempotencyPersistenceLayerError ,
1615 IdempotencyValidationError ,
1716)
18- from aws_lambda_powertools .utilities .idempotency .persistence .base import (
19- BasePersistenceLayer ,
20- )
2117from aws_lambda_powertools .utilities .idempotency .persistence .datarecord import (
2218 STATUS_CONSTANTS ,
2319 DataRecord ,
2420)
25- from aws_lambda_powertools .utilities .idempotency .serialization .base import (
26- BaseIdempotencySerializer ,
27- )
2821from aws_lambda_powertools .utilities .idempotency .serialization .no_op import (
2922 NoOpSerializer ,
3023)
3124
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+
3236MAX_RETRIES = 2
3337logger = logging .getLogger (__name__ )
3438
@@ -69,9 +73,9 @@ def __init__(
6973 function_payload : Any ,
7074 config : IdempotencyConfig ,
7175 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 ,
7579 ):
7680 """
7781 Initialize the IdempotencyHandler
@@ -84,12 +88,12 @@ def __init__(
8488 Idempotency Configuration
8589 persistence_store : BasePersistenceLayer
8690 Instance of persistence layer to store idempotency records
87- output_serializer: Optional[ BaseIdempotencySerializer]
91+ output_serializer: BaseIdempotencySerializer | None
8892 Serializer to transform the data to and from a dictionary.
8993 If not supplied, no serialization is done via the NoOpSerializer
90- function_args: Optional[Tuple]
94+ function_args: tuple | None
9195 Function arguments
92- function_kwargs: Optional[Dict]
96+ function_kwargs: dict | None
9397 Function keyword arguments
9498 """
9599 self .function = function
@@ -150,7 +154,7 @@ def _process_idempotency(self):
150154
151155 return self ._get_function_response ()
152156
153- def _get_remaining_time_in_millis (self ) -> Optional [ int ] :
157+ def _get_remaining_time_in_millis (self ) -> int | None :
154158 """
155159 Tries to determine the remaining time available for the current lambda invocation.
156160
@@ -160,7 +164,7 @@ def _get_remaining_time_in_millis(self) -> Optional[int]:
160164
161165 Returns
162166 -------
163- Optional[ int]
167+ int | None
164168 Remaining time in millis, or None if the remaining time cannot be determined.
165169 """
166170
@@ -169,7 +173,7 @@ def _get_remaining_time_in_millis(self) -> Optional[int]:
169173
170174 return None
171175
172- def _get_idempotency_record (self ) -> Optional [ DataRecord ] :
176+ def _get_idempotency_record (self ) -> DataRecord | None :
173177 """
174178 Retrieve the idempotency record from the persistence layer.
175179
@@ -198,7 +202,7 @@ def _get_idempotency_record(self) -> Optional[DataRecord]:
198202
199203 return data_record
200204
201- def _handle_for_status (self , data_record : DataRecord ) -> Optional [ Any ] :
205+ def _handle_for_status (self , data_record : DataRecord ) -> Any | None :
202206 """
203207 Take appropriate action based on data_record's status
204208
@@ -208,7 +212,7 @@ def _handle_for_status(self, data_record: DataRecord) -> Optional[Any]:
208212
209213 Returns
210214 -------
211- Optional[ Any]
215+ Any | None
212216 Function's response previously used for this idempotency key, if it has successfully executed already.
213217 In case an output serializer is configured, the response is deserialized.
214218
@@ -235,7 +239,7 @@ def _handle_for_status(self, data_record: DataRecord) -> Optional[Any]:
235239 f"Execution already in progress with idempotency key: "
236240 f"{ self .persistence_store .event_key_jmespath } ={ data_record .idempotency_key } " ,
237241 )
238- response_dict : Optional [ dict ] = data_record .response_json_as_dict ()
242+ response_dict : dict | None = data_record .response_json_as_dict ()
239243 if response_dict is not None :
240244 serialized_response = self .output_serializer .from_dict (response_dict )
241245 if self .config .response_hook is not None :
0 commit comments