Skip to content

Commit 51efb09

Browse files
authored
Merge pull request #59 from dapper91/dev
- piped union typehints support added. See #56
2 parents 68ce048 + f6580e6 commit 51efb09

File tree

5 files changed

+74
-3
lines changed

5 files changed

+74
-3
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
0.6.2 (2023-06-10)
5+
------------------
6+
7+
- piped union typehints support added. See https://github.com/dapper91/pydantic-xml/issues/56
8+
49

510
0.6.1 (2023-04-15)
611
------------------

docs/source/pages/quickstart.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ For more information see :ref:`wrapped entities <pages/data-binding/wrapper:wrap
186186
Example
187187
_______
188188

189-
The following example illustrates all previously described rules combined with some ``pydantic`` feature:
189+
The following example illustrates all previously described rules combined with some ``pydantic`` features:
190190

191191
*doc.xml:*
192192

pydantic_xml/serializers/serializer.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import abc
22
import dataclasses as dc
3+
import sys
34
import typing
45
from enum import IntEnum
56
from inspect import isclass
67
from typing import Any, Dict, Optional, Tuple, Type, Union
78

9+
if sys.version_info < (3, 10):
10+
UnionTypes = (Union,)
11+
else:
12+
from types import UnionType
13+
UnionTypes = (Union, UnionType)
14+
815
import pydantic as pd
916

1017
import pydantic_xml as pxml
@@ -67,7 +74,7 @@ def is_xml_model(tp: Any) -> bool:
6774

6875

6976
def is_union(type_: Any) -> bool:
70-
return typing.get_origin(type_) is Union
77+
return typing.get_origin(type_) in UnionTypes
7178

7279

7380
def is_optional(type_: Any) -> bool:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pydantic-xml"
3-
version = "0.6.1"
3+
version = "0.6.2"
44
description = "pydantic xml extension"
55
authors = ["Dmitry Pershin <[email protected]>"]
66
license = "Unlicense"

tests/test_unions.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from typing import List, Tuple, Union
23

34
import pytest
@@ -156,3 +157,61 @@ class SubModel(BaseXmlModel):
156157

157158
class TestModel(BaseXmlModel):
158159
field1: Union[int, SubModel]
160+
161+
162+
@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python 3.10 and above")
163+
def test_union_type():
164+
class TestModel(BaseXmlModel, tag='model'):
165+
text: int | float | str
166+
167+
xml = '''
168+
<model>text</model>
169+
'''
170+
171+
actual_obj = TestModel.from_xml(xml)
172+
expected_obj = TestModel(text='text')
173+
174+
assert actual_obj == expected_obj
175+
176+
actual_xml = actual_obj.to_xml()
177+
assert_xml_equal(actual_xml, xml)
178+
179+
180+
@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python 3.10 and above")
181+
def test_model_union_type():
182+
class SubModel1(BaseXmlModel, tag='model1'):
183+
attr1: int = attr()
184+
185+
class SubModel2(BaseXmlModel, tag='model2'):
186+
text: float
187+
188+
class TestModel(BaseXmlModel, tag='model'):
189+
field1: SubModel1 | SubModel2 = element()
190+
191+
xml = '''
192+
<model><model1 attr1="1"></model1></model>
193+
'''
194+
195+
actual_obj = TestModel.from_xml(xml)
196+
expected_obj = TestModel(
197+
field1=SubModel1(attr1=1),
198+
)
199+
200+
assert actual_obj == expected_obj
201+
202+
actual_xml = actual_obj.to_xml()
203+
assert_xml_equal(actual_xml, xml)
204+
205+
xml = '''
206+
<model><model2>inf</model2></model>
207+
'''
208+
209+
actual_obj = TestModel.from_xml(xml)
210+
expected_obj = TestModel(
211+
field1=SubModel2(text=float('inf')),
212+
)
213+
214+
assert actual_obj == expected_obj
215+
216+
actual_xml = actual_obj.to_xml()
217+
assert_xml_equal(actual_xml, xml)

0 commit comments

Comments
 (0)