You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:pydantic-xml/tests/test_unions.py
Lines 32 to 40 in 68ce048
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:
pydantic-xml/pydantic_xml/serializers/serializer.py
Lines 69 to 70 in 68ce048
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.The text was updated successfully, but these errors were encountered: