Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Unexpected behaviour of .unflatten method. BUG? #240

Open
surabujin opened this issue Aug 25, 2015 · 1 comment
Open

Unexpected behaviour of .unflatten method. BUG? #240

surabujin opened this issue Aug 25, 2015 · 1 comment

Comments

@surabujin
Copy link
Contributor

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?

@tisdall
Copy link
Contributor

tisdall commented Aug 25, 2015

I'd probably call it a "limitation". The unflatten method expects a flattened mapping. If you pass it something that's only partially flattened, it'd be nice if if it retained the parts it didn't understand, but it's not unreasonable for it to not.

You could create a PR to expand the functionality of unflatten to handle mixed mappings like this if you think it's beneficial.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants