Skip to content

Commit

Permalink
Merge pull request #2150 from deckar01/2149-default-nested-partial
Browse files Browse the repository at this point in the history
Default Nested Partial Fix
  • Loading branch information
lafrech committed Jul 20, 2023
2 parents 8197492 + 10afde1 commit 91147b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/marshmallow/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def __init__(
context: dict | None = None,
load_only: types.StrSequenceOrSet = (),
dump_only: types.StrSequenceOrSet = (),
partial: bool | types.StrSequenceOrSet = False,
partial: bool | types.StrSequenceOrSet | None = None,
unknown: str | None = None,
):
# Raise error if only or exclude is passed as string, not list of strings
Expand Down Expand Up @@ -590,7 +590,7 @@ def _deserialize(
*,
error_store: ErrorStore,
many: bool = False,
partial=False,
partial=None,
unknown=RAISE,
index=None,
) -> _T | list[_T]:
Expand Down Expand Up @@ -657,7 +657,7 @@ def _deserialize(
f[len_prefix:] for f in partial if f.startswith(prefix)
]
d_kwargs["partial"] = sub_partial
else:
elif partial is not None:
d_kwargs["partial"] = partial

def getter(
Expand Down Expand Up @@ -1089,7 +1089,7 @@ def _invoke_load_processors(
*,
many: bool,
original_data,
partial: bool | types.StrSequenceOrSet,
partial: bool | types.StrSequenceOrSet | None,
):
# This has to invert the order of the dump processors, so run the pass_many
# processors first.
Expand Down Expand Up @@ -1166,7 +1166,7 @@ def _invoke_schema_validators(
data,
original_data,
many: bool,
partial: bool | types.StrSequenceOrSet,
partial: bool | types.StrSequenceOrSet | None,
field_errors: bool = False,
):
for attr_name in self._hooks[(VALIDATES_SCHEMA, pass_many)]:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_deserialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2236,6 +2236,21 @@ class SchemaB(Schema):
with pytest.raises(ValidationError):
SchemaB().load(b_dict, partial=("z.y",))

def test_nested_partial_default(self):
class SchemaA(Schema):
x = fields.Integer(required=True)
y = fields.Integer(required=True)

class SchemaB(Schema):
z = fields.Nested(SchemaA(partial=("x",)))

b_dict = {"z": {"y": 42}}
# Nested partial args should be respected.
result = SchemaB().load(b_dict)
assert result["z"]["y"] == 42
with pytest.raises(ValidationError):
SchemaB().load({"z": {"x": 0}})


@pytest.mark.parametrize("FieldClass", ALL_FIELDS)
def test_required_field_failure(FieldClass): # noqa
Expand Down

0 comments on commit 91147b2

Please sign in to comment.