Skip to content

Allow extended parser models to pass mypy #849

Closed
@ClaytonJY

Description

@ClaytonJY

Is your feature request related to a problem? Please describe.

The docs example for extending parser models (https://awslabs.github.io/aws-lambda-powertools-python/latest/utilities/parser/#extending-built-in-models) doesn't pass mypy, because the Order class is not compatible with the EventBridgeModel annotation detail: Dict[str, Any].

error: Incompatible types in assignment (expression has type "Order", base class "EventBridgeModel" defined the type as "Dict[str, Any]")

I would like to be able to both extend this model and have my code pass type checks without # type: ignore or similar.

I suspect this affects other parser models, like the Message field of SnsNotificationModel.

Describe the solution you'd like

One solution is to change only the detail type annotation, e.g. Union[Dict[str, Any], BaseModel], but there may be other things to add to that list and it doesn't feel great; there could be many other types that would parse fine and could be listed there, right? Or is that it, because you either use EventBridgeModel directly (Dict) or you subclass it (BaseModel)?

Is there anything that could be done with typing.Protocol, to check the class has certain methods regardless of name?

There might also be an argument to not change the type, but note in the docs that this will fail type-checks and instruct how to silence mypy in such instances.

Describe alternatives you've considered

I tried various solutions which didn't work, like adding dict or TypedDict to the parent class list for Order (incompatible methods), or changing it to TypedDict (same error as above).

Additional context

I'm using the pydantic.mypy extension.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingp1

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions