Schema.dump_only returns empty set if fields explicitly declared dump_only
Author: RookieRickCreated Jul 29, 2021Updated Aug 23, 2026
Ran across this behavior when trying to determine the dump_only fields in a given Schema. If the Schema defines dump_only fields via the Meta class approach, it works as expected, but if I specify dump_only when adding the fields, Schema.dump_only returns an empty set.
An easy workaround is to use Schema.dump_fields.items() - Schema.load_fields.items() so this isn't a show-stopper for me, but it was counter-intuitive when I saw that Schema had a dump_only attribute and expected it to include those.
class MySchema(m.Schema):
id = m.fields.Integer(dump_only=True)
label = m.fields.String(missing="(none)")
instance = MySchema()
assert(instance.dump_only == set()) # this should be {"id"}, no???
# declaring dump_only explicitly as Meta:
class MyOtherSchema(m.Schema):
id = m.fields.Integer()
label = m.fields.String(missing="(none)")
class Meta:
dump_only=("id", )
other_instance = MyOtherSchema()
assert(other_instance.dump_only == {"id"})```Source: marshmallow-code/marshmallow