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

docs(typo): Field(validate_defaults=True) should be validate_default=True #7228

Closed
1 task done
lmmx opened this issue Aug 23, 2023 · 0 comments
Closed
1 task done
Assignees
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable

Comments

@lmmx
Copy link
Contributor

lmmx commented Aug 23, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Got a bit confused about validation of defaults which I knew I'd seen, firstly I expected it to be in the section on model config (maybe an opportunity to put a link in there to the validators section?)

When I looked around some more I was led to Validation of default values¶:

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). 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.

I ran the code section as written

Field(validate_defaults=True)...

after scratching my head for a second I realised there's a typo, in the 2nd usage it changes to the correct form:

Setting validate_default to True...

Looks like a single instance

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.

Example Code

No response

Python, Pydantic & OS Version

pydantic version: 2.1.1
        pydantic-core version: 2.4.0
          pydantic-core build: profile=release pgo=true mimalloc=true
                 install path: /home/louis/miniconda3/lib/python3.10/site-packages/pydantic
               python version: 3.10.4 (main, Mar 31 2022, 08:41:55) [GCC 7.5.0]
                     platform: Linux-5.15.0-76-generic-x86_64-with-glibc2.31
     optional deps. installed: ['typing-extensions']

Selected Assignee: @hramezani

@lmmx lmmx added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Aug 23, 2023
lmmx added a commit to lmmx/pydantic that referenced this issue Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable
Projects
None yet
Development

No branches or pull requests

2 participants