Skip to content

fix(parser): support TypeAdapter instances as models #5535

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 2 commits into from
Nov 11, 2024

Conversation

leandrodamascena
Copy link
Contributor

Issue number: #5476

Summary

Changes

The current implementation of event_parser fails when a TypeAdapter instance is provided as the model. This is due to an attempt to cast an existing TypeAdapter into a new TypeAdapter, which results in a PydanticSchemaGenerationError.

Changes Made

  1. Modified _retrieve_or_set_model_from_cache function to check if the input is already a TypeAdapter instance.
  2. If the input is a TypeAdapter, it's returned directly from the cache without additional wrapping.
  3. Maintained backwards compatibility for non-TypeAdapter models.

User experience

This code works now.

from typing import Literal, Union, Annotated

from pydantic import Field, TypeAdapter

# from aws_lambda_powertools.utilities.parser import event_parser

from aws_lambda_powertools.utilities.parser.models.event_bridge import EventBridgeModel
from aws_lambda_powertools.utilities.parser.models.s3 import (
    S3EventNotificationEventBridgeDetailModel,
)

class S3EventNotificationEventBridgeModel(EventBridgeModel):
    detail: S3EventNotificationEventBridgeDetailModel
    source: Literal["aws.s3"]


class ScheduledNotificationEventBridgeModel(EventBridgeModel):
    source: Literal["aws.events"]


EventBridgeSource = Annotated[
    Union[S3EventNotificationEventBridgeModel, ScheduledNotificationEventBridgeModel],
    Field(discriminator="source"),
]

EventBridgeModelAdapter = TypeAdapter(Union[EventBridgeSource, EventBridgeModel])

@event_parser(model=EventBridgeModelAdapter)
def handler(event: EventBridgeModelAdapter, context):
    print(f'Got event, parsed into {type(event)=}')

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.

@leandrodamascena leandrodamascena requested a review from a team November 9, 2024 16:55
@boring-cyborg boring-cyborg bot added the tests label Nov 9, 2024
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Nov 9, 2024
@leandrodamascena leandrodamascena linked an issue Nov 9, 2024 that may be closed by this pull request
2 tasks
@leandrodamascena leandrodamascena self-assigned this Nov 9, 2024
@github-actions github-actions bot added the bug Something isn't working label Nov 9, 2024
Copy link

codecov bot commented Nov 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.16%. Comparing base (fdc9416) to head (24a5902).
Report is 2 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #5535   +/-   ##
========================================
  Coverage    96.16%   96.16%           
========================================
  Files          229      229           
  Lines        10810    10812    +2     
  Branches      2007     2008    +1     
========================================
+ Hits         10395    10397    +2     
  Misses         327      327           
  Partials        88       88           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@leandrodamascena leandrodamascena merged commit 217f985 into develop Nov 11, 2024
12 checks passed
@leandrodamascena leandrodamascena deleted the parser/fix-typeadapter branch November 11, 2024 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Using discriminator field with TypeAdapter instances
2 participants