-
Notifications
You must be signed in to change notification settings - Fork 401
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
Bug: Using discriminator
field with TypeAdapter instances
#5476
Comments
Your
```python
In [13]: EventBridgeModelAdapter = TypeAdapter(Union[EventBridgeSource, EventBridgeModel])
In [14]: _retrieve_or_set_model_from_cache(EventBridgeModelAdapter)AttributeError Traceback (most recent call last) File /usr/local/lib/python3.11/site-packages/pydantic/type_adapter.py:112, in _getattr_no_parents(obj, attribute) AttributeError: pydantic_core_schema During handling of the above exception, another exception occurred: PydanticSchemaGenerationError Traceback (most recent call last) File /usr/local/lib/python3.11/site-packages/aws_lambda_powertools/utilities/parser/functions.py:43, in _retrieve_or_set_model_from_cache(model) File /usr/local/lib/python3.11/site-packages/pydantic/type_adapter.py:257, in TypeAdapter.init(self, type, config, _parent_depth, module) File /usr/local/lib/python3.11/site-packages/pydantic/type_adapter.py:135, in _frame_depth..wrapper..wrapped(self, *args, **kwargs) File /usr/local/lib/python3.11/site-packages/pydantic/type_adapter.py:277, in TypeAdapter._init_core_attrs(self, rebuild_mocks) File /usr/local/lib/python3.11/site-packages/pydantic/type_adapter.py:95, in get_schema(type, config_wrapper, parent_depth) File /usr/local/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py:655, in GenerateSchema.generate_schema(self, obj, from_dunder_get_core_schema) File /usr/local/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py:929, in GenerateSchema._generate_schema_inner(self, obj) File /usr/local/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py:1038, in GenerateSchema.match_type(self, obj) File /usr/local/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py:558, in GenerateSchema._unknown_type_schema(self, obj) PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <pydantic.type_adapter.TypeAdapter object at 0x7ffffc34ff10>. Set If you got this error by calling handler() within For further information visit https://errors.pydantic.dev/2.9/u/schema-for-unknown-type
|
Hi @xkortex! Thanks a lot for reporting this bug, we really need to fix this bug when passing a In theory, you don't need to use class S3EventNotificationEventBridgeModel(EventBridgeModel):
detail: S3EventNotificationEventBridgeDetailModel
source: Literal["aws.s3"]
class ScheduledNotificationEventBridgeModel(EventBridgeModel):
source: Literal["aws.events"]
EventBridgeModelAdapter = Annotated[
Union[S3EventNotificationEventBridgeModel, ScheduledNotificationEventBridgeModel, EventBridgeModel],
Field(discriminator="source"),
]
@event_parser(model=EventBridgeModelAdapter)
def handler(event: EventBridgeModelAdapter, context = None):
print(f'Got event, parsed into {type(event)=}') Another thing is that considering you are using the code you sent here, you should not use this That said, would you be available to submit a PR to fix this? I'd love to have your contribution. The solution you sent works perfectly. |
discriminator
field to parse Event modelsdiscriminator
field with TypeAdapter instances
|
This is now released under 3.3.0 version! |
Use case
I happened upon using the
discriminator
to allow my lambdas to handle multiple different event sources, e.g.aws.events
andaws.s3
. Using Pydantic'sTypeAdapter
andField
classes, we can create an adapter which looks at a specific field (in this casesource
) to determine how to parse the data, without having to do the expensive try-validate-fail-try_next_model pattern. This allows us to decorate a lambda in such a way that it can automatically cast different one of several event types to stricter subtypesSolution/User Experience
This fails with the stock
event_parser
so I had to bodgeevent_parser
to get it to work. I think the issue is trying to cast the inputTypeAdapter
into aTypeAdapter
again, throwingPydanticSchemaGenerationError
. It should be straightforward to allow_retrieve_or_set_model_from_cache
to check if the input is already aTypeAdapter
and return that from the cache rather than trying to wrap it.Setup
Testing
results
Alternative solutions
Not sure how else to implement this.
Acknowledgment
The text was updated successfully, but these errors were encountered: