Skip to content

Commit fc6f52a

Browse files
committed
Add changelog and docs for propagate_unknown
The changelog entry, including credit to prior related work, covers the closed issues and describes how `propagate_unknown` is expected to behave. Inline documentation attempts to strike a balance between clarify and brevity. closes #1428, #1429, #1490, #1575
1 parent 0dcee07 commit fc6f52a

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

CHANGELOG.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
11
Changelog
22
---------
33

4+
3.8.0 (Unreleased)
5+
******************
6+
7+
Features:
8+
9+
- The behavior of the ``unknown`` option can be further customized with a
10+
second option, ``propagate_unknown``. When set to ``True``, any nested
11+
schemas will receive the same ``unknown`` value as their parent if they did
12+
not set an ``unknown`` value explicitly.
13+
``propagate_unknown`` itself propagates across any number of layers of
14+
nesting and cannot be disabled by a child schema if it enabled in its parent.
15+
(:issue:`1490`, :issue:`1428`)
16+
Thanks :user:`lmignon` and :user:`mahenzon`.
17+
18+
.. note::
19+
20+
In order to retain flexibility, when a schema is being loaded with
21+
``propagate_unknown=True``, you can still set ``unknown`` explicitly on child
22+
schemas. However, be aware that such a value will be propagated to any of its
23+
descendants.
24+
25+
For example, a schema which specifies ``unknown=EXCLUDE`` will set
26+
``EXCLUDE`` in all of its children. If one of the children specifies
27+
``unknown=IGNORE``, then ``IGNORE`` will be passed to the relevant
28+
grandchildren.
29+
Other children of the original schema could specify ``unknown=RAISE``, and
30+
this would apply to them equally well.
31+
432
3.7.0 (2020-07-08)
533
******************
634

src/marshmallow/fields.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,10 @@ class ParentSchema(Schema):
459459
:param many: Whether the field is a collection of objects.
460460
:param unknown: Whether to exclude, include, or raise an error for unknown
461461
fields in the data. Use `EXCLUDE`, `INCLUDE` or `RAISE`.
462+
:param propagate_unknown: If ``True``, the value for ``unknown`` will be
463+
applied to any ``Nested`` fields within the nested schema. Propagates down and allows
464+
``Nested`` fields to apply their own ``unknown`` value. Note that this
465+
only has an effect when there are multiple layers of nesting
462466
:param kwargs: The same keyword arguments that :class:`Field` receives.
463467
"""
464468

src/marshmallow/schema.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ class AlbumSchema(Schema):
287287
will be ignored. Use dot delimiters to specify nested fields.
288288
:param unknown: Whether to exclude, include, or raise an error for unknown
289289
fields in the data. Use `EXCLUDE`, `INCLUDE` or `RAISE`.
290+
:param propagate_unknown: If ``True``, the value for ``unknown`` will be
291+
applied to all ``Nested`` fields. Propagates down and allows
292+
``Nested`` fields to apply their own ``unknown`` value
290293
291294
.. versionchanged:: 3.0.0
292295
`prefix` parameter removed.
@@ -364,6 +367,10 @@ class Meta:
364367
- ``dump_only``: Tuple or list of fields to exclude from deserialization
365368
- ``unknown``: Whether to exclude, include, or raise an error for unknown
366369
fields in the data. Use `EXCLUDE`, `INCLUDE` or `RAISE`.
370+
- ``propagate_unknown`` If ``True``, the value for ``unknown`` will be
371+
applied to all ``Nested`` fields. Propagates down, but if a ``Nested``
372+
field sets ``unknown``, it will begin to propagate that value, not the
373+
where this is set.
367374
- ``register``: Whether to register the `Schema` with marshmallow's internal
368375
class registry. Must be `True` if you intend to refer to this `Schema`
369376
by class name in `Nested` fields. Only set this to `False` when memory
@@ -619,6 +626,9 @@ def _deserialize(
619626
will be ignored. Use dot delimiters to specify nested fields.
620627
:param unknown: Whether to exclude, include, or raise an error for unknown
621628
fields in the data. Use `EXCLUDE`, `INCLUDE` or `RAISE`.
629+
:param propagate_unknown: If ``True``, the value for ``unknown`` will be
630+
applied to all ``Nested`` fields. Propagates down and allows
631+
``Nested`` fields to apply their own ``unknown`` value
622632
:param int index: Index of the item being serialized (for storing errors) if
623633
serializing a collection, otherwise `None`.
624634
:return: A dictionary of the deserialized data.
@@ -733,6 +743,9 @@ def load(
733743
:param unknown: Whether to exclude, include, or raise an error for unknown
734744
fields in the data. Use `EXCLUDE`, `INCLUDE` or `RAISE`.
735745
If `None`, the value for `self.unknown` is used.
746+
:param propagate_unknown: If ``True``, the value for ``unknown`` will be
747+
applied to all ``Nested`` fields. Propagates down and allows
748+
``Nested`` fields to apply their own ``unknown`` value
736749
:return: Deserialized data
737750
738751
.. versionadded:: 1.0.0
@@ -772,6 +785,9 @@ def loads(
772785
:param unknown: Whether to exclude, include, or raise an error for unknown
773786
fields in the data. Use `EXCLUDE`, `INCLUDE` or `RAISE`.
774787
If `None`, the value for `self.unknown` is used.
788+
:param propagate_unknown: If ``True``, the value for ``unknown`` will be
789+
applied to all ``Nested`` fields. Propagates down and allows
790+
``Nested`` fields to apply their own ``unknown`` value
775791
:return: Deserialized data
776792
777793
.. versionadded:: 1.0.0
@@ -864,6 +880,9 @@ def _do_load(
864880
:param unknown: Whether to exclude, include, or raise an error for unknown
865881
fields in the data. Use `EXCLUDE`, `INCLUDE` or `RAISE`.
866882
If `None`, the value for `self.unknown` is used.
883+
:param propagate_unknown: If ``True``, the value for ``unknown`` will be
884+
applied to all ``Nested`` fields. Propagates down and allows
885+
``Nested`` fields to apply their own ``unknown`` value
867886
:param postprocess: Whether to run post_load methods..
868887
:return: Deserialized data
869888
"""

0 commit comments

Comments
 (0)