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

Validators for optional dataclass fields are called twice #899

Closed
bochecha opened this issue Oct 15, 2019 · 2 comments
Closed

Validators for optional dataclass fields are called twice #899

bochecha opened this issue Oct 15, 2019 · 2 comments
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@bochecha
Copy link

Bug

Please complete:

  • OS: Ubuntu 19.04 and Fedora 30
  • 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:

from typing import Optional

from pydantic.class_validators import validator
from pydantic.dataclasses import dataclass


@dataclass
class Dataclass:
    field1: int
    field2: Optional[int] = None

    @validator('field1', pre=True)
    def get_field1(cls, value: int) -> int:
        print(f'get_field1({value})')

        if not isinstance(value, int):
            raise ValueError(f'{value} is not an int')

        return value

    @validator('field2', pre=True)
    def get_field2(cls, value: Optional[int]) -> Optional[int]:
        print(f'get_field2({value})')

        if not isinstance(value, int):
            raise ValueError(f'{value} is not an int')

        return value


Dataclass(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.

@bochecha bochecha added the bug V1 Bug related to Pydantic V1.X label Oct 15, 2019
@samuelcolvin
Copy link
Member

This is a duplicate of #737, but it should also be fixed in v1 which will be released soon.

The best solution for now is to use whole=True in the validator.

@bochecha
Copy link
Author

This is a duplicate of #737, but it should also be fixed in v1 which will be released soon.

The best solution for now is to use whole=True in the validator.

Thanks, I'm using whole=True for now.

I see you've released 1.0 in the meantime, is that now fixed? (#737 was already closed when you marked this as a duplicate so it's hard to tell)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

No branches or pull requests

2 participants