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

cdeil picture cdeil  路  3Comments

bartekbrak picture bartekbrak  路  3Comments

drpoggi picture drpoggi  路  3Comments

dmontagu picture dmontagu  路  3Comments

dconathan picture dconathan  路  3Comments