marshmallow
marshmallow copied to clipboard
Schema.dump_only returns empty set if fields explicitly declared dump_only
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"})```
I'm not sure this is safe to change this in marshmallow 3 but we could think of it for v4, along with the thoughts about setting/getting field attributes.