Skip to content

Colander and None during deserialization? #276

@tsauerwein

Description

@tsauerwein

I am confused about the way Colander handles None values (or not handles None values) during the deserialization. For example:

class Person(colander.MappingSchema):
    name = colander.SchemaNode(colander.String(), missing=None)

schema = Person()
print(schema.deserialize({'name': None}))
# {'name': None}

In this case Colander accepts a None value, and keeps it because missing is set to None. This sounds reasonable to me.

Another example using a sequence:

class NoneSequence(colander.MappingSchema):
    foo = colander.SchemaNode(colander.String())
    bar = colander.SchemaNode(
        colander.Sequence(), colander.SchemaNode(colander.String()),
        missing=None)

schema = NoneSequence()
deserialized = schema.deserialize({'foo': '1', 'bar': None})
# colander.Invalid: {'bar': '"None" is not iterable'}

In this case Colander does not accept None.

Yet another example using a nested schema:

class SchemaA(colander.MappingSchema):
    val = colander.SchemaNode(colander.String())


class SchemaB(colander.MappingSchema):
    a = SchemaA(missing=None)


class SchemaC(colander.MappingSchema):
    b = SchemaB()

schema = SchemaC()
print(schema.deserialize({"b": {"a": None}}))
# colander.Invalid: {'b.a': '"None" is not a mapping type: Does not implement dict-like functionality.'}

Again None is not accepted. I have some more examples with nested sequences and tuples here: https://gist.github.com/tsauerwein/93c88ba3c37f936dea113a27447c0db5

What is the idea? Why does Colander accept None in some cases during the deserialization, and in some cases not? Do I have to replace all None values with colander.null before calling deserialize?

Looking the tests it seems that this is even the expected behavior:
https://github.com/Pylons/colander/blob/cb1645d/colander/tests/test_colander.py#L935
https://github.com/Pylons/colander/blob/cb1645d/colander/tests/test_colander.py#L690

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