Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#100 Propagate meta dict to Nested dataclasses #106

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions changelog.d/100.propagate.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Nested fields will now inherit their parent dataclass' metadata params so that `marshmallow.EXCLUDE` will propagate to child dataclasses.
5 changes: 3 additions & 2 deletions src/desert/_make.py
Original file line number Diff line number Diff line change
@@ -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},
Copy link
Author

@wanderrful wanderrful Sep 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we're using field.metadata second here, the parent Schema's meta dict will not overwrite a Nested field's potential metadata params.

)

cls_schema = type(
@@ -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}
Copy link
Author

@wanderrful wanderrful Sep 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we are filtering for string keys, we don't have to worry about the Sentinel key breaking any typing expectations downstream.

field = marshmallow.fields.Nested(nested, **params)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the Nested class accepts **kwargs in its signature, we don't have to worry about extraneous params breaking anything downstream.


field.metadata.update(metadata)

18 changes: 18 additions & 0 deletions tests/test_make.py
Original file line number Diff line number Diff line change
@@ -191,6 +191,24 @@ 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."""