Skip to content

feat(parser): Allow primitive data types to be parsed using TypeAdapter #4502

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Jun 26, 2024

Conversation

sthulb
Copy link
Contributor

@sthulb sthulb commented Jun 13, 2024

Issue number: #2734

Summary

This PR allows customers to use union types as per #2734.

Changes

Please provide a summary of what's being changed

This change adopts the TypeAdapter from Pydantic v2 to allow the parser and base envelope to validate between the potentially different types.

User experience

Please share what the user experience looks like before and after this change

from aws_lambda_powertools.utilities.parser import event_parser
from pydantic import BaseModel, Field
from typing import Annotated, Any, Literal, Union


class SuccessfulCallback(BaseModel):
    status: Literal["succeeded"]
    name: str
    breed: Literal["Husky", "Labrador"]


class FailedCallback(BaseModel):
    status: Literal["failed"]
    error: str


DogCallback = Annotated[
    Union[SuccessfulCallback, FailedCallback], Field(discriminator="status")
]

@event_parser(model=DogCallback)
def lambda_handler(event: DogCallback, _: Any) -> str:
    if isinstance(event, FailedCallback): # alternatively `if event.status == "failed"` (if your type checker is on top of things)
        return f"Uh oh. Had a problem: {event.error}"

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

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

Acknowledgment

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

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@sthulb sthulb requested a review from a team June 13, 2024 14:56
@boring-cyborg boring-cyborg bot added github-actions Pull requests that update Github_actions code internal Maintenance changes labels Jun 13, 2024
@sthulb sthulb changed the base branch from develop to v3 June 13, 2024 14:56
@pull-request-size pull-request-size bot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Jun 13, 2024
@boring-cyborg boring-cyborg bot added the tests label Jun 13, 2024
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jun 13, 2024
Copy link
Contributor

⚠️Large PR detected⚠️

Please consider breaking into smaller PRs to avoid significant review delays. Ignore if this PR has naturally grown to this size after reviews.

@github-actions github-actions bot added feature New feature or functionality and removed internal Maintenance changes labels Jun 13, 2024
Copy link
Contributor

⚠️Large PR detected⚠️

Please consider breaking into smaller PRs to avoid significant review delays. Ignore if this PR has naturally grown to this size after reviews.

1 similar comment
Copy link
Contributor

⚠️Large PR detected⚠️

Please consider breaking into smaller PRs to avoid significant review delays. Ignore if this PR has naturally grown to this size after reviews.

@sthulb sthulb removed the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Jun 13, 2024
@leandrodamascena
Copy link
Contributor

Before I start reviewing this PR, I'm curious as to why CI wasn't run here. Any thoughts @sthulb?

@sthulb
Copy link
Contributor Author

sthulb commented Jun 13, 2024

I'd like to say I know why, but I don't 😂

@leandrodamascena
Copy link
Contributor

leandrodamascena commented Jun 16, 2024

Hey @sthulb, I was reviewing this PR and exploring more use cases where TypeAdapter can be beneficial. I believe that if we go in this direction, we can make the Parser feature much more flexible without introducing a significant breaking change, I really like this.

When it comes to Pydantic v2, there are no significant performance changes when using model_validate or model_validate_json and TypeAdapter with validate_json and validate_python. Instead, I noticed that when using a dataclass with TypeAdapter, the performance is better than using a Pydantic model.

In my point of view I think we can move forward with this solution and fix some small things in this PR. I'll schedule a discussion with you and @heitorlessa to talk about this.

Thanks

Things missing in this PR

  • Update PR body with use case and example
  • Fix tests
  • Update messages when raising InvalidModelTypeError error - we now support many more types than just BaseModel
  • Update documentation - We need to improve as a whole in another PR
  • Update docstrings

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jun 18, 2024
@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jun 18, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jun 18, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jun 18, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jun 25, 2024
@pull-request-size pull-request-size bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 25, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jun 25, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jun 25, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jun 25, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jun 25, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jun 25, 2024
@leandrodamascena
Copy link
Contributor

Hi @sthulb! Thank you for addressing all the feedback! I've made a few minor changes, such as creating a dedicated function to handle the cache. Since we need to use this in two places, I believe this approach makes more sense. I've also removed the pydanticv1 compatibility, as we no longer require it.

Additionally, I've created end-to-end (e2e) tests and a performance test.

@heitorlessa we need a final review here before merge.

@leandrodamascena leandrodamascena changed the title feat(parser): Allow arbitrary data types to be parsed feat(parser): Allow primitive data types to be parsed using TypeAdapter Jun 26, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jun 26, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jun 26, 2024
Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SHIIIIPPPPP IT!!

Superb work you two!

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jun 26, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jun 26, 2024
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or functionality github-actions Pull requests that update Github_actions code size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants