Skip to content
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

More union serialization tidying #1536

Merged
merged 2 commits into from
Nov 11, 2024
Merged

Conversation

sydney-runkle
Copy link
Member

@sydney-runkle sydney-runkle commented Nov 8, 2024

  1. Try value[discriminator_key] or value.key depending on the type of value
  2. Only raise discriminator not found warnings if we're at the top level of a union, following up on Unstable behavior for warning Failed to get discriminator value for tagged union serialization ? pydantic#10657

1 fixes this issue that @Kludex reported, the following now runs with no warnings

from typing import Literal, Annotated
from pydantic import TypeAdapter, Discriminator
from typing_extensions import TypedDict

class ModelA(TypedDict):
    a: str
    state: Literal['ModelA']

class ModelB(TypedDict):
    b: str
    state: Literal['ModelB']

ta = TypeAdapter(Annotated[ModelA | ModelB, Discriminator('state')])
model_a = ta.validate_python({'a': 'a', 'state': 'ModelA'})
model_b = ta.validate_python({'b': 'b', 'state': 'ModelB'})
ta.dump_python(model_a)
ta.dump_python(model_b)

2 fixes pydantic/pydantic#10657

I've given some thought to how we might want to do a refactor here to minimize shared logic. I'm a bit held up on conflicting function signatures, so perhaps we could chat about that on Monday @davidhewitt :).

Copy link

codspeed-hq bot commented Nov 8, 2024

CodSpeed Performance Report

Merging #1536 will not alter performance

Comparing typed-dict-discriminator-warning (132dc48) with main (2419981)

Summary

✅ 155 untouched benchmarks

Copy link
Contributor

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Regarding the shared logic, happy to chat about that separately & save that for a follow up refactor.

@sydney-runkle sydney-runkle merged commit cd0346d into main Nov 11, 2024
29 checks passed
@sydney-runkle sydney-runkle deleted the typed-dict-discriminator-warning branch November 11, 2024 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unstable behavior for warning Failed to get discriminator value for tagged union serialization ?
2 participants