Skip to content

Unexpected behaviour of .unflatten method. BUG? #240

@surabujin

Description

@surabujin

Some code chunk:

#!/usr/bin/env python

from __future__ import print_function

import copy
import pprint

from colander import MappingSchema
from colander import SchemaNode
from colander import String
from colander import drop


class StrictMappingSchema(MappingSchema):
    @staticmethod
    def schema_type():
        return MappingSchema.schema_type(unknown='raise')


class StrictSchema(StrictMappingSchema):
    foo = SchemaNode(String(), type='str', missing=drop)
    bar = SchemaNode(String(), type='str')


class NestedSchema(MappingSchema):
    egg = StrictSchema()
    ham = StrictSchema()


data = {'egg.bar': 'GET', 'ham': {'bar': 'POST'}}

schema = NestedSchema()
print('original data:')
pprint.pprint(data)

print('\nunflattened data:')
unflattened = copy.deepcopy(data)
pprint.pprint(schema.unflatten(unflattened))

It's output:

original data:
{'egg.bar': 'GET', 'ham': {'bar': 'POST'}}

unflattened data:
{'egg': {'bar': 'GET'}, 'ham': {}}

As you can see - we have lost content of original 'ham' key. Is it a bug or just .unflatten feature?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions