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

Validation of fields in tagged unions creates confusing errors #3113

Closed
3 tasks done
ojii opened this issue Aug 18, 2021 · 0 comments · Fixed by #2336
Closed
3 tasks done

Validation of fields in tagged unions creates confusing errors #3113

ojii opened this issue Aug 18, 2021 · 0 comments · Fixed by #2336
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@ojii
Copy link
Contributor

ojii commented Aug 18, 2021

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.8.2
            pydantic compiled: False
                 install path: /Users/jonas.obrist/playground/pydantic/pydantic
               python version: 3.8.3 (default, Jul 17 2020, 11:57:37)  [Clang 10.0.0 ]
                     platform: macOS-10.16-x86_64-i386-64bit
     optional deps. installed: ['devtools', 'dotenv', 'email-validator', 'typing-extensions']

Tested with git checkout 5ccbdcb.

See this test in my branch.

from typing import Literal, Union

from pydantic import BaseModel, conint


class A(BaseModel):
    tag: Literal["a"]


class B(BaseModel):
    tag: Literal["b"]
    x: conint(ge=0, le=1)


class Model(BaseModel):
    value: Union[A, B]

Model(value={"tag": "b", "x": 2}) # this will fail with "tag must be 'a'" and "x" must be LE 1

I would expect the error to be just "'x' needs to be LE 1", but the error also complains that "'tag' must be 'a'".

This is somewhat related to #619

@ojii ojii added the bug V1 Bug related to Pydantic V1.X label Aug 18, 2021
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