Validate default values at schema definition #1593
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 asFalse
.init
: validation at model instantiation. This is the same asTrue
.definition
: validation at model definition.The behaviour of
True
/False
does not change with this PR.Note that even though
init
anddefinition
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 needalways
.Related issue number
Part of pydantic/pydantic#8722
Checklist
pydantic-core
(except for expected changes)Selected Reviewer: @davidhewitt