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

Validate default values at schema definition #1593

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

changhc
Copy link
Contributor

@changhc changhc commented Dec 23, 2024

Change Summary

Extended option validate_default so that it's possible to validate default values when a model schema is being built.

This option now can be a boolean or 3 possible string values: 'never', 'definition', or 'init'.

  • never: no validation for default values at all. This is the same as False.
  • init: validation at model instantiation. This is the same as True.
  • definition: validation at model definition.

The behaviour of True/False does not change with this PR.

Note that even though init and definition are not mutually exclusive and I implemented these as flags that can co-exist, I decided not to add another option, e.g. always, that cover both flags because if a validation is raised at model definition, it's pretty much impossible to trigger such error at model instantiation because the model does not built at all and therefore we don't really need always.

Related issue number

Part of pydantic/pydantic#8722

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: @davidhewitt

Copy link

codecov bot commented Dec 23, 2024

Codecov Report

Attention: Patch coverage is 89.47368% with 8 lines in your changes missing coverage. Please review.

Project coverage is 89.60%. Comparing base (ab503cb) to head (1a82948).
Report is 264 commits behind head on main.

Files with missing lines Patch % Lines
src/validators/with_default.rs 89.18% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1593      +/-   ##
==========================================
- Coverage   90.21%   89.60%   -0.62%     
==========================================
  Files         106      112       +6     
  Lines       16339    17909    +1570     
  Branches       36       40       +4     
==========================================
+ Hits        14740    16047    +1307     
- Misses       1592     1842     +250     
- Partials        7       20      +13     
Files with missing lines Coverage Δ
python/pydantic_core/core_schema.py 94.86% <100.00%> (+0.09%) ⬆️
src/validators/with_default.rs 93.47% <89.18%> (-5.60%) ⬇️

... and 62 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 562fad3...1a82948. Read the comment docs.

Copy link

codspeed-hq bot commented Dec 23, 2024

CodSpeed Performance Report

Merging #1593 will not alter performance

Comparing changhc:8722-validate-default (1a82948) with main (562fad3)

Summary

✅ 157 untouched benchmarks

@changhc changhc marked this pull request as ready for review December 23, 2024 23:26
@changhc
Copy link
Contributor Author

changhc commented Dec 23, 2024

please review

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.

2 participants