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

infer_discriminator #9290

Open
3 of 13 tasks
samuelcolvin opened this issue Apr 19, 2024 · 3 comments
Open
3 of 13 tasks

infer_discriminator #9290

samuelcolvin opened this issue Apr 19, 2024 · 3 comments

Comments

@samuelcolvin
Copy link
Member

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

We should have a config setting which causes Pydantic to try to infer union discriminators.

It's shouldn't try to do anything too clever, it should just look for:

  • fields on all members of the union with the same name
  • where all fields are literals or enums
  • and none of the allowed values overlap

If so, generate a discriminator.

Affected Components

@dmontagu
Copy link
Contributor

If necessary for the use cases you have in mind, I think we could also do this with functional discriminators in the case where there is a required field that is only present in one of the cases. If inferring for literals/enum discriminators is enough then that's probably less effort

@samuelcolvin
Copy link
Member Author

I guess we make the setting:

  • False - don't infer, current behaviour
  • simple - literals only
  • exhaustive - try all the things we can reasonably think of to create a discriminator

Since this method will be on GenerateSchema , any more complex semantics can be implemented by uses.

@NeevCohen
Copy link
Contributor

@samuelcolvin Hey, I'd like to pick this up if it's still available

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants