Pydantic: How to use `Union` without type conversion?

Created on 24 Apr 2020  路  1Comment  路  Source: samuelcolvin/pydantic

Question

How to use Union without type conversion?

import pydantic
from typings import Union

class Foo(pydantic.BaseModel):
  foo: Union[int, float]

assert isinstance(Foo(foo=1.1), float), "will throw an error"

I know that I can flip Union[float, int], but now all foo will always be floats.

question

>All comments

There's no way currently, you need to wait until #619 or the change I just suggested on #1423.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

demospace picture demospace  路  26Comments

dmfigol picture dmfigol  路  38Comments

koxudaxi picture koxudaxi  路  25Comments

chopraaa picture chopraaa  路  18Comments

rrbarbosa picture rrbarbosa  路  35Comments