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.

Static typing: type inconsistency with BaseEnvelope and DictWrapper

See original GitHub issue

Static type checker used

{“label”=>“mypy (project’s standard)”}

AWS Lambda function runtime

{“label”=>“3.8”}

AWS Lambda Powertools for Python version

1.20.2

Static type checker info

Argument 1 to “APIGatewayProxyEvent” has incompatible type “Union[Dict[str, Any], Any, None]”; expected "Dict[str, Any]"mypy(error)

Code snippet

class SomeEnvelope(BaseEnvelope):

    def parse(self, data: Optional[Union[Dict[str, Any], Any]], model: Type[Model]) -> Optional[Model]:

        parsed_env = APIGatewayProxyEvent(data)
        data = dict(
            headers=parsed_env.headers or {},
            http_method=parsed_env.http_method,
            path=parsed_env.path,
            path_parameters=parsed_env.path_parameters or {},
            query_parameters=parsed_env.query_string_parameters or {},
            body=json.loads(parsed_env.body) if parsed_env.body else None,
        )
        return self._parse(data=data, model=model)

Possible Solution

update DictWrapper into receiving Optional[Union[Dict[str, Any], Any]]

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
heitorlessacommented, Apr 12, 2022

hey @heitorlessa, just tested with the latest version 1.25.7 and I’m still getting the same error.

I’ve checked ant the problem seems to be on the __ini__ of DictWrapper.

APIGatewayProxyEvent --> BaseProxyEvent --> DictWrapper

This is DictWrapper’s __init__ on latest version:

def __init__(self, data: Dict[str, Any]): self._data = data

Let’s take a step back: what are you trying to achieve? Mypy and the types are correct, as you’re accidentally using the incorrect class - APIGatewayProxyEvent instead of APIGatewayProxyEventModel which is a Pydantic model in itself.

from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventModel
# Or if using HTTP API with v2 payload
# from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventV2Model

Understanding what you’re after will help us provide a more accurate solution.

Look forward to hearing from you 😃

1reaction
michaelbrewercommented, Apr 11, 2022

@heitorlessa @efrquerido - but APIGatewayProxyEvent (aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py) was built to except the original raw dict event and not the Pydantic version.

If you are using Pydantic, then why not stick to ApiGatewayEnvelope (aws_lambda_powertools/utilities/parser/envelopes/apigw.py), but if you still want both, then maybe convert data to a dict?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use static type checking using Dict with different value ...
You need a Union type, of some sort. from typing import Dict, List, Union # simple str values hour_dict: Dict[str, str] = {"test_key": ......
Read more >
Is static typing worth the trade-offs?
It's sort of a myth that programmers don't have to worry about types in dynamically typed languages. In dynamically typed languages:.
Read more >
All About Typing: Explicit Vs. Implicit and Static Vs. Dynamic
Dynamic typing refers to compilers that deal more with the types during runtime, rather than when the program is compiled. Like static typing, ......
Read more >
Static vs. dynamic typing: The details and differences
Currently, the two most common approaches to variable typing are static typing and dynamic typing. When it comes to handling types, ...
Read more >
Dynamic typing vs. static typing - Oracle Help Center
First, dynamically-typed languages perform type checking at runtime, while statically typed languages perform type checking at compile time.
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