question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow extended parser models to pass mypy

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:14 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
heitorlessacommented, Dec 10, 2021

PR available - should be available in the next release: https://github.com/awslabs/aws-lambda-powertools-python/pull/883

1reaction
ClaytonJYcommented, Dec 6, 2021

@heitorlessa here’s that reprex, as a new repo: https://github.com/ClaytonJY/powertools-type-error-example

thanks for caring!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Extending and integrating mypy - mypy 0.991 documentation
It is possible to integrate mypy into another Python 3 application by importing mypy.api and calling the run function with a parameter of...
Read more >
Parser (Pydantic) - AWS Lambda Powertools for Python
Extending built-in models​​ You can extend them to include your own models, and yet have all other known fields parsed along the way....
Read more >
Passing pydantic FieldType as an argument and checking ...
I'm trying to use pydantic and mypy at the same time and I get an error when I try to pass a pydantic...
Read more >
Python Type Checking (Guide) - Real Python
Unfortunately, ignoring the import means that Mypy has no way of discovering the bug in our program. A better solution would be to...
Read more >
Not Your Pie, MyPy! - Nylas
Mypy is a tool that's beloved at Nylas – it allows you to make your Python code type-aware. It takes the burden of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found