Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/marshmallow/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,16 @@ def __init__(
self.exclude: set[typing.Any] | typing.MutableSet[typing.Any] = set(
self.opts.exclude
) | set(exclude)
self.load_only = set(load_only) or set(self.opts.load_only)
self.dump_only = set(dump_only) or set(self.opts.dump_only)
self.load_only = (set(load_only) or set(self.opts.load_only)) | {
name
for name, field_obj in self.declared_fields.items()
if field_obj.load_only
}
self.dump_only = (set(dump_only) or set(self.opts.dump_only)) | {
name
for name, field_obj in self.declared_fields.items()
if field_obj.dump_only
}
self.partial = partial
self.unknown: types.UnknownOption = (
self.opts.unknown if unknown is None else unknown
Expand Down
11 changes: 11 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,17 @@ class MySchema(Schema):
MySchema()


def test_load_only_and_dump_only_include_declared_field_options():
class MySchema(Schema):
read_only = fields.String(dump_only=True)
write_only = fields.String(load_only=True)

schema = MySchema()

assert schema.dump_only == {"read_only"}
assert schema.load_only == {"write_only"}


class TestDeeplyNestedLoadOnly:
@pytest.fixture
def schema(self):
Expand Down