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

Include generics in discriminated union schemas #157

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

tpoliaw
Copy link
Contributor

@tpoliaw tpoliaw commented Jan 31, 2025

  • Restrict subclasses of typed unions
  • Allow arbitrary types to be included in 'StrictConfig' types

Fixes #156

Instructions to reviewer on how to test:

  1. Create set of subclasses of a decorated base class:
@discriminated_union_of_subclasses
class Base(Generic[T]):
    pass

@dataclass
class Child(Base[T]):
    foo: T
  1. Try and deserialise a serialised Child as a Base:
one = TypeAdapter(Base[int]).validate_python({"type": "Child", "foo": "42"})
assert one.foo == 42 # <== 42 instead of "42" as type should be converted

Checks for reviewer

  • Would the PR title make sense to a user on a set of release notes

Without allowing arbitrary types, instances of Spec such as
`Line[Movable]` cannot be validated as Movable cannot be serialised.
Copy link
Contributor

@coretl coretl left a comment

Choose a reason for hiding this comment

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

From a skim this looks fine to me

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.

discriminated_union_of_subclasses loses type information when deserialising
2 participants