This repository was archived by the owner on Apr 11, 2026. It is now read-only.
Validate default values at schema definition#1593
Open
changhc wants to merge 4 commits intopydantic:mainfrom
Open
Validate default values at schema definition#1593changhc wants to merge 4 commits intopydantic:mainfrom
changhc wants to merge 4 commits intopydantic:mainfrom
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
CodSpeed Performance ReportMerging #1593 will not alter performanceComparing Summary
|
Contributor
Author
|
please review |
|
Thank you! The problem this addresses has been a persistent thorn in our sides. Would love to see this released. |
psychedelicious
added a commit
to invoke-ai/InvokeAI
that referenced
this pull request
May 2, 2025
This prevents issues where the node is defined with an invalid default value, which would guarantee an error during a ser/de roundtrip. - Upstream issue requesting this functionality be built-in to pydantic: pydantic/pydantic#8722 - Upstream PR that implements the functionality: pydantic/pydantic-core#1593
psychedelicious
added a commit
to invoke-ai/InvokeAI
that referenced
this pull request
May 13, 2025
This prevents issues where the node is defined with an invalid default value, which would guarantee an error during a ser/de roundtrip. - Upstream issue requesting this functionality be built-in to pydantic: pydantic/pydantic#8722 - Upstream PR that implements the functionality: pydantic/pydantic-core#1593
psychedelicious
added a commit
to invoke-ai/InvokeAI
that referenced
this pull request
May 13, 2025
This prevents issues where the node is defined with an invalid default value, which would guarantee an error during a ser/de roundtrip. - Upstream issue requesting this functionality be built-in to pydantic: pydantic/pydantic#8722 - Upstream PR that implements the functionality: pydantic/pydantic-core#1593
davidhewitt
reviewed
Sep 23, 2025
Collaborator
davidhewitt
left a comment
There was a problem hiding this comment.
Thanks, this seems good to me, and I'm sorry the review took so long.
Just one thing I would like to check is the choice of bitflags, I think we can just have a plain enum.
Once this is merged we will need to document it in pydantic.
Comment on lines
+88
to
+95
| bitflags::bitflags! { | ||
| #[derive(Debug, Clone)] | ||
| struct ValidateDefaultFlag: u8 { | ||
| const NEVER = 0; | ||
| const INIT = 0x01; | ||
| const DEFINITION = 0x02; | ||
| } | ||
| } |
Collaborator
There was a problem hiding this comment.
I don't think this needs to be a bitflag, can just be an enum?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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_defaultso 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/Falsedoes not change with this PR.Note that even though
initanddefinitionare 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