Skip to content

Commit

Permalink
python-desert#100 Propagate meta dict to Nested dataclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
wanderrful committed Sep 17, 2020
1 parent 631de5f commit 66f0f17
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/desert/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def class_schema(
attributes[field.name] = field_for_schema(
hints.get(field.name, field.type),
_get_field_default(field),
field.metadata,
{**meta, **field.metadata},
)

cls_schema = type(
Expand Down Expand Up @@ -289,7 +289,8 @@ def field_for_schema(

if field is None:
nested = forward_reference or class_schema(typ)
field = marshmallow.fields.Nested(nested)
params = {k: v for k, v in metadata.items() if type(k) is str}
field = marshmallow.fields.Nested(nested, **params)

field.metadata.update(metadata)

Expand Down
16 changes: 16 additions & 0 deletions tests/test_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@ class B:
assert data == B(A(5))


def test_nested_unknown(module):
"""Schemas will propagate meta to Nested dataclasses."""

@module.dataclass
class A:
x: int

@module.dataclass
class B:
y: A

data = desert.schema_class(B, meta={"unknown": marshmallow.EXCLUDE})().load({"y": {"x": 5, "z": 3}})

assert data == B(A(5))


def test_optional(module):
"""Setting an optional type makes the default None."""

Expand Down

0 comments on commit 66f0f17

Please sign in to comment.