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

Optional field with validate_default only performs one field validation #7715

Closed
1 task done
reritom opened this issue Oct 1, 2023 · 3 comments · Fixed by #7723
Closed
1 task done

Optional field with validate_default only performs one field validation #7715

reritom opened this issue Oct 1, 2023 · 3 comments · Fixed by #7723
Assignees
Labels
bug V2 Bug related to Pydantic V2

Comments

@reritom
Copy link

reritom commented Oct 1, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

When validating a class that has field_validators for optional fields that have validate_default=True, only one error gets returned based on the first field_validator. None of the basic validation errors get returned (like missing), and no sequential field_validators get called.

Given a model with optional fields with default values, validation of defaults, and field validators:

class ExampleKO(BaseModel):
    value_0: str
    value_a: Annotated[Optional[str], Field(None, validate_default=True)]
    value_b: Annotated[Optional[str], Field(None, validate_default=True)]

    @field_validator("value_a", mode="after")
    def value_a_validator(cls, value):
        raise AssertionError

    @field_validator("value_b", mode="after")
    def value_b_validator(cls, value):
        raise AssertionError

When called with no arguments we only get 1 validation error. The first field_validator.

ExampleKO()

"""
pydantic_core._pydantic_core.ValidationError: 1 validation error for ExampleKO
value_a
  Assertion failed,  [type=assertion_error, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.4/v/assertion_error
"""

I would expect the above to behave the same as if I passed two None values for those optional fields:

ExampleKO(value_a=None, value_b=None)

"""
value_0
  Field required [type=missing, input_value={'value_a': None, 'value_b': None}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.4/v/missing
value_a
  Assertion failed,  [type=assertion_error, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.4/v/assertion_error
value_b
  Assertion failed,  [type=assertion_error, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.4/v/assertion_error
"""

Example Code

from typing import Annotated, Optional

from pydantic import BaseModel, Field, field_validator

class ExampleKO(BaseModel):
    value_0: str
    value_a: Annotated[Optional[str], Field(None, validate_default=True)]
    value_b: Annotated[Optional[str], Field(None, validate_default=True)]

    @field_validator("value_a", mode="after")
    def value_a_validator(cls, value):
        raise AssertionError

    @field_validator("value_b", mode="after")
    def value_b_validator(cls, value):
        raise AssertionError


ExampleKO()

"""
pydantic_core._pydantic_core.ValidationError: 1 validation error for V2ExampleKO
value_a
  Assertion failed,  [type=assertion_error, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.4/v/assertion_error
"""

Python, Pydantic & OS Version

pydantic version: 2.4.2
        pydantic-core version: 2.10.1
          pydantic-core build: profile=release pgo=false
                 install path: /Users/me/Documents/Projects/pydanticbug/.venv2/lib/python3.9/site-packages/pydantic
               python version: 3.9.12 (v3.9.12:b28265d7e6, Mar 23 2022, 18:22:40)  [Clang 13.0.0 (clang-1300.0.29.30)]
                     platform: macOS-13.5-arm64-arm-64bit
             related packages: typing_extensions-4.8.0
@reritom reritom added bug V2 Bug related to Pydantic V2 pending Is unconfirmed labels Oct 1, 2023
@sydney-runkle sydney-runkle self-assigned this Oct 2, 2023
@sydney-runkle sydney-runkle removed the pending Is unconfirmed label Oct 2, 2023
@sydney-runkle
Copy link
Member

Hi @reritom,

Thanks for reporting this. This is a bug in our validation error handling in pydantic-core, and we're working on a fix. Stay tuned 👍.

@reritom
Copy link
Author

reritom commented Oct 2, 2023

@sydney-runkle is there an issue on pydantic-core that I can follow? I didn't see one regarding this when I checked. Thanks

@sydney-runkle
Copy link
Member

Hi @reritom,

We don't have an issue there - your filing of this issue alerted us to this bug. I'll have a PR up shortly in both pydantic-core and pydantic working on this fix.

Thanks again for bringing this issue to our attention 😄

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants