From 165478b675ae9c94b24967a9f6adc3c68b3924b3 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sat, 12 Jun 2010 18:03:00 +0000 Subject: [PATCH] - Make it possible to use ``colander.null`` as a ``missing`` argument to ``colander.SchemaNode`` for roundtripping purposes. - Make it possible to pickle ``colander.null``. --- CHANGES.txt | 11 +++++++++++ colander/__init__.py | 13 +++++++------ colander/tests.py | 7 +++++++ docs/null.rst | 15 ++++++++------- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 293ed5b..3147268 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,6 +1,17 @@ Changes ======= +Next Release +------------ + +Bug Fixes +~~~~~~~~~ + +- Make it possible to use ``colander.null`` as a ``missing`` argument + to ``colander.SchemaNode`` for roundtripping purposes. + +- Make it possible to pickle ``colander.null``. + 0.7.0 ----- diff --git a/colander/__init__.py b/colander/__init__.py index 8ac6ca3..de6b546 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -8,14 +8,16 @@ import translationstring _ = translationstring.TranslationStringFactory('colander') -class null(object): +_marker = object() + +class _null(object): def __nonzero__(self): return False def __repr__(self): return '' -null = null() +null = _null() def interpolate(msgs): for s in msgs: @@ -1169,7 +1171,7 @@ class SchemaNode(object): self.typ = typ self.validator = kw.pop('validator', None) self.default = kw.pop('default', null) - self.missing = kw.pop('missing', null) + self.missing = kw.pop('missing', _marker) self.name = kw.pop('name', '') self.title = kw.pop('title', self.name.capitalize()) self.description = kw.pop('description', '') @@ -1194,7 +1196,7 @@ class SchemaNode(object): A return value of ``True`` implies that a ``missing`` value wasn't specified for this node. A return value of ``False`` implies that a ``missing`` value was specified for this node.""" - return self.missing is null + return self.missing is _marker def serialize(self, appstruct=null): """ Serialize the :term:`appstruct` to a :term:`cstruct` based @@ -1208,7 +1210,6 @@ class SchemaNode(object): If an ``appstruct`` argument is not explicitly provided, it defaults to :attr:`colander.null`. """ - if appstruct is null: appstruct = self.default cstruct = self.typ.serialize(self, appstruct) @@ -1235,7 +1236,7 @@ class SchemaNode(object): """ if cstruct is null: appstruct = self.missing - if appstruct is null: + if appstruct is _marker: raise Invalid(self, _('Required')) # We never deserialize or validate the missing value return appstruct diff --git a/colander/tests.py b/colander/tests.py index 573eadb..562bf80 100644 --- a/colander/tests.py +++ b/colander/tests.py @@ -1340,6 +1340,13 @@ class TestSchemaNode(unittest.TestCase): node.missing = 'abc' self.assertEqual(node.deserialize(), 'abc') + def test_deserialize_null_can_be_used_as_missing(self): + from colander import null + typ = DummyType() + node = self._makeOne(typ) + node.missing = null + self.assertEqual(node.deserialize(null), null) + def test_serialize(self): typ = DummyType() node = self._makeOne(typ) diff --git a/docs/null.rst b/docs/null.rst index 823c0f4..1cd476c 100644 --- a/docs/null.rst +++ b/docs/null.rst @@ -255,7 +255,8 @@ an ``age`` key of :attr:`colander.null`, the ``missing`` value of .. note:: Note that ``None`` can be used for the ``missing`` schema node value as required, as in the above example. It's no different - than any other value used as ``missing``. + than any other value used as ``missing``. or ``colander.nuil`` can + also be used as the ``missing`` value if that is helpful. The :attr:`colander.null` value is also the default, so it needn't be specified in the cstruct. Therefore, the ``deserialized`` value of @@ -287,12 +288,12 @@ Value Missing Result ===================== ===================== =========================== colander.null Invalid exception raised Invalid exception raised -colander.null colander.null Invalid exception raised -colander.null value value deserialized - value value deserialized -value value deserialized -value colander.null value deserialized -value_a value_b value_a deserialized +colander.null value value used + value value used + colander.null colander.null used +value value used +value colander.null value used +value_a value_b value_a used ===================== ===================== =========================== .. note:: ```` in the above table represents the circumstance