Skip to content

fix(parser): mypy support for payload type override as models #883

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged

Conversation

heitorlessa
Copy link
Contributor

Issue #, if available: #857

Description of changes:

This PR allows MyPy to not trigger an error when overriding payload types in built-in models provided by Parser, as described: https://awslabs.github.io/aws-lambda-powertools-python/latest/utilities/parser/#extending-built-in-models

Scenario: There are cases where you want to provide your own Model while retaining access to metadata information about the event. Previously, when using MyPy, this would cause a type override error since payload fields would only support what's expected, not a custom model as docs suggest for non-MyPy customers.

from aws_lambda_powertools.utilities.parser import parse, BaseModel
from aws_lambda_powertools.utilities.parser.models import EventBridgeModel

from typing import List, Optional


class OrderItem(BaseModel):
    id: int
    quantity: int
    description: str


class Order(BaseModel):
    id: int
    description: str
    items: List[OrderItem]


class OrderEventModel(EventBridgeModel):
    detail: Order

Checklist

Breaking change checklist

RFC issue #:

  • Migration process documented
  • Implement warnings (if it can live side by side)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Dec 10, 2021
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Dec 10, 2021
@heitorlessa heitorlessa added the bug Something isn't working label Dec 10, 2021
Co-authored-by: Tom McCarthy <tmac.se@gmail.com>
@codecov-commenter
Copy link

codecov-commenter commented Dec 10, 2021

Codecov Report

Merging #883 (adf324d) into develop (4994349) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop     #883   +/-   ##
========================================
  Coverage    99.88%   99.88%           
========================================
  Files          118      118           
  Lines         5129     5133    +4     
  Branches       573      573           
========================================
+ Hits          5123     5127    +4     
  Misses           2        2           
  Partials         4        4           
Impacted Files Coverage Δ
...s_lambda_powertools/utilities/parser/models/alb.py 100.00% <100.00%> (ø)
...lambda_powertools/utilities/parser/models/apigw.py 100.00% <100.00%> (ø)
...mbda_powertools/utilities/parser/models/apigwv2.py 100.00% <100.00%> (ø)
...a_powertools/utilities/parser/models/cloudwatch.py 100.00% <100.00%> (ø)
...bda_powertools/utilities/parser/models/dynamodb.py 100.00% <100.00%> (ø)
...powertools/utilities/parser/models/event_bridge.py 100.00% <100.00%> (ø)
...mbda_powertools/utilities/parser/models/kinesis.py 100.00% <100.00%> (ø)
...ws_lambda_powertools/utilities/parser/models/s3.py 100.00% <100.00%> (ø)
...ertools/utilities/parser/models/s3_object_event.py 100.00% <100.00%> (ø)
...s_lambda_powertools/utilities/parser/models/sns.py 100.00% <100.00%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4994349...adf324d. Read the comment docs.

@heitorlessa heitorlessa merged commit 8de3729 into aws-powertools:develop Dec 10, 2021
@heitorlessa heitorlessa deleted the fix/parser-override-model branch December 10, 2021 08:50
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants