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

Clarify validate_default and _Unset handling in usage docs and migration guide #6950

Merged
merged 4 commits into from Aug 14, 2023

Conversation

benbenbang
Copy link
Contributor

@benbenbang benbenbang commented Jul 30, 2023

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 the validate_default parameter in the Field function is updated to specify that its default value is None.
  • Validator usage: The documentation about the usage of validators, specifically with the default values, is updated. Added emphasis on how to mimic the always=True behavior from Pydantic v1 using validate_default=True. In migration guide: The migration guide is updated to include information about the validate_default parameter for that migrating from Pydantic v1 to v2.
  • Explain the handling of _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

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @dmontagu

@benbenbang benbenbang changed the title Clarify 'validate_default' and '_Unset' handling in usage docs and migration guide Clarify validate_default and _Unset handling in usage docs and migration guide Jul 30, 2023
@benbenbang
Copy link
Contributor Author

please review 🙏🏻

@benbenbang
Copy link
Contributor Author

Hello @dmontagu or @hramezani
Do you mind to take a look 🙏🏻

@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@hramezani
Copy link
Member

Thanks @benbenbang for creating this PR 🙏
I left some suggestions.
please update

@benbenbang
Copy link
Contributor Author

Thanks @benbenbang for creating this PR 🙏 I left some suggestions. please update

Thanks @hramezani for the review, gonna update it

@benbenbang benbenbang force-pushed the docs/improve_field_validator_docs branch from b26dc20 to 15e7142 Compare August 14, 2023 20:41
@benbenbang
Copy link
Contributor Author

@hramezani , all applied. please take a look when you get some time

@hramezani hramezani merged commit 35144d0 into pydantic:main Aug 14, 2023
48 checks passed
@benbenbang benbenbang deleted the docs/improve_field_validator_docs branch August 15, 2023 08:08
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.

Clarify "always" usage in validator migration guide and field validator doc
3 participants