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

add union_mode='left_to_right' #7151

Merged
merged 1 commit into from Aug 16, 2023
Merged

add union_mode='left_to_right' #7151

merged 1 commit into from Aug 16, 2023

Conversation

davidhewitt
Copy link
Contributor

Change Summary

Adds a new Field option, union_mode, which can be used to alter the union validation. At present, there are only two options:

  • smart (the default), which is equivalent to the current V2 behaviour
  • left_to_right, which is just simple left-to-right validation, first success wins, like V1

This option can be used to opt out of smart union validation in cases when users want more easily understandable results.

In #7097 (comment) we suggested a dumb_union=True boolean flag. In further discussion offline I raised the idea that using a string setting here (while a touch more verbose) is more descriptive and also gives us space to add more options backwards-compatibly later.

Related issue number

Related #7097
Related #7110

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@cloudflare-pages
Copy link

cloudflare-pages bot commented Aug 16, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 84d6f6d
Status: ✅  Deploy successful!
Preview URL: https://691f9560.pydantic-docs2.pages.dev
Branch Preview URL: https://dh-union-mode.pydantic-docs2.pages.dev

View logs

@davidhewitt davidhewitt force-pushed the dh/union-mode branch 3 times, most recently from 29f26ff to 84d6f6d Compare August 16, 2023 22:07
Comment on lines +509 to +510
To revert to the non-short-circuiting left-to-right behavior of V1, annotate the union with `Field(union_mode='left_to_right')`.
See [Union Mode](./usage/types/unions.md#union-mode) for more details.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not an expert on V1's union validation, this statement may not be true. I thought it might be worth calling out this or something similar here in the migration guide.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's right

@dmontagu dmontagu merged commit 7387c8a into main Aug 16, 2023
50 checks passed
@dmontagu dmontagu deleted the dh/union-mode branch August 16, 2023 22:23
@stevegoss-komodo
Copy link

@davidhewitt Thank you! This will greatly ease our transition to Pydantic V2!

@wooparadog
Copy link

Hi, just found this gem while migrating to V2. Is there a way to make this behavior as default..?

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.

None yet

4 participants