You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
fromtypingimportOptionalfrompydanticimportBaseModel, ValidationInfo, Field, field_validatorclassTestModel(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')@classmethoddefvalidate_list(cls,
value: list[int|tuple[int]]) ->list[int|tuple[int]]:
""" THIS SHOULD GO FIRST. """returnvalue@field_validator('my_dict', mode='before')@classmethoddefvalidate_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 HEREprint(print_me)
ifvalueisNone:
return {}
else:
returnvalue# This works; print shows successfullytest_model=TestModel(my_list=[1, 2])
# This DOESN'T work; KeyErrortest_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
The text was updated successfully, but these errors were encountered:
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
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
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
The text was updated successfully, but these errors were encountered: