An observation record carries a list of attachments in which a slot may be empty, so the field is fields.List(fields.Nested(AttachmentSchema, allow_none=True)). The empty slots break both directions: loading rejects them…
An observation record carries a list of attachments in which a slot may be empty, so the field is fields.List(fields.Nested(AttachmentSchema, allow_none=True)). The empty slots break both directions: loading rejects them, and dumping turns them into empty objects.
from marshmallow import Schema, fields
class TagSchema(Schema):
label = fields.String(required=True)
field = fields.List(fields.Nested(TagSchema, allow_none=True))
field.deserialize([{"label": "a"}, None])
field.serialize("tags", {"tags": [{"label": "a"}, None]})
[{"label": "a"}, None] # deserialize
[{"label": "a"}, None] # serialize
ValidationError: {1: {'_schema': ['Invalid input type.']}} # deserialize
[{'label': 'a'}, {}] # serialize
Nested field inside a List should treat each item the way it treats a single value outside a list. With allow_none=True, None loads as None. Without it, a None item is reported as {index: ['Field may not be null.']}.None item stays None, with or without allow_none._schema.Adapted from marshmallow-code/marshmallow (MIT).
Visible test command
pytest testsThe repository’s own tests. Run all tests includes them, and so does every submission.
Repository
none-items-in-list-of-nested
Changes under src/ are evaluated.