Skip to content

Commit 252090c

Browse files
bysiberdeckar01sloria
authored
Fix nested partial to use attr_name instead of data_key for prefix (#2903)
* Fix nested partial to use attr_name instead of data_key When building dot-delimited sub_partial for nested schemas, the prefix was computed from field_name (which is data_key when set) instead of attr_name. Since users specify partial fields using attribute names like partial=("nested_field.child",), the prefix needs to match attr_name. When data_key differs from attr_name, the prefix mismatch caused the nested partial list to always be empty, so the nested schema never received the partial option and required fields raised unexpected validation errors. * Add test for nested partial with data_key using attr_name prefix * Update authors and changelog --------- Co-authored-by: Jared Deckard <jared@shademaps.com> Co-authored-by: Steven Loria <git@stevenloria.com>
1 parent 65374df commit 252090c

4 files changed

Lines changed: 24 additions & 1 deletion

File tree

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,4 @@ Contributors (chronological)
186186
- Emmanuel Ferdman `@emmanuel-ferdman <https://github.com/emmanuel-ferdman>`_
187187
- Fridayworks `@worksbyfriday <https://github.com/worksbyfriday>`_
188188
- `@rstar327 <https://github.com/rstar327>`_
189+
- Kadir Can Ozden `@bysiber <https://github.com/bysiber>`_

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Bug fixes:
1212
Thanks :user:`nosnickid` for the report and :user:`worksbyfriday` for the PR.
1313
- Fix `marshmallow.validate.OneOf` emitting extra pairs when labels outnumber choices (:issue:`2869`).
1414
Thanks: user:`T90REAL` for the report and :user:`rstar327` for the PR.
15+
- Fix behavior when passing a dot-delited attribute name to ``partial`` for a key with ``data_key`` set (:pr:`2903`).
16+
Thanks :user:`bysiber` for the PR.
1517

1618

1719
4.2.2 (2026-02-04)

src/marshmallow/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ def _deserialize(
654654
d_kwargs = {}
655655
# Allow partial loading of nested schemas.
656656
if partial_is_collection:
657-
prefix = field_name + "."
657+
prefix = attr_name + "."
658658
len_prefix = len(prefix)
659659
sub_partial = [
660660
f[len_prefix:] for f in partial if f.startswith(prefix)

tests/test_deserialization.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,6 +2307,26 @@ class SchemaB(Schema):
23072307
with pytest.raises(ValidationError):
23082308
SchemaB().load({"z": {"x": 0}})
23092309

2310+
def test_nested_partial_tuple_with_data_key(self):
2311+
class AddressSchema(Schema):
2312+
zip_code = fields.String(required=True)
2313+
city = fields.String(required=True)
2314+
2315+
class PersonSchema(Schema):
2316+
address = fields.Nested(
2317+
AddressSchema, data_key="homeAddress", required=True
2318+
)
2319+
2320+
data = {"homeAddress": {"city": "Springfield"}}
2321+
# partial uses attr_name ("address.zip_code"), not data_key
2322+
result = PersonSchema().load(data, partial=("address.zip_code",))
2323+
assert result["address"]["city"] == "Springfield"
2324+
assert "zip_code" not in result["address"]
2325+
# Without partial, it should fail on the missing required field
2326+
with pytest.raises(ValidationError) as excinfo:
2327+
PersonSchema().load(data)
2328+
assert "zip_code" in excinfo.value.messages["homeAddress"]
2329+
23102330

23112331
@pytest.mark.parametrize("FieldClass", ALL_FIELDS)
23122332
def test_required_field_failure(FieldClass):

0 commit comments

Comments
 (0)