Skip to content

Commit 1585b66

Browse files
fix(parser): type hints should reflect primitive types support (#8175)
* fix: parser type hints should reflect primitive types As per #4502, primitive types are supported by the `parser` utility. However, the type hints utilized in the functions and envelopes there do not reflect that at the moment, making type checkers lack when inferring the return types, for example. This aims to improve this situation by adjusting the type hints there. * fix: small changes --------- Co-authored-by: Leandro Damascena <lcdama@amazon.pt>
1 parent 2e11129 commit 1585b66

19 files changed

Lines changed: 83 additions & 73 deletions

aws_lambda_powertools/utilities/parser/envelopes/apigw.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@
77
from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventModel
88

99
if TYPE_CHECKING:
10-
from aws_lambda_powertools.utilities.parser.types import Model
10+
from aws_lambda_powertools.utilities.parser.types import T
1111

1212
logger = logging.getLogger(__name__)
1313

1414

1515
class ApiGatewayEnvelope(BaseEnvelope):
1616
"""API Gateway envelope to extract data within body key"""
1717

18-
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> Model | None:
18+
def parse(self, data: dict[str, Any] | Any | None, model: type[T]) -> T | None:
1919
"""Parses data found with model provided
2020
2121
Parameters
2222
----------
2323
data : dict
2424
Lambda event to be parsed
25-
model : type[Model]
25+
model : type[T]
2626
Data model provided to parse after extracting data using envelope
2727
2828
Returns
2929
-------
30-
Any
30+
T | None
3131
Parsed detail payload with model provided
3232
"""
3333
logger.debug(f"Parsing incoming data with Api Gateway model {APIGatewayProxyEventModel}")

aws_lambda_powertools/utilities/parser/envelopes/apigw_websocket.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from aws_lambda_powertools.utilities.parser.models import APIGatewayWebSocketMessageEventModel
88

99
if TYPE_CHECKING:
10-
from aws_lambda_powertools.utilities.parser.types import Model
10+
from aws_lambda_powertools.utilities.parser.types import T
1111

1212
logger = logging.getLogger(__name__)
1313

@@ -16,19 +16,19 @@ class ApiGatewayWebSocketEnvelope(BaseEnvelope):
1616
"""API Gateway WebSockets envelope to extract data within body key of messages routes
1717
(not disconnect or connect)"""
1818

19-
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> Model | None:
19+
def parse(self, data: dict[str, Any] | Any | None, model: type[T]) -> T | None:
2020
"""Parses data found with model provided
2121
2222
Parameters
2323
----------
2424
data : dict
2525
Lambda event to be parsed
26-
model : type[Model]
26+
model : type[T]
2727
Data model provided to parse after extracting data using envelope
2828
2929
Returns
3030
-------
31-
Any
31+
T | None
3232
Parsed detail payload with model provided
3333
"""
3434
logger.debug(

aws_lambda_powertools/utilities/parser/envelopes/apigwv2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@
77
from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventV2Model
88

99
if TYPE_CHECKING:
10-
from aws_lambda_powertools.utilities.parser.types import Model
10+
from aws_lambda_powertools.utilities.parser.types import T
1111

1212
logger = logging.getLogger(__name__)
1313

1414

1515
class ApiGatewayV2Envelope(BaseEnvelope):
1616
"""API Gateway V2 envelope to extract data within body key"""
1717

18-
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> Model | None:
18+
def parse(self, data: dict[str, Any] | Any | None, model: type[T]) -> T | None:
1919
"""Parses data found with model provided
2020
2121
Parameters
2222
----------
2323
data : dict
2424
Lambda event to be parsed
25-
model : type[Model]
25+
model : type[T]
2626
Data model provided to parse after extracting data using envelope
2727
2828
Returns
2929
-------
30-
Any
30+
T | None
3131
Parsed detail payload with model provided
3232
"""
3333
logger.debug(f"Parsing incoming data with Api Gateway model V2 {APIGatewayProxyEventV2Model}")

aws_lambda_powertools/utilities/parser/envelopes/base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ def _parse(data: dict[str, Any] | Any | None, model: type[T]) -> T | None:
4444
return _parse_and_validate_event(data=data, adapter=adapter)
4545

4646
@abstractmethod
47-
def parse(self, data: dict[str, Any] | Any | None, model: type[T]):
47+
def parse(
48+
self,
49+
data: dict[str, Any] | Any | None,
50+
model: type[T],
51+
) -> T | list[T | None] | list[dict[str, T | None]] | None:
4852
"""Implementation to parse data against envelope model, then against the data model
4953
5054
NOTE: Call `_parse` method to fully parse data with model provided.

aws_lambda_powertools/utilities/parser/envelopes/bedrock_agent.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@
77
from aws_lambda_powertools.utilities.parser.models import BedrockAgentEventModel, BedrockAgentFunctionEventModel
88

99
if TYPE_CHECKING:
10-
from aws_lambda_powertools.utilities.parser.types import Model
10+
from aws_lambda_powertools.utilities.parser.types import T
1111

1212
logger = logging.getLogger(__name__)
1313

1414

1515
class BedrockAgentEnvelope(BaseEnvelope):
1616
"""Bedrock Agent envelope to extract data within input_text key"""
1717

18-
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> Model | None:
18+
def parse(self, data: dict[str, Any] | Any | None, model: type[T]) -> T | None:
1919
"""Parses data found with model provided
2020
2121
Parameters
2222
----------
2323
data : dict
2424
Lambda event to be parsed
25-
model : type[Model]
25+
model : type[T]
2626
Data model provided to parse after extracting data using envelope
2727
2828
Returns
2929
-------
30-
Model | None
30+
T | None
3131
Parsed detail payload with model provided
3232
"""
3333
logger.debug(f"Parsing incoming data with Bedrock Agent model {BedrockAgentEventModel}")
@@ -39,19 +39,19 @@ def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> Model
3939
class BedrockAgentFunctionEnvelope(BaseEnvelope):
4040
"""Bedrock Agent Function envelope to extract data within input_text key"""
4141

42-
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> Model | None:
42+
def parse(self, data: dict[str, Any] | Any | None, model: type[T]) -> T | None:
4343
"""Parses data found with model provided
4444
4545
Parameters
4646
----------
4747
data : dict
4848
Lambda event to be parsed
49-
model : type[Model]
49+
model : type[T]
5050
Data model provided to parse after extracting data using envelope
5151
5252
Returns
5353
-------
54-
Model | None
54+
T | None
5555
Parsed detail payload with model provided
5656
"""
5757
logger.debug(f"Parsing incoming data with Bedrock Agent Function model {BedrockAgentFunctionEventModel}")

aws_lambda_powertools/utilities/parser/envelopes/cloudwatch.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from aws_lambda_powertools.utilities.parser.models import CloudWatchLogsModel
88

99
if TYPE_CHECKING:
10-
from aws_lambda_powertools.utilities.parser.types import Model
10+
from aws_lambda_powertools.utilities.parser.types import T
1111

1212
logger = logging.getLogger(__name__)
1313

@@ -22,19 +22,19 @@ class CloudWatchLogsEnvelope(BaseEnvelope):
2222
Note: The record will be parsed the same way so if model is str
2323
"""
2424

25-
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> list[Model | None]:
25+
def parse(self, data: dict[str, Any] | Any | None, model: type[T]) -> list[T | None]:
2626
"""Parses records found with model provided
2727
2828
Parameters
2929
----------
3030
data : dict
3131
Lambda event to be parsed
32-
model : type[Model]
32+
model : type[T]
3333
Data model provided to parse after extracting data using envelope
3434
3535
Returns
3636
-------
37-
list
37+
list[T | None]
3838
List of records parsed with model provided
3939
"""
4040
logger.debug(f"Parsing incoming data with SNS model {CloudWatchLogsModel}")

aws_lambda_powertools/utilities/parser/envelopes/dynamodb.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from aws_lambda_powertools.utilities.parser.models import DynamoDBStreamModel
88

99
if TYPE_CHECKING:
10-
from aws_lambda_powertools.utilities.parser.types import Model
10+
from aws_lambda_powertools.utilities.parser.types import T
1111

1212
logger = logging.getLogger(__name__)
1313

@@ -19,19 +19,19 @@ class DynamoDBStreamEnvelope(BaseEnvelope):
1919
length of the list is the record's amount in the original event.
2020
"""
2121

22-
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> list[dict[str, Model | None]]:
22+
def parse(self, data: dict[str, Any] | Any | None, model: type[T]) -> list[dict[str, T | None]]:
2323
"""Parses DynamoDB Stream records found in either NewImage and OldImage with model provided
2424
2525
Parameters
2626
----------
2727
data : dict
2828
Lambda event to be parsed
29-
model : type[Model]
29+
model : type[T]
3030
Data model provided to parse after extracting data using envelope
3131
3232
Returns
3333
-------
34-
list
34+
list[T | None]
3535
List of dictionaries with NewImage and OldImage records parsed with model provided
3636
"""
3737
logger.debug(f"Parsing incoming data with DynamoDB Stream model {DynamoDBStreamModel}")

aws_lambda_powertools/utilities/parser/envelopes/event_bridge.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@
77
from aws_lambda_powertools.utilities.parser.models import EventBridgeModel
88

99
if TYPE_CHECKING:
10-
from aws_lambda_powertools.utilities.parser.types import Model
10+
from aws_lambda_powertools.utilities.parser.types import T
1111

1212
logger = logging.getLogger(__name__)
1313

1414

1515
class EventBridgeEnvelope(BaseEnvelope):
1616
"""EventBridge envelope to extract data within detail key"""
1717

18-
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> Model | None:
18+
def parse(self, data: dict[str, Any] | Any | None, model: type[T]) -> T | None:
1919
"""Parses data found with model provided
2020
2121
Parameters
2222
----------
2323
data : dict
2424
Lambda event to be parsed
25-
model : type[Model]
25+
model : type[T]
2626
Data model provided to parse after extracting data using envelope
2727
2828
Returns
2929
-------
30-
Any
30+
T | None
3131
Parsed detail payload with model provided
3232
"""
3333
logger.debug(f"Parsing incoming data with EventBridge model {EventBridgeModel}")

aws_lambda_powertools/utilities/parser/envelopes/kafka.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from aws_lambda_powertools.utilities.parser.models import KafkaMskEventModel, KafkaSelfManagedEventModel
88

99
if TYPE_CHECKING:
10-
from aws_lambda_powertools.utilities.parser.types import Model
10+
from aws_lambda_powertools.utilities.parser.types import T
1111

1212
logger = logging.getLogger(__name__)
1313

@@ -21,19 +21,19 @@ class KafkaEnvelope(BaseEnvelope):
2121
all items in the list will be parsed as str and npt as JSON (and vice versa)
2222
"""
2323

24-
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> list[Model | None]:
24+
def parse(self, data: dict[str, Any] | Any | None, model: type[T]) -> list[T | None]:
2525
"""Parses data found with model provided
2626
2727
Parameters
2828
----------
2929
data : dict
3030
Lambda event to be parsed
31-
model : type[Model]
31+
model : type[T]
3232
Data model provided to parse after extracting data using envelope
3333
3434
Returns
3535
-------
36-
list
36+
list[T | None]
3737
List of records parsed with model provided
3838
"""
3939
event_source = cast(dict, data).get("eventSource")

aws_lambda_powertools/utilities/parser/envelopes/kinesis.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from aws_lambda_powertools.utilities.parser.models import KinesisDataStreamModel
99

1010
if TYPE_CHECKING:
11-
from aws_lambda_powertools.utilities.parser.types import Model
11+
from aws_lambda_powertools.utilities.parser.types import T
1212

1313
logger = logging.getLogger(__name__)
1414

@@ -24,19 +24,19 @@ class KinesisDataStreamEnvelope(BaseEnvelope):
2424
all items in the list will be parsed as str and not as JSON (and vice versa)
2525
"""
2626

27-
def parse(self, data: dict[str, Any] | Any | None, model: type[Model]) -> list[Model | None]:
27+
def parse(self, data: dict[str, Any] | Any | None, model: type[T]) -> list[T | None]:
2828
"""Parses records found with model provided
2929
3030
Parameters
3131
----------
3232
data : dict
3333
Lambda event to be parsed
34-
model : type[Model]
34+
model : type[T]
3535
Data model provided to parse after extracting data using envelope
3636
3737
Returns
3838
-------
39-
list
39+
list[T | None]
4040
List of records parsed with model provided
4141
"""
4242
logger.debug(f"Parsing incoming data with Kinesis model {KinesisDataStreamModel}")

0 commit comments

Comments
 (0)