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

Field Validator Order Changes with Lists of Tuples #9280

Open
aleewen opened this issue Apr 18, 2024 · 2 comments
Open

Field Validator Order Changes with Lists of Tuples #9280

aleewen opened this issue Apr 18, 2024 · 2 comments
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@aleewen
Copy link

aleewen commented Apr 18, 2024

Initial Checks
I confirm that I'm using Pydantic V2
Description
Hi all,

I have a field that is a list of some data type or a tuple. When passing in that data type, it works fine. However, when passing in a list of tuples, it's almost like the validation order changes; the ValidationInfo dictionary key is unset, resulting in a KeyError.

Example Code

from typing import Optional
from pydantic import BaseModel, ValidationInfo, Field, field_validator


class TestModel(BaseModel):
    
    my_list: list[int | tuple[int]]
    my_dict: dict[int | tuple[int], str] = Field(default=None, validate_default=True)
    
    @field_validator('my_list', mode='before')
    @classmethod
    def validate_list(cls,
                      value: list[int | tuple[int]]) -> list[int | tuple[int]]:
        """
        THIS SHOULD GO FIRST.
        """
        return value

    @field_validator('my_dict', mode='before')
    @classmethod
    def validate_dict(cls,
                      value: Optional[dict[int | tuple[int], str]],
                      instance: ValidationInfo) -> dict[int | tuple[int], str]:
        """
        THIS SHOULD GO SECOND.
        """
        print_me = instance.data['my_list'] # KEY ERROR HERE
        print(print_me)
        
        if value is None:
            return {}
        else:
            return value
    


# This works; print shows successfully
test_model = TestModel(my_list=[1, 2])

# This DOESN'T work; KeyError
test_model = TestModel(my_list=[(1, 2)])

Python, Pydantic & OS Version
pydantic version: 2.7.0
pydantic-core version: 2.18.1
pydantic-core build: profile=release pgo=true
install path: C:\Users\aleewen.pyenv\pyenv-win\versions\3.11.8\Lib\site-packages\pydantic
python version: 3.11.8 (tags/v3.11.8:db85d51, Feb 6 2024, 22:03:32) [MSC v.1937 64 bit (AMD64)]
platform: Windows-10-10.0.19045-SP0
related packages: pydantic-settings-2.2.1 typing_extensions-4.10.0
commit: unknown

@aleewen aleewen added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Apr 18, 2024
@aleewen
Copy link
Author

aleewen commented Apr 18, 2024

(Sorry, I tried to edit it but ended up messing up the formatting)

@aleewen
Copy link
Author

aleewen commented Apr 24, 2024

Learning now that this issue goes away when specifying flexible length with ellipses: tuple[int, ...]

But will leave this open because I doubt that this is intended behavior: to change the validation order and raise a KeyError when the length of the provided tuple does not match the one required by the field. I would assume it would be more appropriate to raise a ValidationError

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 pending Awaiting a response / confirmation
Projects
None yet
Development

No branches or pull requests

1 participant