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
Python version import sys; print(sys.version): 3.7.3 and 3.7.4
Pydantic version import pydantic; print(pydantic.VERSION): 0.32.2
The following code snippet is a simplified version of my code, to illustrate the problem:
fromtypingimportOptionalfrompydantic.class_validatorsimportvalidatorfrompydantic.dataclassesimportdataclass@dataclassclassDataclass:
field1: intfield2: Optional[int] =None@validator('field1', pre=True)defget_field1(cls, value: int) ->int:
print(f'get_field1({value})')
ifnotisinstance(value, int):
raiseValueError(f'{value} is not an int')
returnvalue@validator('field2', pre=True)defget_field2(cls, value: Optional[int]) ->Optional[int]:
print(f'get_field2({value})')
ifnotisinstance(value, int):
raiseValueError(f'{value} is not an int')
returnvalueDataclass(field1="foo", field2="bar")
Running it gives the following:
get_field1(foo)
get_field2(bar)
get_field2(bar)
Traceback (most recent call last):
File "reproducer.py", line 31, in <module>
Dataclass(field1="foo", field2="bar")
File "<string>", line 4, in __init__
File "pydantic/dataclasses.py", line 75, in pydantic.dataclasses._process_class._pydantic_post_init
# __delattr__
File "pydantic/main.py", line 785, in pydantic.main.validate_model
pydantic.error_wrappers.ValidationError: 3 validation errors for Dataclass
field1
foo is not an int (type=value_error)
field2
bar is not an int (type=value_error)
field2
bar is not an int (type=value_error)
As you can see the get_field2 validator is called twice. This happens whenever the field is Optional.
This results in 2 identical errors being shown, which is very confusing.
The text was updated successfully, but these errors were encountered:
Bug
Please complete:
import sys; print(sys.version)
: 3.7.3 and 3.7.4import pydantic; print(pydantic.VERSION)
: 0.32.2The following code snippet is a simplified version of my code, to illustrate the problem:
Running it gives the following:
As you can see the
get_field2
validator is called twice. This happens whenever the field isOptional
.This results in 2 identical errors being shown, which is very confusing.
The text was updated successfully, but these errors were encountered: