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
Show file tree
Hide file tree
Changes from 1 commit
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
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},
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(
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}
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)

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