The catalogue API nests an artist inside each album and trims it with only. That works when the nested schema is passed as a class. Passed as an instance, which we need in order to configure it, the album schema crashes…
The catalogue API nests an artist inside each album and trims it with only. That works when the nested schema is passed as a class. Passed as an instance, which we need in order to configure it, the album schema crashes on first use.
from marshmallow import Schema, fields
class ArtistSchema(Schema):
first = fields.Str()
last = fields.Str()
class AlbumSchema(Schema):
title = fields.Str()
artist = fields.Nested(ArtistSchema(), only=("last",))
AlbumSchema().dump({"title": "Hunky Dory", "artist": {"first": "David", "last": "Bowie"}})
{"title": "Hunky Dory", "artist": {"last": "Bowie"}}
TypeError: 'NoneType' object is not iterable
only on the field keeps just the named fields of the nested schema, for dumping and for loading, exactly as with fields.Nested(ArtistSchema, only=("last",)).only on the parent, AlbumSchema(only=("artist.last",)), goes through the same code and fails the same way.only of its own, the two are intersected. That case works today and must keep working, as must exclude on a nested instance.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
nested-schema-instance-with-only-crashes
Changes under src/ are evaluated.