Description
The output type specified for aws_lambda_powertools.utilities.parser.parse
does not always match the documented or actual output. The output is often a collection but the specified type is a single object.
Expected Behavior
The return type for parse
should match the documented output types for each envelope.
Current Behavior
The return type for parse
is Model
(the type of the model
parameter's input) even though the returned value may be a collection with a more complex type such as List[Dict[str, Optional[Model]]]
.
Possible Solution
There may be a way to solve this using mypy generics but I can't think of how. The return type could be specified manually as something like the following Union
of all the possibilities:
Union[Model, List[Optional[Model]], List[Dict[str, Optional[Model]]]]
This would have to be updated as envelopes are added, and it wouldn't cover custom envelopes.
Alternatively, removing the output type annotation from the toplevel parse
might allow the type of the underlying envelope's parse
function to bubble up.
Steps to Reproduce
from aws_lambda_powertools.utilities.parser import parse, envelopes, BaseModel
class Item(BaseModel):
pass
event: dict = {"Records": []}
result: list[Item] = parse(event=event, model=Item, envelope=envelopes.SnsEnvelope)
print(type(result))
Output:
<class 'list'>
Mypy output:
parser_type_example.py:9: error: Incompatible types in assignment (expression has type "Item", variable has type "List[Item]")
Found 1 error in 1 file (checked 1 source file)
Environment
- Powertools version used:
1.21.1 - Packaging format (Layers, PyPi):
Pypi - AWS Lambda function runtime:
n/a - Debugging logs
n/a