8000 add `left_to_right` union mode by davidhewitt · Pull Request #889 · pydantic/pydantic-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add left_to_right union mode #889

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

Merged
merged 2 commits into from
Aug 16, 2023
Merged

add left_to_right union mode #889

merged 2 commits into from
Aug 16, 2023

Conversation

davidhewitt
Copy link
Contributor
@davidhewitt davidhewitt commented Aug 16, 2023

Change Summary

Adds a left_to_right union mode which allows for V1-style union validation, for cases when V2 smart mode is problematic.

The related issue linked below suggested a dumb_union flag but I think it's better to take mode as a string to give us space to invent new strategies backwards-compatibly if we need to.

I assume that we'll expose this in pydantic as Field(union_mode="left_to_right").

Related issue number

pydantic/pydantic#7097 (comment)

Checklist

  • Unit tests for the changes exist
  • Documentation reflects the changes where applicable
  • Pydantic tests pass with this pydantic-core (except for expected changes)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @samuelcolvin

@@ -2473,6 +2474,7 @@ def union_schema(
custom_error_type: str | None = None,
custom_error_message: str | None = None,
custom_error_context: dict[str, str | int] | None = None,
mode: Literal['smart', 'left_to_right'] | None = None,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would this be better if it was called union_mode in the schema as well, or does it not matter if it differs from Field(union_mode="...")?

Copy link
Collaborator
@dmontagu dmontagu Aug 16, 2023

Choose a reason for hiding this comment

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

I think it doesn't matter, I'm okay either way, would lean toward the way you've done it here though unless the other way meaningfully simplified things on the pydantic side, which I'm not expecting it to do.

@codecov
Copy link
codecov bot commented Aug 16, 2023

Codecov Report

Merging #889 (27ccd5a) into main (2e59f25) will decrease coverage by 0.05%.
Report is 1 commits behind head on main.
The diff coverage is 98.66%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #889      +/-   ##
==========================================
- Coverage   93.79%   93.74%   -0.05%     
==========================================
  Files         105      105              
  Lines       15252    15333      +81     
  Branches       25       25              
==========================================
+ Hits        14305    14374      +69     
- Misses        941      953      +12     
  Partials        6        6              
Files Changed Coverage Δ
src/validators/union.rs 89.29% <98.42%> (+2.15%) ⬆️
python/pydantic_core/core_schema.py 96.77% <100.00%> (+<0.01%) ⬆️
src/validators/dataclass.rs 98.21% <100.00%> (-0.02%) ⬇️
src/validators/model.rs 97.78% <100.00%> (-0.03%) ⬇️
src/validators/validation_state.rs 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2e59f25...27ccd5a. Read the comment docs.

@codspeed-hq
Copy link
codspeed-hq bot commented Aug 16, 2023

CodSpeed Performance Report

Merging #889 will degrade performances by 12.15%

Comparing dh/union-mode (27ccd5a) with main (2e59f25)

Summary

🔥 2 improvements
❌ 1 regressions
✅ 135 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main dh/union-mode Change
🔥 test_complete_core_lax 1.4 ms 1.2 ms +13.34%
🔥 test_strict_union_core 25 µs 22.3 µs +12.36%
test_generator_rust 33.9 µs 38.6 µs -12.15%

@davidhewitt
Copy link
Contributor Author

please review

Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
@davidhewitt davidhewitt enabled auto-merge (squash) August 16, 2023 13:34
@davidhewitt davidhewitt merged commit 1d8a253 into main Aug 16, 2023
@davidhewitt davidhewitt deleted the dh/union-mode branch August 16, 2023 14:02
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.

3 participants
0