Skip to content

Incorrect type annotation for parse function #802

Closed
@DanLipsitt

Description

@DanLipsitt

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingp2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions