diff --git a/CHANGELOG.md b/CHANGELOG.md index 218a7df..4fb186e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# Release 0.2.1 - Friday 11 November 15:54:21 AEDT 2016 + +- Ensure that lists with `None` value aren't turned into `[None]` (#7) + # Release 0.2.0 - Thursday 3 November 23:49:32 AEDT 2016 - Allow user to set XML encoding (#6) diff --git a/VERSION b/VERSION index 0ea3a94..0c62199 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.0 +0.2.1 diff --git a/schematics_xml/__init__.py b/schematics_xml/__init__.py index 1d791fa..cb6118c 100644 --- a/schematics_xml/__init__.py +++ b/schematics_xml/__init__.py @@ -10,7 +10,7 @@ 'VersionInfo', ('major', 'minor', 'micro', 'releaselevel', 'serial'), ) -VERSION = VersionInfo(0, 2, 0, '', '') +VERSION = VersionInfo(0, 2, 1, '', '') __version__ = '{0.major}.{0.minor}.{0.micro}{0.releaselevel}'.format(VERSION) __author__ = 'Alex Hayes' __contact__ = 'alex@alution.com' diff --git a/schematics_xml/models.py b/schematics_xml/models.py index bdaa570..7445ad1 100644 --- a/schematics_xml/models.py +++ b/schematics_xml/models.py @@ -191,6 +191,10 @@ def ensure_lists_in_model(raw_data: dict, model_cls: XMLModel): def ensure_lists_in_value(value: 'typing.Any', field: BaseType): + if value is None: + # Don't turn None items into a list of None items + return None + if isinstance(field, ListType): if not isinstance(value, list): value = [ diff --git a/schematics_xml/tests/test_models.py b/schematics_xml/tests/test_models.py index 0a51250..19ab210 100644 --- a/schematics_xml/tests/test_models.py +++ b/schematics_xml/tests/test_models.py @@ -842,6 +842,40 @@ class TestModel(XMLModel): # Assert good data stays good assert actual == expected + def test_model_with_listtype_of_none(self): # pylint: disable=no-self-use,invalid-name + """ + Ensure a model with a list type that has a value of None isn't turned into ``[None]``. + """ + class TestModel(XMLModel): + numbers = ListType(IntType()) + + bad_data = dict(numbers=None) + actual = ensure_lists_in_model(bad_data, TestModel) + expected = dict(numbers=None) + # Assert bad data can be turned good + assert actual == expected + + actual = ensure_lists_in_model(expected, TestModel) + # Assert good data stays good + assert actual == expected + + def test_model_with_listtype_of_mixed_value(self): # pylint: disable=no-self-use,invalid-name + """ + Ensure a model with a list type that has a value ``[1, None]`` is handled correctly. + """ + class TestModel(XMLModel): + numbers = ListType(IntType()) + + bad_data = dict(numbers=[1, None]) + actual = ensure_lists_in_model(bad_data, TestModel) + expected = dict(numbers=[1, None]) + # Assert bad data can be turned good + assert actual == expected + + actual = ensure_lists_in_model(expected, TestModel) + # Assert good data stays good + assert actual == expected + def test_model_with_listtype_of_modeltype(self): # pylint: disable=no-self-use,invalid-name """ Test that a model with a list type of models can correctly be