Skip to content

add union_mode='left_to_right'#7151

Merged
dmontagu merged 1 commit intomainfrom
dh/union-mode
Aug 16, 2023
Merged

add union_mode='left_to_right'#7151
dmontagu merged 1 commit intomainfrom
dh/union-mode

Conversation

@davidhewitt
Copy link
Copy Markdown
Collaborator

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-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-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 thread docs/migration.md
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
Copy Markdown
Collaborator 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
Copy Markdown
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
@dmontagu dmontagu deleted the dh/union-mode branch August 16, 2023 22:23
@stevegoss-komodo
Copy link
Copy Markdown

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

@wooparadog
Copy link
Copy Markdown

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.

4 participants