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

Wrong assignement of values in Union[Foo, List[Foo]] #3365

Closed
3 tasks done
armingeiser opened this issue Oct 27, 2021 · 3 comments · Fixed by #2092
Closed
3 tasks done

Wrong assignement of values in Union[Foo, List[Foo]] #3365

armingeiser opened this issue Oct 27, 2021 · 3 comments · Fixed by #2092
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@armingeiser
Copy link

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

When validating a model that allows either a single object Foo or a list of Foo objects, the wrong values are assigned. The key of the second parameter is assigned as value of the first parameter (which is a string).

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

             pydantic version: 1.8.2
            pydantic compiled: True
                 install path: /home/anaconda3/envs/py38/lib/python3.8/site-packages/pydantic
               python version: 3.8.5 (default, Sep  4 2020, 07:30:14)  [GCC 7.3.0]
                     platform: Linux-5.11.0-38-generic-x86_64-with-glibc2.10
     optional deps. installed: ['typing-extensions']

The following minimal example repruduces the bug:

from pydantic import BaseModel, Field
from typing import List, Optional, Union


class Foo(BaseModel):
    a: str = Field(
        ...,
    )
    b: Optional[float]


class Bar(BaseModel):
    foo: Union[Foo, List[Foo]]


bar = {
    "foo": [
        {
            "a": "some string",
            "b": 1.0
        },
        {
            "a": "another string",
            "b": 2.0
        }
    ]
}

a = Bar(**bar)

print(a)
  • Output: foo=Foo(a='b', b=None)
  • Expected Output: foo=[Foo(a='some string', b=1.0), Foo(a='another string', b=2.0)]

I am not sure if this is related somehow to #2092

@armingeiser armingeiser added the bug V1 Bug related to Pydantic V1.X label Oct 27, 2021
@vigneshmanick
Copy link

can confirm. Have the same issue.

@PrettyWood
Copy link
Member

I am not sure if this is related somehow to #2092

@armingeiser It is 😉

With #2092 code and

class Bar(BaseModel, smart_union=True):
    foo: Union[Foo, List[Foo]]

you get

foo=[Foo(a='some string', b=1.0), Foo(a='another string', b=2.0)]

@armingeiser
Copy link
Author

@PrettyWood awesome, thanks for the quick reply. Looking forward to that MR being merged.

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.

3 participants