colander icon indicating copy to clipboard operation
colander copied to clipboard

flatten() AttributeError: '_null' object has no attribute 'get'

Open dmdm opened this issue 10 years ago • 0 comments

I have a schema with a nested MappingSchema:

class ConfSchema(colander.MappingSchema):
    foo = colander.SchemaNode(colander.Integer())

    @colander.instantiate(missing={})
    class scales(colander.MappingSchema):
        bar = MiniMax()
        baz = MiniMax()

When I flatten an appstruct that has no key 'scales', flatten() throws this exception:

    pprint(sch.flatten(self.mgr.conf))
  File "..../python3.4/site-packages/colander/__init__.py", line 1874, in flatten
    flat = self.typ.flatten(self, appstruct)
  File "..../python3.4/site-packages/colander/__init__.py", line 622, in flatten
    prefix=selfprefix))
  File "..../python3.4/site-packages/colander/__init__.py", line 620, in flatten
    substruct = appstruct.get(name, null)
AttributeError: '_null' object has no attribute 'get'

This patch might help:

    def flatten(self, node, appstruct, prefix='', listitem=False):
        result = {}
        if listitem:
            selfprefix = prefix
        else:
            if node.name:
                selfprefix = '%s%s.' % (prefix, node.name)
            else:
                selfprefix = prefix

        for subnode in node.children:
            # BEGIN PATCH
            if isinstance(appstruct, _null):
                continue
            # END PATCH
            name = subnode.name
            substruct = appstruct.get(name, null)
            result.update(subnode.typ.flatten(subnode, substruct,
                                              prefix=selfprefix))
        return result

dmdm avatar Feb 16 '15 12:02 dmdm