From fa4deaf2940f142115b3a9dfef8782a7f84e68c5 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Mon, 28 Feb 2011 18:04:58 -0500 Subject: [PATCH] Revert "- Added ``min_len`` and ``max_len`` arguments to ``colander.Sequence``" This reverts commit 037bf71f6b107db560f90d7bdb267cf4e881e0c6. --- CHANGES.txt | 5 ---- colander/__init__.py | 60 +++++++++++--------------------------------- colander/tests.py | 30 ---------------------- 3 files changed, 15 insertions(+), 80 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 335ea01..7bf9afb 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -15,11 +15,6 @@ Next release passed explicitly to a SchemaNode, it is no longer replaced by a title computed from the name. -- Added ``min_len`` and ``max_len`` arguments to ``colander.Sequence`` - constructor. If used, these arguments indicate the minimum and maximum - length of the sequence during deserialization. If either constraint is not - met, an Invalid error is raised. - 0.9.1 (2010-12-02) ------------------ diff --git a/colander/__init__.py b/colander/__init__.py index 801df53..be99e86 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -561,7 +561,8 @@ class Tuple(Positional, SchemaType): return result class Sequence(Positional, SchemaType): - """A type which represents a variable-length sequence of nodes, + """ + A type which represents a variable-length sequence of nodes, all of which must be of the same type. The type of the first subnode of the @@ -571,66 +572,35 @@ class Sequence(Positional, SchemaType): The optional ``accept_scalar`` argument to this type's constructor indicates what should happen if the value found during serialization or deserialization does not have an ``__iter__`` method or is a - mapping type: + mapping type. - - If ``accept_scalar`` is ``True`` and the value does not have an - ``__iter__`` method or is a mapping type, the value will be turned into - a single element list. + If ``accept_scalar`` is ``True`` and the value does not have an + ``__iter__`` method or is a mapping type, the value will be turned + into a single element list. - - If ``accept_scalar`` is ``False`` and the value does not have an - ``__iter__`` method or is a mapping type, an :exc:`colander.Invalid` - error will be raised during serialization and deserialization. + If ``accept_scalar`` is ``False`` and the value does not have an + ``__iter__`` method or is a mapping type, an + :exc:`colander.Invalid` error will be raised during serialization + and deserialization. The default value of ``accept_scalar`` is ``False``. - The option ``min_len`` argument to this type's constructor indicates the - minimum number of values that must be present in this sequence during - deserialization. If a number of values fewer than ``min_len`` is present - in the deserialized sequence, an ``Invalid`` exception is raised. - - The option ``max_len`` argument to this type's constructor indicates the - minimum number of values that must be present in this sequence during - deserialization. If a number of values greater than ``max_len`` is - present in the deserialized sequence, an ``Invalid`` exception is raised. - If the :attr:`colander.null` value is passed to the serialize method of this class, the :attr:`colander.null` value is returned. - """ - def __init__(self, accept_scalar=False, min_len=None, max_len=None): + def __init__(self, accept_scalar=False): self.accept_scalar = accept_scalar - self.min = min_len - self.max = max_len def _validate(self, node, value, accept_scalar): + if hasattr(value, '__iter__') and not hasattr(value, 'get'): + return list(value) if accept_scalar: - if (not hasattr(value, '__iter__')) or hasattr(value, 'get'): - value = [value] - - if not hasattr(value, '__iter__'): + return [value] + else: raise Invalid(node, _('"${val}" is not iterable', mapping={'val':value}) ) - val = list(value) - - if self.max is not None: - if len(val) > self.max: - raise Invalid( - node, - _('Too many sequence elements (maximum ${num})', - mapping={'num':self.max}) - ) - - if self.min is not None: - if len(val) < self.min: - raise Invalid(node, - _('Too few sequence elements (minimum ${num})', - mapping={'num':self.min}) - ) - - return val - def _impl(self, node, value, callback, accept_scalar): if accept_scalar is None: accept_scalar = self.accept_scalar diff --git a/colander/tests.py b/colander/tests.py index 5ca0e2b..1c72572 100644 --- a/colander/tests.py +++ b/colander/tests.py @@ -613,36 +613,6 @@ class TestSequence(unittest.TestCase): result = typ.deserialize(node, None) self.assertEqual(result, [None]) - def test_deserialize_min_len_fail(self): - node = DummySchemaNode(None) - typ = self._makeOne(min_len=1) - e = invalid_exc(typ.deserialize, node, []) - self.assertEqual(e.msg.interpolate(), - 'Too few sequence elements (minimum 1)') - - def test_deserialize_min_len_success(self): - node = DummySchemaNode(None) - typ = self._makeOne(min_len=1) - child = DummySchemaNode(None) - node.children = [child] - result = typ.deserialize(node, [None]) - self.assertEqual(result, [None]) - - def test_deserialize_max_len_fail(self): - node = DummySchemaNode(None) - typ = self._makeOne(max_len=1) - e = invalid_exc(typ.deserialize, node, [None, None]) - self.assertEqual(e.msg.interpolate(), - 'Too many sequence elements (maximum 1)') - - def test_deserialize_max_len_success(self): - node = DummySchemaNode(None) - typ = self._makeOne(max_len=1) - child = DummySchemaNode(None) - node.children = [child] - result = typ.deserialize(node, [None]) - self.assertEqual(result, [None]) - def test_deserialize_no_subnodes(self): typ = self._makeOne() node = DummySchemaNode(None)