-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Clarify validate_default and _Unset handling in usage docs and migration guide #6950
Clarify validate_default and _Unset handling in usage docs and migration guide #6950
Conversation
please review 🙏🏻 |
Hello @dmontagu or @hramezani |
docs/usage/validators.md
Outdated
@@ -266,7 +266,8 @@ print(context['logs']) | |||
|
|||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
docs/migration.md
Outdated
@@ -238,6 +238,10 @@ See [Model Config](usage/model_config.md) for more details. | |||
for the annotated type will _also_ be applied even to defaults, not just the custom validators. For | |||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
Thanks @benbenbang for creating this PR 🙏 |
Thanks @hramezani for the review, gonna update it |
@hramezani , all applied. please take a look when you get some time |
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_default
default value clarification: The documentation for thevalidate_default
parameter in the Field function is updated to specify that its default value isNone
.Validator
usage: The documentation about the usage of validators, specifically with the default values, is updated. Added emphasis on how to mimic thealways=True
behavior from Pydantic v1 usingvalidate_default=True
. In migration guide: The migration guide is updated to include information about thevalidate_default
parameter for that migrating from Pydantic v1 to v2._Unset
objects: Added a note to the documentation to clarify that_Unset
objects will be replaced by the corresponding value defined in the_DefaultValues
dictionary 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