colander
colander copied to clipboard
Unexpected behaviour of .unflatten method. BUG?
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?
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.