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

default_factory is called with missing validated_data if validation of prior field fails #11358

Open
1 task done
hassec opened this issue Jan 29, 2025 · 4 comments · May be fixed by pydantic/pydantic-core#1623
Open
1 task done
Assignees
Labels
bug V2 Bug related to Pydantic V2
Milestone

Comments

@hassec
Copy link

hassec commented Jan 29, 2025

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

The below code will crash with the following error.

Traceback (most recent call last):
  File "...", line 9, in <module>
    model = Model(a="abc")
            ^^^^^^^^^^^^^^
  File "/.../.venv/lib/python3.12/site-packages/pydantic/main.py", line 214, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../test.py", line 6, in <lambda>
    b: int = Field(default_factory=lambda data: 2 * data["a"])
                                                    ~~~~^^^^^
KeyError: 'a'

I think the problem is that the error from the failed validation of a: int = "abc" isn't immediately raised, and then the call to the default_factory is done with a dictionary that doesn't have the expected keys.

The resulting error is then not really helpful to a user as the KeyError is just a symptom when the actual cause would be the ValidationError that now isn't reported.

Example Code

from pydantic import BaseModel, Field


class Model(BaseModel):
    a: int = 1
    b: int = Field(default_factory=lambda data: 2 * data["a"])


model = Model(a="abc")

Python, Pydantic & OS Version

pydantic version: 2.10.3
        pydantic-core version: 2.27.1
          pydantic-core build: profile=release pgo=false
                 install path: /Users/chasse/.../.venv/lib/python3.12/site-packages/pydantic
               python version: 3.12.6 (main, Sep  9 2024, 21:36:32) [Clang 18.1.8 ]
                     platform: macOS-15.3-arm64-arm-64bit
             related packages: typing_extensions-4.12.2
                       commit: unknown
@hassec hassec added bug V2 Bug related to Pydantic V2 pending Is unconfirmed labels Jan 29, 2025
@Viicos
Copy link
Member

Viicos commented Feb 3, 2025

Indeed this is confusing. I think it's best to directly raise a validation error if the default factory is called (and it takes the data argument) and if at least one validation of a previous field failed.

@Viicos Viicos removed the pending Is unconfirmed label Feb 3, 2025
@osamimi
Copy link

osamimi commented Feb 5, 2025

Ah, I should've looked here before spending hours trying to debug this. I thought it had to do with the order the fields / validators are defined.

@jaklan
Copy link

jaklan commented Mar 16, 2025

Very strong bump, I just deal with the same issue - keeping fingers crossed for a quick fix in 2.11

@Viicos
Copy link
Member

Viicos commented Mar 17, 2025

If this doesn't make it into 2.11.0, we will still be able to include it in a patch release as a backport. Self note: this should also be fixed for field validators accessing the data.

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
4 participants