Skip to content

Commit e69b192

Browse files
refactor(parser): add from __future__ import annotations (#4977)
* refactor(envelopes): add from __future__ import annotations and update code according to ruff rules TCH, UP006, UP007, UP037 and FA100. * Fix e2e tests --------- Co-authored-by: Leandro Damascena <[email protected]>
1 parent 2d59b7a commit e69b192

16 files changed

+180
-121
lines changed

aws_lambda_powertools/utilities/parser/envelopes/apigw.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1+
from __future__ import annotations
2+
13
import logging
2-
from typing import Any, Dict, Optional, Type, Union
4+
from typing import TYPE_CHECKING, Any
5+
6+
from aws_lambda_powertools.utilities.parser.envelopes.base import BaseEnvelope
7+
from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventModel
38

4-
from ..models import APIGatewayProxyEventModel
5-
from ..types import Model
6-
from .base import BaseEnvelope
9+
if TYPE_CHECKING:
10+
from aws_lambda_powertools.utilities.parser.types import Model
711

812
logger = logging.getLogger(__name__)
913

1014

1115
class ApiGatewayEnvelope(BaseEnvelope):
1216
"""API Gateway envelope to extract data within body key"""
1317

14-
def parse(self, data: Optional[Union[Dict[str, Any], Any]], model: Type[Model]) -> Optional[Model]:
18+
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> Model | None:
1519
"""Parses data found with model provided
1620
1721
Parameters
1822
----------
19-
data : Dict
23+
data : dict
2024
Lambda event to be parsed
21-
model : Type[Model]
25+
model : type[Model]
2226
Data model provided to parse after extracting data using envelope
2327
2428
Returns

aws_lambda_powertools/utilities/parser/envelopes/apigwv2.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1+
from __future__ import annotations
2+
13
import logging
2-
from typing import Any, Dict, Optional, Type, Union
4+
from typing import TYPE_CHECKING, Any
5+
6+
from aws_lambda_powertools.utilities.parser.envelopes.base import BaseEnvelope
7+
from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventV2Model
38

4-
from ..models import APIGatewayProxyEventV2Model
5-
from ..types import Model
6-
from .base import BaseEnvelope
9+
if TYPE_CHECKING:
10+
from aws_lambda_powertools.utilities.parser.types import Model
711

812
logger = logging.getLogger(__name__)
913

1014

1115
class ApiGatewayV2Envelope(BaseEnvelope):
1216
"""API Gateway V2 envelope to extract data within body key"""
1317

14-
def parse(self, data: Optional[Union[Dict[str, Any], Any]], model: Type[Model]) -> Optional[Model]:
18+
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> Model | None:
1519
"""Parses data found with model provided
1620
1721
Parameters
1822
----------
19-
data : Dict
23+
data : dict
2024
Lambda event to be parsed
21-
model : Type[Model]
25+
model : type[Model]
2226
Data model provided to parse after extracting data using envelope
2327
2428
Returns

aws_lambda_powertools/utilities/parser/envelopes/base.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import logging
44
from abc import ABC, abstractmethod
5-
from typing import Any, Dict, Optional, TypeVar, Union
5+
from typing import TYPE_CHECKING, Any, TypeVar
66

77
from aws_lambda_powertools.utilities.parser.functions import _retrieve_or_set_model_from_cache
8-
from aws_lambda_powertools.utilities.parser.types import T
8+
9+
if TYPE_CHECKING:
10+
from aws_lambda_powertools.utilities.parser.types import T
911

1012
logger = logging.getLogger(__name__)
1113

@@ -14,12 +16,12 @@ class BaseEnvelope(ABC):
1416
"""ABC implementation for creating a supported Envelope"""
1517

1618
@staticmethod
17-
def _parse(data: Optional[Union[Dict[str, Any], Any]], model: type[T]) -> Union[T, None]:
19+
def _parse(data: dict[str, Any] | Any | None, model: type[T]) -> T | None:
1820
"""Parses envelope data against model provided
1921
2022
Parameters
2123
----------
22-
data : Dict
24+
data : dict
2325
Data to be parsed and validated
2426
model : type[T]
2527
Data model to parse and validate data against
@@ -43,7 +45,7 @@ def _parse(data: Optional[Union[Dict[str, Any], Any]], model: type[T]) -> Union[
4345
return adapter.validate_python(data)
4446

4547
@abstractmethod
46-
def parse(self, data: Optional[Union[Dict[str, Any], Any]], model: type[T]):
48+
def parse(self, data: dict[str, Any] | Any | None, model: type[T]):
4749
"""Implementation to parse data against envelope model, then against the data model
4850
4951
NOTE: Call `_parse` method to fully parse data with model provided.

aws_lambda_powertools/utilities/parser/envelopes/bedrock_agent.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1+
from __future__ import annotations
2+
13
import logging
2-
from typing import Any, Dict, Optional, Type, Union
4+
from typing import TYPE_CHECKING, Any
5+
6+
from aws_lambda_powertools.utilities.parser.envelopes.base import BaseEnvelope
7+
from aws_lambda_powertools.utilities.parser.models import BedrockAgentEventModel
38

4-
from ..models import BedrockAgentEventModel
5-
from ..types import Model
6-
from .base import BaseEnvelope
9+
if TYPE_CHECKING:
10+
from aws_lambda_powertools.utilities.parser.types import Model
711

812
logger = logging.getLogger(__name__)
913

1014

1115
class BedrockAgentEnvelope(BaseEnvelope):
1216
"""Bedrock Agent envelope to extract data within input_text key"""
1317

14-
def parse(self, data: Optional[Union[Dict[str, Any], Any]], model: Type[Model]) -> Optional[Model]:
18+
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> Model | None:
1519
"""Parses data found with model provided
1620
1721
Parameters
1822
----------
19-
data : Dict
23+
data : dict
2024
Lambda event to be parsed
21-
model : Type[Model]
25+
model : type[Model]
2226
Data model provided to parse after extracting data using envelope
2327
2428
Returns
2529
-------
26-
Optional[Model]
30+
Model | None
2731
Parsed detail payload with model provided
2832
"""
2933
logger.debug(f"Parsing incoming data with Bedrock Agent model {BedrockAgentEventModel}")

aws_lambda_powertools/utilities/parser/envelopes/cloudwatch.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
from __future__ import annotations
2+
13
import logging
2-
from typing import Any, Dict, List, Optional, Type, Union
4+
from typing import TYPE_CHECKING, Any
5+
6+
from aws_lambda_powertools.utilities.parser.envelopes.base import BaseEnvelope
7+
from aws_lambda_powertools.utilities.parser.models import CloudWatchLogsModel
38

4-
from ..models import CloudWatchLogsModel
5-
from ..types import Model
6-
from .base import BaseEnvelope
9+
if TYPE_CHECKING:
10+
from aws_lambda_powertools.utilities.parser.types import Model
711

812
logger = logging.getLogger(__name__)
913

1014

1115
class CloudWatchLogsEnvelope(BaseEnvelope):
12-
"""CloudWatch Envelope to extract a List of log records.
16+
"""CloudWatch Envelope to extract a list of log records.
1317
1418
The record's body parameter is a string (after being base64 decoded and gzipped),
1519
though it can also be a JSON encoded string.
@@ -18,19 +22,19 @@ class CloudWatchLogsEnvelope(BaseEnvelope):
1822
Note: The record will be parsed the same way so if model is str
1923
"""
2024

21-
def parse(self, data: Optional[Union[Dict[str, Any], Any]], model: Type[Model]) -> List[Optional[Model]]:
25+
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> list[Model | None]:
2226
"""Parses records found with model provided
2327
2428
Parameters
2529
----------
26-
data : Dict
30+
data : dict
2731
Lambda event to be parsed
28-
model : Type[Model]
32+
model : type[Model]
2933
Data model provided to parse after extracting data using envelope
3034
3135
Returns
3236
-------
33-
List
37+
list
3438
List of records parsed with model provided
3539
"""
3640
logger.debug(f"Parsing incoming data with SNS model {CloudWatchLogsModel}")

aws_lambda_powertools/utilities/parser/envelopes/dynamodb.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
from __future__ import annotations
2+
13
import logging
2-
from typing import Any, Dict, List, Optional, Type, Union
4+
from typing import TYPE_CHECKING, Any
5+
6+
from aws_lambda_powertools.utilities.parser.envelopes.base import BaseEnvelope
7+
from aws_lambda_powertools.utilities.parser.models import DynamoDBStreamModel
38

4-
from ..models import DynamoDBStreamModel
5-
from ..types import Model
6-
from .base import BaseEnvelope
9+
if TYPE_CHECKING:
10+
from aws_lambda_powertools.utilities.parser.types import Model
711

812
logger = logging.getLogger(__name__)
913

@@ -15,19 +19,19 @@ class DynamoDBStreamEnvelope(BaseEnvelope):
1519
length of the list is the record's amount in the original event.
1620
"""
1721

18-
def parse(self, data: Optional[Union[Dict[str, Any], Any]], model: Type[Model]) -> List[Dict[str, Optional[Model]]]:
22+
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> list[dict[str, Model | None]]:
1923
"""Parses DynamoDB Stream records found in either NewImage and OldImage with model provided
2024
2125
Parameters
2226
----------
23-
data : Dict
27+
data : dict
2428
Lambda event to be parsed
25-
model : Type[Model]
29+
model : type[Model]
2630
Data model provided to parse after extracting data using envelope
2731
2832
Returns
2933
-------
30-
List
34+
list
3135
List of dictionaries with NewImage and OldImage records parsed with model provided
3236
"""
3337
logger.debug(f"Parsing incoming data with DynamoDB Stream model {DynamoDBStreamModel}")

aws_lambda_powertools/utilities/parser/envelopes/event_bridge.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1+
from __future__ import annotations
2+
13
import logging
2-
from typing import Any, Dict, Optional, Type, Union
4+
from typing import TYPE_CHECKING, Any
5+
6+
from aws_lambda_powertools.utilities.parser.envelopes.base import BaseEnvelope
7+
from aws_lambda_powertools.utilities.parser.models import EventBridgeModel
38

4-
from ..models import EventBridgeModel
5-
from ..types import Model
6-
from .base import BaseEnvelope
9+
if TYPE_CHECKING:
10+
from aws_lambda_powertools.utilities.parser.types import Model
711

812
logger = logging.getLogger(__name__)
913

1014

1115
class EventBridgeEnvelope(BaseEnvelope):
1216
"""EventBridge envelope to extract data within detail key"""
1317

14-
def parse(self, data: Optional[Union[Dict[str, Any], Any]], model: Type[Model]) -> Optional[Model]:
18+
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> Model | None:
1519
"""Parses data found with model provided
1620
1721
Parameters
1822
----------
19-
data : Dict
23+
data : dict
2024
Lambda event to be parsed
21-
model : Type[Model]
25+
model : type[Model]
2226
Data model provided to parse after extracting data using envelope
2327
2428
Returns

aws_lambda_powertools/utilities/parser/envelopes/kafka.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
from __future__ import annotations
2+
13
import logging
2-
from typing import Any, Dict, List, Optional, Type, Union, cast
4+
from typing import TYPE_CHECKING, Any, cast
5+
6+
from aws_lambda_powertools.utilities.parser.envelopes.base import BaseEnvelope
7+
from aws_lambda_powertools.utilities.parser.models import KafkaMskEventModel, KafkaSelfManagedEventModel
38

4-
from ..models import KafkaMskEventModel, KafkaSelfManagedEventModel
5-
from ..types import Model
6-
from .base import BaseEnvelope
9+
if TYPE_CHECKING:
10+
from aws_lambda_powertools.utilities.parser.types import Model
711

812
logger = logging.getLogger(__name__)
913

@@ -17,23 +21,23 @@ class KafkaEnvelope(BaseEnvelope):
1721
all items in the list will be parsed as str and npt as JSON (and vice versa)
1822
"""
1923

20-
def parse(self, data: Optional[Union[Dict[str, Any], Any]], model: Type[Model]) -> List[Optional[Model]]:
24+
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> list[Model | None]:
2125
"""Parses data found with model provided
2226
2327
Parameters
2428
----------
25-
data : Dict
29+
data : dict
2630
Lambda event to be parsed
27-
model : Type[Model]
31+
model : type[Model]
2832
Data model provided to parse after extracting data using envelope
2933
3034
Returns
3135
-------
32-
List
36+
list
3337
List of records parsed with model provided
3438
"""
3539
event_source = cast(dict, data).get("eventSource")
36-
model_parse_event: Union[Type[KafkaMskEventModel], Type[KafkaSelfManagedEventModel]] = (
40+
model_parse_event: type[KafkaMskEventModel | KafkaSelfManagedEventModel] = (
3741
KafkaMskEventModel if event_source == "aws:kafka" else KafkaSelfManagedEventModel
3842
)
3943

aws_lambda_powertools/utilities/parser/envelopes/kinesis.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
from __future__ import annotations
2+
13
import logging
2-
from typing import Any, Dict, List, Optional, Type, Union, cast
4+
from typing import TYPE_CHECKING, Any, cast
5+
6+
from aws_lambda_powertools.utilities.parser.envelopes.base import BaseEnvelope
7+
from aws_lambda_powertools.utilities.parser.models import KinesisDataStreamModel
38

4-
from ..models import KinesisDataStreamModel
5-
from ..types import Model
6-
from .base import BaseEnvelope
9+
if TYPE_CHECKING:
10+
from aws_lambda_powertools.utilities.parser.types import Model
711

812
logger = logging.getLogger(__name__)
913

@@ -19,19 +23,19 @@ class KinesisDataStreamEnvelope(BaseEnvelope):
1923
all items in the list will be parsed as str and not as JSON (and vice versa)
2024
"""
2125

22-
def parse(self, data: Optional[Union[Dict[str, Any], Any]], model: Type[Model]) -> List[Optional[Model]]:
26+
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> list[Model | None]:
2327
"""Parses records found with model provided
2428
2529
Parameters
2630
----------
27-
data : Dict
31+
data : dict
2832
Lambda event to be parsed
29-
model : Type[Model]
33+
model : type[Model]
3034
Data model provided to parse after extracting data using envelope
3135
3236
Returns
3337
-------
34-
List
38+
list
3539
List of records parsed with model provided
3640
"""
3741
logger.debug(f"Parsing incoming data with Kinesis model {KinesisDataStreamModel}")

0 commit comments

Comments
 (0)