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

Expose ability to use a callable as the discriminator in a tagged union #6915

Closed
wants to merge 6 commits into from

Conversation

dmontagu
Copy link
Contributor

@dmontagu dmontagu commented Jul 27, 2023

Still needs documentation, but I wanted to open this and get feedback.

(I started working on this because of #6040, where I noticed some errors that could maybe be cleaned up by this. But I've been thinking about it for a while, and there have been other cases where it was even more well-suited.)

As demonstrated by the tests, this can improve errors for many cases today, especially those involving recursive types with unions in the recursion.

I have not made Field(discriminator=CallableDiscriminator(...)) work, mostly because the current logic for applying discriminators from Field is pretty nested/complex and doesn't have access to the GenerateSchema instance, but I also think it can be partially justified as — the Field discriminator affects JSON schema, whereas the CallableDiscriminator annotation won't affect the JSON schema.

Alternatively, I think it could be justified as an initial implementation, and if it gets traction and people like it, we could make it work in Field(...) in the future. (Or, if necessary, we could just get that implemented, I just wanted to keep the changes smaller if possible.)

Selected Reviewer: @Kludex

@dmontagu
Copy link
Contributor Author

please review

@cloudflare-pages
Copy link

cloudflare-pages bot commented Jul 28, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 30cb7b5
Status: ✅  Deploy successful!
Preview URL: https://7ebbb69d.pydantic-docs2.pages.dev
Branch Preview URL: https://expose-callable-discriminato.pydantic-docs2.pages.dev

View logs

@sydney-runkle
Copy link
Member

Closing, as we've decided to implement this behavior via #7983 instead

@samuelcolvin samuelcolvin deleted the expose-callable-discriminator branch November 14, 2023 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants