add support for deferred schema nodes

This commit is contained in:
Gabriela Surita 2017-01-31 17:59:08 -02:00
parent df3d9497e9
commit 4d3df0881a
3 changed files with 16 additions and 1 deletions

View File

@ -137,3 +137,4 @@ Contributors
- Steve Piercy, 2016/02/26
- Sergiu Bivol, 2016/04/23
- Denis Nasyrov, 2016/08/23
- Gabriela Surita, 2017/01/31

View File

@ -2145,7 +2145,10 @@ class _SchemaNode(object):
v = getattr(self, k)
if isinstance(v, deferred):
v = v(self, kw)
setattr(self, k, v)
if isinstance(v, SchemaNode):
self[k] = v
else:
setattr(self, k, v)
if getattr(self, 'after_bind', None):
self.after_bind(self, kw)

View File

@ -3008,6 +3008,17 @@ class TestSchemaNodeSubclassing(unittest.TestCase):
bound_node = node.bind()
self.assertEqual(bound_node.deserialize(colander.null), 10)
def test_nodes_can_be_deffered(self):
import colander
class MySchema(colander.MappingSchema):
@colander.deferred
def child(node, kw):
return colander.SchemaNode(colander.String(), missing='foo')
node = MySchema()
bound_node = node.bind()
self.assertEqual(bound_node.deserialize({}), {'child': 'foo'})
def test_schema_child_names_conflict_with_value_names_notused(self):
import colander
class MyNode(colander.SchemaNode):