From 66f0f173f36f7f3c67c1cb60b7859e2c7573d1b7 Mon Sep 17 00:00:00 2001 From: Wanderrful Date: Thu, 17 Sep 2020 18:38:38 -0500 Subject: [PATCH 1/3] #100 Propagate meta dict to Nested dataclasses --- src/desert/_make.py | 5 +++-- tests/test_make.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/desert/_make.py b/src/desert/_make.py index 088386c..d250cf5 100644 --- a/src/desert/_make.py +++ b/src/desert/_make.py @@ -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( @@ -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) diff --git a/tests/test_make.py b/tests/test_make.py index 0e1aa01..cfda7c8 100644 --- a/tests/test_make.py +++ b/tests/test_make.py @@ -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.""" From 7258aba02299855344affb8cfc95748c467c8aa4 Mon Sep 17 00:00:00 2001 From: Wanderrful Date: Thu, 17 Sep 2020 18:53:52 -0500 Subject: [PATCH 2/3] Added 100.propagate.rst to changelog.d --- changelog.d/100.propagate.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/100.propagate.rst diff --git a/changelog.d/100.propagate.rst b/changelog.d/100.propagate.rst new file mode 100644 index 0000000..be278fc --- /dev/null +++ b/changelog.d/100.propagate.rst @@ -0,0 +1 @@ +Nested fields will now inherit their parent dataclass' metadata params so that `marshmallow.EXCLUDE` will propagate to child dataclasses. From 7a57ed169ed0cf98b7753e38aba9e749b4ac6b48 Mon Sep 17 00:00:00 2001 From: Wanderrful Date: Thu, 17 Sep 2020 18:58:22 -0500 Subject: [PATCH 3/3] Black'd the test_make.py file --- tests/test_make.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_make.py b/tests/test_make.py index cfda7c8..4230c1a 100644 --- a/tests/test_make.py +++ b/tests/test_make.py @@ -202,7 +202,9 @@ class A: class B: y: A - data = desert.schema_class(B, meta={"unknown": marshmallow.EXCLUDE})().load({"y": {"x": 5, "z": 3}}) + data = desert.schema_class(B, meta={"unknown": marshmallow.EXCLUDE})().load( + {"y": {"x": 5, "z": 3}} + ) assert data == B(A(5))