Allow extended parser models to pass mypy
See original GitHub issueIs 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.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:14 (10 by maintainers)
PR available - should be available in the next release: https://github.com/awslabs/aws-lambda-powertools-python/pull/883
@heitorlessa here’s that reprex, as a new repo: https://github.com/ClaytonJY/powertools-type-error-example
thanks for caring!