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

Union[str, Decimal] annotation always convert Decimal to str #2156

Closed
3 tasks done
zen-xu opened this issue Nov 30, 2020 · 1 comment
Closed
3 tasks done

Union[str, Decimal] annotation always convert Decimal to str #2156

zen-xu opened this issue Nov 30, 2020 · 1 comment
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@zen-xu
Copy link

zen-xu commented Nov 30, 2020

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.7.2
            pydantic compiled: True
                 install path: /home/xzy/dev/airport/.venv/lib/python3.8/site-packages/pydantic
               python version: 3.8.5 (default, Aug 18 2020, 10:14:10)  [GCC 7.4.0]
                     platform: Linux-4.19.104-microsoft-standard-x86_64-with-glibc2.27
     optional deps. installed: []
from pydantic import BaseModel
from decimal import Decimal
from typing import Union

class Model(BaseModel):
    v: Union[str, Decimal]

model = Model(v=Decimal("1.1"))
model.v   # '1.1' rather than Decimal('1.1')

# But change Union position is correct
class Model(BaseModel):
    v: Union[Decimal, str]

model = Model(v=Decimal("1.1"))
model.v # Decimal('1.1')
@zen-xu zen-xu added the bug V1 Bug related to Pydantic V1.X label Nov 30, 2020
@PrettyWood
Copy link
Member

Hi @zen-xu
This is a know issue (see #453 or #1423)
I recently opened a PR #2092 and am planning to work on this on my spare time.
Hope it helps

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