marshmallow-code / marshmallow-code/marshmallow

Schema.dump_only returns empty set if fields explicitly declared dump_only

Open Beginner friendly
#1,857 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
Python
Stars
7.2k
Forks
738
Avg merge
1d 23h
Merged PRs (30d)
7

Description

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.

```# declaring dump_only inline in field defs:
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"})```

Contributor guide

Open the contributing guide

Research direction

Start at the Schema.dump_only entry point and reproduce the two schema definitions shown in the issue. Trace how inline field declarations and Meta.dump_only are collected, then add a regression check showing that the inline id field appears in dump_only and that the existing Meta behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
74/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.