Revert "- Added `min_len and max_len arguments to colander.Sequence`"

This reverts commit 037bf71f6b.
This commit is contained in:
Chris McDonough
2011-02-28 18:04:58 -05:00
parent 037bf71f6b
commit fa4deaf294
3 changed files with 15 additions and 80 deletions

View File

@@ -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)
------------------

View File

@@ -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

View File

@@ -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)