Clarify validate_default and _Unset handling in usage docs and migration guide#6950
Merged
hramezani merged 4 commits intopydantic:mainfrom Aug 14, 2023
benbenbang:docs/improve_field_validator_docs
Merged
Clarify validate_default and _Unset handling in usage docs and migration guide#6950hramezani merged 4 commits intopydantic:mainfrom benbenbang:docs/improve_field_validator_docs
hramezani merged 4 commits intopydantic:mainfrom
benbenbang:docs/improve_field_validator_docs
Conversation
1 task
Contributor
Author
|
please review 🙏🏻 |
Contributor
Author
|
Hello @dmontagu or @hramezani |
hramezani
reviewed
Aug 14, 2023
| Validators won't run when the default value is used. | ||
| This applies both to `@field_validator` validators and `Annotated` validators. | ||
| You can force them to run with `Field(validate_defaults=True)`, but you are generally better off using a `@model_validator(mode='before')`. | ||
| You can force them to run with `Field(validate_defaults=True)`. Setting `validate_default` to `True` has the closest behavior to using `always=True` in `validator` in pydantic v1. However, you are generally better off using a `@model_validator(mode='before')` where the function is called before the inner validator is called. |
Member
There was a problem hiding this comment.
Suggested change
| You can force them to run with `Field(validate_defaults=True)`. Setting `validate_default` to `True` has the closest behavior to using `always=True` in `validator` in pydantic v1. However, you are generally better off using a `@model_validator(mode='before')` where the function is called before the inner validator is called. | |
| You can force them to run with `Field(validate_defaults=True)`. Setting `validate_default` to `True` has the closest behavior to using `always=True` in `validator` in Pydantic V1. However, you are generally better off using a `@model_validator(mode='before')` where the function is called before the inner validator is called. |
hramezani
reviewed
Aug 14, 2023
| example, despite the fact that the validator below will never error, the following code raises a `ValidationError`: | ||
|
|
||
| !!! note | ||
| To avoid this, you can use the `validate_default` argument in the `Field` function. When set to `True`, it mimics the behavior of `always=True` in pydantic v1. However, the new way of using `validate_default` is encouraged as it provides more flexibility and control. |
Member
There was a problem hiding this comment.
Suggested change
| To avoid this, you can use the `validate_default` argument in the `Field` function. When set to `True`, it mimics the behavior of `always=True` in pydantic v1. However, the new way of using `validate_default` is encouraged as it provides more flexibility and control. | |
| To avoid this, you can use the `validate_default` argument in the `Field` function. When set to `True`, it mimics the behavior of `always=True` in Pydantic V1. However, the new way of using `validate_default` is encouraged as it provides more flexibility and control. |
Member
|
Thanks @benbenbang for creating this PR 🙏 |
Contributor
Author
Thanks @hramezani for the review, gonna update it |
Contributor
Author
|
@hramezani , all applied. please take a look when you get some time |
hramezani
approved these changes
Aug 14, 2023
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Clarify 'validate_default' and '_Unset' handling in usage docs and migration guide.
Description:
This PR aims to improve the clarity and understanding of Pydantic v2 documentation in the following ways:
validate_defaultdefault value clarification: The documentation for thevalidate_defaultparameter in the Field function is updated to specify that its default value isNone.Validatorusage: The documentation about the usage of validators, specifically with the default values, is updated. Added emphasis on how to mimic thealways=Truebehavior from Pydantic v1 usingvalidate_default=True. In migration guide: The migration guide is updated to include information about thevalidate_defaultparameter for that migrating from Pydantic v1 to v2._Unsetobjects: Added a note to the documentation to clarify that_Unsetobjects will be replaced by the corresponding value defined in the_DefaultValuesdictionary or default to None if no specific value is provided.These updates aim to make it easier for users to understand the changes in Pydantic v2 and how to effectively use the Field function and validators.
Related issue number
fix #6812
Checklist
Selected Reviewer: @dmontagu