Skip to content

Commit fc3f499

Browse files
authored
Merge pull request #132 from dapper91/dev
- root model default value bug fixed. - python 3.12 support added.
2 parents b66edb4 + 5032769 commit fc3f499

File tree

6 files changed

+51
-9
lines changed

6 files changed

+51
-9
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
python-version: ['3.8', '3.9', '3.10', '3.11']
17+
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
1818
steps:
1919
- uses: actions/checkout@v2
2020
- name: Set up Python ${{ matrix.python-version }}

CHANGELOG.rst

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

4+
2.2.4 (2023-10-06)
5+
------------------
6+
7+
- root model default value bug fixed.
8+
- python 3.12 support added.
9+
10+
411
2.2.3 (2023-09-20)
512
------------------
613

docs/source/pages/data-binding/unions.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ ___________
77
To declare a field that can be of one type or anther :py:obj:`typing.Union` is used.
88
It works for primitive types and models as well but not combined together.
99

10+
The type declaration order matters.
11+
Currently, `pydantic` has two validation modes:
12+
13+
- 'left_to_right', where the first successful validation is accepted,
14+
- 'smart' (default), the first type that matches (without coercion) wins.
15+
16+
You can read more about it in the
17+
`pydantic docs <https://docs.pydantic.dev/latest/api/standard_library_types/#union>`_.
1018

1119
Primitive types
1220
***************
1321

14-
Union can be applied to text, attributes or elements. The type declaration order matters
15-
since the first type matched wins.
22+
Union can be applied to text, attributes or elements.
1623

1724
.. grid:: 2
1825
:gutter: 2
@@ -48,9 +55,7 @@ since the first type matched wins.
4855
Model types
4956
***********
5057

51-
Union can be applied to model types either. The type declaration order matters
52-
since the first model matched wins.
53-
58+
Union can be applied to model types either.
5459

5560
.. grid:: 2
5661
:gutter: 2

pydantic_xml/serializers/factories/model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ def deserialize(
267267
if element is None:
268268
return None
269269

270-
result = self._root_serializer.deserialize(element, context=context)
270+
if (result := self._root_serializer.deserialize(element, context=context)) is None:
271+
result = pdc.PydanticUndefined
271272

272273
if self._model.model_config.get('extra', 'ignore') == 'forbid':
273274
self._check_extra(self._model.__name__, element)

pyproject.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pydantic-xml"
3-
version = "2.2.3"
3+
version = "2.2.4"
44
description = "pydantic xml extension"
55
authors = ["Dmitry Pershin <dapper1291@gmail.com>"]
66
license = "Unlicense"
@@ -16,14 +16,19 @@ classifiers = [
1616
"Intended Audience :: Developers",
1717
"Natural Language :: English",
1818
"License :: Public Domain",
19+
"Operating System :: OS Independent",
1920
"Topic :: Software Development :: Libraries",
21+
"Topic :: Text Processing :: Markup :: XML",
2022
'Framework :: Pydantic',
21-
'Framework :: Pydantic :: 2',
23+
"Framework :: Pydantic :: 1",
24+
"Framework :: Pydantic :: 2",
2225
"Programming Language :: Python",
2326
"Programming Language :: Python :: 3.8",
2427
"Programming Language :: Python :: 3.9",
2528
"Programming Language :: Python :: 3.10",
2629
"Programming Language :: Python :: 3.11",
30+
"Programming Language :: Python :: 3.12",
31+
"Typing :: Typed",
2732
]
2833

2934
[tool.poetry.dependencies]

tests/test_primitives.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,27 @@ class TestModel(RootXmlModel, tag='model'):
193193

194194
actual_xml = actual_obj.to_xml()
195195
assert_xml_equal(actual_xml, xml)
196+
197+
198+
def test_root_model_default():
199+
class TestRootModel(RootXmlModel, tag='sub'):
200+
root: int = 1
201+
202+
class TestModel(BaseXmlModel, tag='model'):
203+
sub: TestRootModel
204+
205+
xml = '''
206+
<model><sub></sub></model>
207+
'''
208+
209+
actual_obj = TestModel.from_xml(xml)
210+
expected_obj = TestModel(sub=TestRootModel(1))
211+
212+
assert actual_obj == expected_obj
213+
214+
actual_xml = actual_obj.to_xml()
215+
216+
expected_xml = '''
217+
<model><sub>1</sub></model>
218+
'''
219+
assert_xml_equal(actual_xml, expected_xml)

0 commit comments

Comments
 (0)