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

duplicated errors when validators raise ValidationError #737

Closed
samuelcolvin opened this issue Aug 11, 2019 · 1 comment · Fixed by #738
Closed

duplicated errors when validators raise ValidationError #737

samuelcolvin opened this issue Aug 11, 2019 · 1 comment · Fixed by #738
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@samuelcolvin
Copy link
Member

Bug

As a work around for #619 I tried the following

from pydantic import VERSION, BaseModel, Union, validator
from typing_extensions import Literal
print('pydantic version:', VERSION)

class Foo(BaseModel):
    model_type: Literal['foo']
    f: int

class Bar(BaseModel):
    model_type: Literal['bar']
    b: int

class MyModel(BaseModel):
    foobar: Union[Foo, Bar]

    @validator('foobar', pre=True)
    def check_action(cls, v):
        if isinstance(v, dict):
            model_type = v.get('model_type')
            if model_type == 'foo':
                return Foo(**v)
            if model_type == 'var':
                return Bar(**v)
        return v

MyModel(foobar={'model_type': 'foo', 'f': 'x'})

Output:

pydantic version: 0.32.1
Traceback (most recent call last):
  File "test.py", line 31, in <module>
    MyModel(foobar={'model_type': 'foo', 'f': 'x'})
  File "pydantic/main.py", line 275, in pydantic.main.BaseModel.__init__
  File "pydantic/main.py", line 785, in pydantic.main.validate_model
pydantic.error_wrappers.ValidationError: 2 validation errors for MyModel
foobar -> f
  value is not a valid integer (type=type_error.integer)
foobar -> f
  value is not a valid integer (type=type_error.integer)

When validators raise ValidationError the errors are duplicated.

Won't be that common, but should be fixed.

@samuelcolvin samuelcolvin added the bug V1 Bug related to Pydantic V1.X label Aug 11, 2019
@samuelcolvin
Copy link
Member Author

I was being dumb, just that I didn't have whole=True, I've added a test to demonstrate this.

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

Successfully merging a pull request may close this issue.

1 participant