class TestModel(BaseXmlModel, tag='model'):
field1: SubModel1 | SubModel2 = element()
this will fail when .from_xml() is called (and in testing my project, when to_xml() is called also) with
> ???
E pydantic.error_wrappers.ValidationError: 1 validation error for TestModel
E field1
E field required (type=value_error.missing)
pydantic/main.py:341: ValidationError
Currently, the
Model1 | Model2
Python 3.10 syntax of a union is not recognized between models by pydantic-xml.This can be replicated pretty easily by changing the
TestModel
class here to use them: https://github.com/dapper91/pydantic-xml/blob/68ce0480c1c691d1742f479b919872bd4dba730d/tests/test_unions.py#L32-L40 as in:this will fail when
.from_xml()
is called (and in testing my project, whento_xml()
is called also) withThe cause for this is this function: https://github.com/dapper91/pydantic-xml/blob/68ce0480c1c691d1742f479b919872bd4dba730d/pydantic_xml/serializers/serializer.py#L69-L70
Unions denoted by the
|
in Python 3.10 are actuallytypes.UnionType
rather thantyping.Union
.typing.get_origin()
actually returns it specifically:https://github.com/python/cpython/blob/a5d2b546c1b0b73d0695b98838a3ddd497382999/Lib/typing.py#L2412-L2413
The fix is just adding
UnionType
to theis_union()
function.