Static typing: type inconsistency with BaseEnvelope and DictWrapper
See original GitHub issueStatic 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:
- Created a year ago
- Comments:6 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 ofAPIGatewayProxyEventModel
which is a Pydantic model in itself.Understanding what you’re after will help us provide a more accurate solution.
Look forward to hearing from you 😃
@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?