Skip to content

Commit

Permalink
Merge pull request #59 from dapper91/dev
Browse files Browse the repository at this point in the history
- piped union typehints support added. See #56
  • Loading branch information
dapper91 authored Jun 10, 2023
2 parents 68ce048 + f6580e6 commit 51efb09
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

0.6.2 (2023-06-10)
------------------

- piped union typehints support added. See https://github.com/dapper91/pydantic-xml/issues/56


0.6.1 (2023-04-15)
------------------
Expand Down
2 changes: 1 addition & 1 deletion docs/source/pages/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ For more information see :ref:`wrapped entities <pages/data-binding/wrapper:wrap
Example
_______

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

*doc.xml:*

Expand Down
9 changes: 8 additions & 1 deletion pydantic_xml/serializers/serializer.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import abc
import dataclasses as dc
import sys
import typing
from enum import IntEnum
from inspect import isclass
from typing import Any, Dict, Optional, Tuple, Type, Union

if sys.version_info < (3, 10):
UnionTypes = (Union,)
else:
from types import UnionType
UnionTypes = (Union, UnionType)

import pydantic as pd

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


def is_union(type_: Any) -> bool:
return typing.get_origin(type_) is Union
return typing.get_origin(type_) in UnionTypes


def is_optional(type_: Any) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pydantic-xml"
version = "0.6.1"
version = "0.6.2"
description = "pydantic xml extension"
authors = ["Dmitry Pershin <dapper1291@gmail.com>"]
license = "Unlicense"
Expand Down
59 changes: 59 additions & 0 deletions tests/test_unions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from typing import List, Tuple, Union

import pytest
Expand Down Expand Up @@ -156,3 +157,61 @@ class SubModel(BaseXmlModel):

class TestModel(BaseXmlModel):
field1: Union[int, SubModel]


@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python 3.10 and above")
def test_union_type():
class TestModel(BaseXmlModel, tag='model'):
text: int | float | str

xml = '''
<model>text</model>
'''

actual_obj = TestModel.from_xml(xml)
expected_obj = TestModel(text='text')

assert actual_obj == expected_obj

actual_xml = actual_obj.to_xml()
assert_xml_equal(actual_xml, xml)


@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python 3.10 and above")
def test_model_union_type():
class SubModel1(BaseXmlModel, tag='model1'):
attr1: int = attr()

class SubModel2(BaseXmlModel, tag='model2'):
text: float

class TestModel(BaseXmlModel, tag='model'):
field1: SubModel1 | SubModel2 = element()

xml = '''
<model><model1 attr1="1"></model1></model>
'''

actual_obj = TestModel.from_xml(xml)
expected_obj = TestModel(
field1=SubModel1(attr1=1),
)

assert actual_obj == expected_obj

actual_xml = actual_obj.to_xml()
assert_xml_equal(actual_xml, xml)

xml = '''
<model><model2>inf</model2></model>
'''

actual_obj = TestModel.from_xml(xml)
expected_obj = TestModel(
field1=SubModel2(text=float('inf')),
)

assert actual_obj == expected_obj

actual_xml = actual_obj.to_xml()
assert_xml_equal(actual_xml, xml)

0 comments on commit 51efb09

Please sign in to comment.