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

Support for discriminated unions with callable discriminator #157

Open
joshvote opened this issue Jan 4, 2024 · 1 comment
Open

Support for discriminated unions with callable discriminator #157

joshvote opened this issue Jan 4, 2024 · 1 comment
Labels
enhancement New feature or request v2 Version 2 related

Comments

@joshvote
Copy link

joshvote commented Jan 4, 2024

The latest pydantic release has added support for Union discriminators that can be implemented via a custom function: https://docs.pydantic.dev/latest/concepts/unions/#discriminated-unions-with-callable-discriminator

I recently attempted to utilise this with pydantic-xml but ran into the following error raised by pydantic-xml:

E pydantic_xml.errors.ModelFieldError: .... field type error: only string discriminators are supported

I'd be willing to contribute a PR if you're able to point me in the right direction of how to add support for this to pydantic-xml

@joshvote
Copy link
Author

joshvote commented Jan 4, 2024

I had a quick poke around in the code and I'm not familiar enough the internals of pydantic to make an obvious patch (the current tagged_union.py logic assumes that the fields will all be available as literals when the model is created. However I did end up whipping up a really simple unit test that hopefully highlights the issue.

master...joshvote:pydantic-xml:master

@joshvote joshvote changed the title Support for discriminated uniones with callable discriminator Support for discriminated unions with callable discriminator Jan 4, 2024
@dapper91 dapper91 added enhancement New feature or request v2 Version 2 related labels Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request v2 Version 2 related
Projects
None yet
Development

No branches or pull requests

2 participants