Closed maurosilber closed 9 months ago
@maurosilber Hi.
The order of the types in the union is respected except the case when the input type matches one of the union types. I agree that is not very intuitive but that is how pydantic
itself works. Here you can read more about that.
I will mention that in the documentation, thanks.
To fix you problem pass left_to_right
union_mode to the w
field:
class Model(BaseXmlModel):
x: float | bool = attr()
y: bool | float = attr()
z: str | float = attr()
w: float | str = attr(union_mode='left_to_right')
Thank you!
I was not aware of that behaviour, as I'm not quite familiar with pydantic
.
I've opened a PR updating the docs.
According to the docs, the order of the types in the union is respected. But when using
str
, it seems that it always wins:Output:
Expected: