I am using pydantic python package in FastAPI for a webapp, and I noticed there are some inconsistent float-int conversion with different typing checks. For example:
class model(BaseModel):
data: Optional[Union[int, float]] = None
m = model(data=3.33)
m.data --> 3.33
class model(BaseModel):
data: Optional[Union[int, float, str]] = None
m = model(data=3.33)
m.data --> 3
class model(BaseModel):
data: Union[int, float, str] = None
m = model(data=3.33)
m.data --> 3
class model(BaseModel):
data: Union[str, int, float] = None
m = model(data=3.33)
m.data --> '3.33'
As shown here, different orders/combinations of typings have different behaviors.
I checked out thread https://github.com/samuelcolvin/pydantic/issues/360, and https://github.com/samuelcolvin/pydantic/issues/284, but they seem not to be the exact same problem.
What causes such behavior under the hood? Is there a specific reason for this? Or did I do anything wrong/inappropriate here?
I'm using python 3.8, pydantic 1.8.2
Thank you for helping!