marshmallow 3.26.0__tar.gz → 4.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {marshmallow-3.26.0 → marshmallow-4.0.0}/CHANGELOG.rst +141 -1
- {marshmallow-3.26.0 → marshmallow-4.0.0}/PKG-INFO +8 -6
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/api_reference.rst +1 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/conf.py +2 -1
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/custom_fields.rst +50 -22
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/extending/index.rst +0 -1
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/install.rst +0 -2
- marshmallow-4.0.0/docs/marshmallow.experimental.context.rst +5 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/nesting.rst +1 -1
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/quickstart.rst +21 -57
- marshmallow-4.0.0/docs/top_level.rst +35 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/upgrading.rst +333 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/why.rst +0 -33
- {marshmallow-3.26.0 → marshmallow-4.0.0}/pyproject.toml +13 -12
- marshmallow-4.0.0/src/marshmallow/__init__.py +31 -0
- marshmallow-4.0.0/src/marshmallow/constants.py +22 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/src/marshmallow/decorators.py +56 -40
- {marshmallow-3.26.0 → marshmallow-4.0.0}/src/marshmallow/exceptions.py +3 -3
- marshmallow-4.0.0/src/marshmallow/experimental/__init__.py +5 -0
- marshmallow-4.0.0/src/marshmallow/experimental/context.py +73 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/src/marshmallow/fields.py +379 -452
- {marshmallow-3.26.0 → marshmallow-4.0.0}/src/marshmallow/schema.py +140 -199
- {marshmallow-3.26.0 → marshmallow-4.0.0}/src/marshmallow/types.py +14 -6
- marshmallow-4.0.0/src/marshmallow/utils.py +171 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/src/marshmallow/validate.py +2 -16
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tests/base.py +20 -79
- marshmallow-4.0.0/tests/mypy_test_cases/test_schema.py +29 -0
- marshmallow-4.0.0/tests/test_context.py +251 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tests/test_decorators.py +99 -62
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tests/test_deserialization.py +161 -143
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tests/test_fields.py +75 -132
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tests/test_options.py +9 -138
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tests/test_registry.py +24 -9
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tests/test_schema.py +96 -551
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tests/test_serialization.py +39 -97
- marshmallow-4.0.0/tests/test_utils.py +141 -0
- marshmallow-3.26.0/docs/extending/using_context.rst +0 -20
- marshmallow-3.26.0/docs/top_level.rst +0 -12
- marshmallow-3.26.0/src/marshmallow/__init__.py +0 -81
- marshmallow-3.26.0/src/marshmallow/base.py +0 -61
- marshmallow-3.26.0/src/marshmallow/utils.py +0 -380
- marshmallow-3.26.0/src/marshmallow/warnings.py +0 -10
- marshmallow-3.26.0/tests/test_utils.py +0 -283
- marshmallow-3.26.0/tests/test_version_attributes.py +0 -15
- {marshmallow-3.26.0 → marshmallow-4.0.0}/CONTRIBUTING.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/LICENSE +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/NOTICE +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/README.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/SECURITY.md +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/.gitignore +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/_static/apple-touch-icon.png +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/_static/custom.css +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/_static/favicon.ico +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/_static/marshmallow-logo-200.png +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/_static/marshmallow-logo-with-title-for-dark-theme.png +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/_static/marshmallow-logo-with-title.png +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/_static/marshmallow-logo.png +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/authors.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/changelog.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/code_of_conduct.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/contributing.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/dashing.json +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/donate.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/examples/index.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/examples/inflection.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/examples/quotes_api.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/examples/validating_package_json.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/extending/custom_error_handling.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/extending/custom_error_messages.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/extending/custom_options.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/extending/overriding_attribute_access.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/extending/pre_and_post_processing_methods.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/extending/schema_validation.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/extending/using_original_input_data.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/index.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/kudos.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/license.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/marshmallow.class_registry.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/marshmallow.decorators.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/marshmallow.error_store.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/marshmallow.exceptions.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/marshmallow.fields.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/marshmallow.schema.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/marshmallow.types.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/marshmallow.utils.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/marshmallow.validate.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/docs/whos_using.rst +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/src/marshmallow/class_registry.py +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/src/marshmallow/error_store.py +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/src/marshmallow/orderedset.py +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/src/marshmallow/py.typed +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tests/__init__.py +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tests/conftest.py +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tests/foo_serializer.py +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tests/mypy_test_cases/test_class_registry.py +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tests/mypy_test_cases/test_validation_error.py +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tests/test_error_store.py +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tests/test_exceptions.py +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tests/test_validate.py +0 -0
- {marshmallow-3.26.0 → marshmallow-4.0.0}/tox.ini +0 -0
|
@@ -1,6 +1,146 @@
|
|
|
1
1
|
Changelog
|
|
2
2
|
---------
|
|
3
3
|
|
|
4
|
+
4.0.0 (2025-04-16)
|
|
5
|
+
******************
|
|
6
|
+
|
|
7
|
+
See :ref:`upgrading_4_0` for a guide on updating your code.
|
|
8
|
+
|
|
9
|
+
Features:
|
|
10
|
+
|
|
11
|
+
- Typing: Add types to all `Field <marshmallow.fields.Field>` constructor kwargs (:issue:`2285`).
|
|
12
|
+
Thanks :user:`navignaw` for the suggestion.
|
|
13
|
+
- `DateTime <marshmallow.fields.DateTime>`, `Date <marshmallow.fields.Date>`, `Time <marshmallow.fields.Time>`,
|
|
14
|
+
`TimeDelta <marshmallow.fields.TimeDelta>`, and `Enum <marshmallow.fields.Enum>`
|
|
15
|
+
accept their internal value types as valid input (:issue:`1415`).
|
|
16
|
+
Thanks :user:`bitdancer` for the suggestion.
|
|
17
|
+
- `@validates <marshmallow.validates>` accepts multiple field names (:issue:`1960`).
|
|
18
|
+
*Backwards-incompatible*: Decorated methods now receive ``data_key`` as a keyword argument.
|
|
19
|
+
Thanks :user:`dpriskorn` for the suggestion and :user:`dharani7998` for the PR.
|
|
20
|
+
|
|
21
|
+
Other changes:
|
|
22
|
+
|
|
23
|
+
- Typing: `Field <marshmallow.fields.Field>` is now a generic type with a type argument for the internal value type.
|
|
24
|
+
- `marshmallow.fields.UUID` no longer subclasses `marshmallow.fields.String`.
|
|
25
|
+
- *Backwards-incompatible*: Use `datetime.date.fromisoformat`, `datetime.time.fromisoformat`, and `datetime.datetime.fromisoformat` from the standard library to deserialize dates, times and datetimes (:pr:`2078`).
|
|
26
|
+
- `marshmallow.Schema.load` no longer silently fails to call schema validators when a generator is passed (:issue:`1898`).
|
|
27
|
+
The typing of `data` is also updated to be more accurate.
|
|
28
|
+
Thanks :user:`ziplokk1` for reporting.
|
|
29
|
+
|
|
30
|
+
As a consequence of this change:
|
|
31
|
+
- Time with time offsets are now supported.
|
|
32
|
+
- YYYY-MM-DD is now accepted as a datetime and deserialized as naive 00:00 AM.
|
|
33
|
+
- `from_iso_date`, `from_iso_time` and `from_iso_datetime` are removed from `marshmallow.utils`.
|
|
34
|
+
|
|
35
|
+
- Remove `isoformat`, `to_iso_time` and `to_iso_datetime` from `marshmallow.utils` (:pr:`2766`).
|
|
36
|
+
- Remove `from_rfc`, and `rfcformat` from `marshmallow.utils` (:pr:`2767`).
|
|
37
|
+
- Remove `is_keyed_tuple` from `marshmallow.utils` (:pr:`2768`).
|
|
38
|
+
- Remove `get_fixed_timezone` from `marshmallow.utils` (:pr:`2773`).
|
|
39
|
+
|
|
40
|
+
- *Backwards-incompatible*: `marshmallow.fields.Boolean` no longer serializes non-boolean values (:pr:`2725`).
|
|
41
|
+
- *Backwards-incompatible*: Rename ``schema`` parameter to ``parent`` in `marshmallow.fields.Field._bind_to_schema` (:issue:`1360`).
|
|
42
|
+
- *Backwards-incompatible*: Rename ``pass_many`` parameter to ``pass_collection`` in pre/post processing methods (:issue:`1369`).
|
|
43
|
+
- *Backwards-incompatible*: `marshmallow.fields.TimeDelta` no longer truncates float values when
|
|
44
|
+
deserializing (:pr:`2654`). This allows microseconds to be preserved, e.g.
|
|
45
|
+
|
|
46
|
+
.. code-block:: python
|
|
47
|
+
|
|
48
|
+
from marshmallow import fields
|
|
49
|
+
|
|
50
|
+
field = fields.TimeDelta()
|
|
51
|
+
|
|
52
|
+
# Before
|
|
53
|
+
field.deserialize(12.9)
|
|
54
|
+
datetime.timedelta(seconds=12)
|
|
55
|
+
# datetime.timedelta(seconds=12)
|
|
56
|
+
|
|
57
|
+
# After
|
|
58
|
+
field.deserialize(12.9)
|
|
59
|
+
# datetime.timedelta(seconds=12, microseconds=900000)
|
|
60
|
+
|
|
61
|
+
- Improve performance and minimize float precision loss of `marshmallow.fields.TimeDelta` serialization (:pr:`2654`).
|
|
62
|
+
- *Backwards-incompatible*: Remove ``serialization_type`` parameter from
|
|
63
|
+
`marshmallow.fields.TimeDelta` (:pr:`2654`).
|
|
64
|
+
|
|
65
|
+
Thanks :user:`ddelange` for the PR.
|
|
66
|
+
|
|
67
|
+
- *Backwards-incompatible*: Remove `Schema <marshmallow.schema.Schema>`'s ``context`` attribute (deprecated since 3.24.0). Passing a context
|
|
68
|
+
should be done using `contextvars.ContextVar` (:issue:`1826`).
|
|
69
|
+
marshmallow 4 provides an experimental `Context <marshmallow.experimental.context.Context>`
|
|
70
|
+
manager class that can be used to both set and retrieve context.
|
|
71
|
+
|
|
72
|
+
.. code-block:: python
|
|
73
|
+
|
|
74
|
+
import typing
|
|
75
|
+
|
|
76
|
+
from marshmallow import Schema, fields
|
|
77
|
+
from marshmallow.experimental.context import Context
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class UserContext(typing.TypedDict):
|
|
81
|
+
suffix: str
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class UserSchema(Schema):
|
|
85
|
+
name_suffixed = fields.Function(
|
|
86
|
+
lambda obj: obj["name"] + Context[UserContext].get()["suffix"]
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
with Context[UserContext]({"suffix": "bar"}):
|
|
91
|
+
UserSchema().dump({"name": "foo"})
|
|
92
|
+
# {'name_suffixed': 'foobar'}
|
|
93
|
+
|
|
94
|
+
- Methods decorated with `marshmallow.pre_load`, `marshmallow.post_load`, `marshmallow.validates_schema`,
|
|
95
|
+
receive ``unknown`` as a keyword argument (:pr:`1632`).
|
|
96
|
+
Thanks :user:`jforand` for the PR.
|
|
97
|
+
- *Backwards-incompatible*: Arguments to `decorators <marshmallow.decorators>` are keyword-only arguments.
|
|
98
|
+
- *Backwards-incompatible*: Rename ``json_data`` parameter of `marshmallow.Schema.loads` to ``s``
|
|
99
|
+
for compatibility with most render module implementations (`json`, ``simplejson``, etc.) (:pr:`2764`).
|
|
100
|
+
Also make it a positional-only argument.
|
|
101
|
+
- Incorrectly declaring a field using a field class rather than instance
|
|
102
|
+
errors at class declaration time (previously happended when the schema was instantiated) (:pr:`2772`).
|
|
103
|
+
- Passing invalid values for ``unknown`` will cause an error in type checkers (:pr:`2771`).
|
|
104
|
+
|
|
105
|
+
Deprecations/Removals:
|
|
106
|
+
|
|
107
|
+
- *Backwards-incompatible*: Remove implicit field creation, i.e. using the ``fields`` or ``additional`` class Meta options with undeclared fields (:issue:`1356`).
|
|
108
|
+
- The `ordered` class Meta option is removed (:issue:`2146`). Field order is already preserved by default.
|
|
109
|
+
Set `Schema.dict_class` to `OrderedDict` to maintain the previous behavior.
|
|
110
|
+
- The `marshmallow.base` module is removed (:pr:`2722`).
|
|
111
|
+
|
|
112
|
+
Previously-deprecated APIs have been removed, including:
|
|
113
|
+
|
|
114
|
+
- The ``ordered`` `class Meta <marshmallow.Schema.Meta>` option is removed (:issue:`2146`) (deprecated in 3.26.0).
|
|
115
|
+
- *Backwards-incompatible*: `marshmallow.fields.Number` is no longer usable as a field in a schema (deprecated in 3.24.0).
|
|
116
|
+
Use `marshmallow.fields.Integer`, `marshmallow.fields.Float`, or `marshmallow.fields.Decimal` instead.
|
|
117
|
+
- *Backwards-incompatible*: `marshmallow.fields.Mapping` is no longer usable as a field in a schema (deprecated in 3.24.0).
|
|
118
|
+
- *Backwards-incompatible*: Custom validators must raise a `ValidationError <marshmallow.exceptions.ValidationError>` for invalid values (deprecated in 3.24.0).
|
|
119
|
+
Returning `False` is no longer supported (:issue:`1775`).
|
|
120
|
+
Use `marshmallow.fields.Dict` instead.
|
|
121
|
+
- Remove ``__version__``, ``__parsed_version__``, and ``__version_info__`` attributes (deprecated in 3.21.0).
|
|
122
|
+
- `default` and `missing` parameters, which were replaced by `dump_default` and `load_default` in 3.13.0 (:pr:`1742`, :pr:`2700`).
|
|
123
|
+
- Passing field metadata via keyword arguments (deprecated in 3.10.0). Use the explicit ``metadata=...``
|
|
124
|
+
argument instead (:issue:`1350`).
|
|
125
|
+
- `marshmallow.utils.pprint` (deprecated in 3.7.0). Use `pprint.pprint` instead.
|
|
126
|
+
- Passing `"self"` to `fields.Nested` (deprecated in 3.3.0). Use a callable instead.
|
|
127
|
+
- ``Field.fail``, which was replaced by ``Field.make_error`` in 3.0.0.
|
|
128
|
+
- `json_module` class Meta option (deprecated in 3.0.0b3). Use `render_module` instead.
|
|
129
|
+
|
|
130
|
+
3.26.1 (2025-02-03)
|
|
131
|
+
*******************
|
|
132
|
+
|
|
133
|
+
Bug fixes:
|
|
134
|
+
|
|
135
|
+
- Typing: Fix type annotations for `class Meta <marshmallow.Schema.Meta>` options (:issue:`2804`).
|
|
136
|
+
Thanks :user:`lawrence-law` for reporting.
|
|
137
|
+
|
|
138
|
+
Other changes:
|
|
139
|
+
|
|
140
|
+
- Remove default value for the ``data`` param of `Nested._deserialize <marshmallow.fields.Nested._deserialize>` (:issue:`2802`).
|
|
141
|
+
Thanks :user:`gbenson` for reporting.
|
|
142
|
+
|
|
143
|
+
|
|
4
144
|
3.26.0 (2025-01-22)
|
|
5
145
|
*******************
|
|
6
146
|
|
|
@@ -95,7 +235,7 @@ Features:
|
|
|
95
235
|
|
|
96
236
|
Bug fixes:
|
|
97
237
|
|
|
98
|
-
- Typing: Fix type hint for ``nested`` parameter of `Nested <marshmallow.fields.Nested
|
|
238
|
+
- Typing: Fix type hint for ``nested`` parameter of `Nested <marshmallow.fields.Nested>` (:pr:`2721`).
|
|
99
239
|
|
|
100
240
|
Deprecations:
|
|
101
241
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: marshmallow
|
|
3
|
-
Version:
|
|
3
|
+
Version: 4.0.0
|
|
4
4
|
Summary: A lightweight library for converting complex datatypes to and from native Python datatypes.
|
|
5
5
|
Author-email: Steven Loria <sloria1@gmail.com>
|
|
6
6
|
Maintainer-email: Steven Loria <sloria1@gmail.com>, Jérôme Lafréchoux <jerome@jolimont.fr>, Jared Deckard <jared@shademaps.com>
|
|
@@ -15,16 +15,18 @@ Classifier: Programming Language :: Python :: 3.10
|
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.11
|
|
16
16
|
Classifier: Programming Language :: Python :: 3.12
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
-
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: backports-datetime-fromisoformat; python_version < '3.11'
|
|
20
|
+
Requires-Dist: typing-extensions; python_version < '3.11'
|
|
19
21
|
Requires-Dist: marshmallow[tests] ; extra == "dev"
|
|
20
22
|
Requires-Dist: tox ; extra == "dev"
|
|
21
23
|
Requires-Dist: pre-commit>=3.5,<5.0 ; extra == "dev"
|
|
22
24
|
Requires-Dist: autodocsumm==0.2.14 ; extra == "docs"
|
|
23
25
|
Requires-Dist: furo==2024.8.6 ; extra == "docs"
|
|
24
26
|
Requires-Dist: sphinx-copybutton==0.5.2 ; extra == "docs"
|
|
25
|
-
Requires-Dist: sphinx-issues==5.0.
|
|
26
|
-
Requires-Dist: sphinx==8.
|
|
27
|
-
Requires-Dist: sphinxext-opengraph==0.
|
|
27
|
+
Requires-Dist: sphinx-issues==5.0.1 ; extra == "docs"
|
|
28
|
+
Requires-Dist: sphinx==8.2.3 ; extra == "docs"
|
|
29
|
+
Requires-Dist: sphinxext-opengraph==0.10.0 ; extra == "docs"
|
|
28
30
|
Requires-Dist: pytest ; extra == "tests"
|
|
29
31
|
Requires-Dist: simplejson ; extra == "tests"
|
|
30
32
|
Project-URL: Changelog, https://marshmallow.readthedocs.io/en/latest/changelog.html
|
|
@@ -37,7 +37,8 @@ html_theme_options = {
|
|
|
37
37
|
"light_logo": "marshmallow-logo-with-title.png",
|
|
38
38
|
"dark_logo": "marshmallow-logo-with-title-for-dark-theme.png",
|
|
39
39
|
"source_repository": "https://github.com/marshmallow-code/marshmallow",
|
|
40
|
-
"
|
|
40
|
+
"announcement": 'This is the documentation for the unreleased 4.0 version. The latest v3 docs are <a href="https://marshmallow.readthedocs.io/en/3.x-line/">here</a>.',
|
|
41
|
+
"source_branch": "dev",
|
|
41
42
|
"source_directory": "docs/",
|
|
42
43
|
"sidebar_hide_name": True,
|
|
43
44
|
"light_css_variables": {
|
|
@@ -13,13 +13,14 @@ Creating a field class
|
|
|
13
13
|
----------------------
|
|
14
14
|
|
|
15
15
|
To create a custom field class, create a subclass of :class:`marshmallow.fields.Field` and implement its :meth:`_serialize <marshmallow.fields.Field._serialize>` and/or :meth:`_deserialize <marshmallow.fields.Field._deserialize>` methods.
|
|
16
|
+
Field's type argument is the internal type, i.e. the type that the field deserializes to.
|
|
16
17
|
|
|
17
18
|
.. code-block:: python
|
|
18
19
|
|
|
19
20
|
from marshmallow import fields, ValidationError
|
|
20
21
|
|
|
21
22
|
|
|
22
|
-
class PinCode(fields.Field):
|
|
23
|
+
class PinCode(fields.Field[list[int]]):
|
|
23
24
|
"""Field that serializes to a string of numbers and deserializes
|
|
24
25
|
to a list of numbers.
|
|
25
26
|
"""
|
|
@@ -94,45 +95,72 @@ Both :class:`Function <marshmallow.fields.Function>` and :class:`Method <marshma
|
|
|
94
95
|
result = schema.load({"balance": "100.00"})
|
|
95
96
|
result["balance"] # => 100.0
|
|
96
97
|
|
|
97
|
-
..
|
|
98
|
+
.. _using_context:
|
|
98
99
|
|
|
99
|
-
|
|
100
|
-
|
|
100
|
+
Using context
|
|
101
|
+
-------------
|
|
101
102
|
|
|
102
|
-
|
|
103
|
+
A field may need information about its environment to know how to (de)serialize a value.
|
|
103
104
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
marshmallow 4 will also provide an `experimental helper API <https://marshmallow.readthedocs.io/en/latest/marshmallow.experimental.context.html>`_
|
|
107
|
-
for using context.
|
|
105
|
+
You can use the experimental `Context <marshmallow.experimental.context.Context>` class
|
|
106
|
+
to set and retrieve context.
|
|
108
107
|
|
|
109
|
-
|
|
108
|
+
Let's say your ``UserSchema`` needs to output
|
|
109
|
+
whether or not a ``User`` is the author of a ``Blog`` or
|
|
110
|
+
whether a certain word appears in a ``Blog's`` title.
|
|
110
111
|
|
|
111
|
-
|
|
112
|
+
.. code-block:: python
|
|
112
113
|
|
|
113
|
-
|
|
114
|
+
import typing
|
|
115
|
+
from dataclasses import dataclass
|
|
116
|
+
|
|
117
|
+
from marshmallow import Schema, fields
|
|
118
|
+
from marshmallow.experimental.context import Context
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@dataclass
|
|
122
|
+
class User:
|
|
123
|
+
name: str
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@dataclass
|
|
127
|
+
class Blog:
|
|
128
|
+
title: str
|
|
129
|
+
author: User
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
class ContextDict(typing.TypedDict):
|
|
133
|
+
blog: Blog
|
|
114
134
|
|
|
115
|
-
.. code-block:: python
|
|
116
135
|
|
|
117
136
|
class UserSchema(Schema):
|
|
118
137
|
name = fields.String()
|
|
119
|
-
|
|
120
|
-
is_author = fields.Function(
|
|
138
|
+
|
|
139
|
+
is_author = fields.Function(
|
|
140
|
+
lambda user: user == Context[ContextDict].get()["blog"].author
|
|
141
|
+
)
|
|
121
142
|
likes_bikes = fields.Method("writes_about_bikes")
|
|
122
143
|
|
|
123
|
-
def writes_about_bikes(self, user):
|
|
124
|
-
return "bicycle" in
|
|
144
|
+
def writes_about_bikes(self, user: User) -> bool:
|
|
145
|
+
return "bicycle" in Context[ContextDict].get()["blog"].title.lower()
|
|
125
146
|
|
|
147
|
+
.. note::
|
|
148
|
+
You can use `Context.get <marshmallow.experimental.context.Context.get>`
|
|
149
|
+
within custom fields, pre-/post-processing methods, and validators.
|
|
150
|
+
|
|
151
|
+
When (de)serializing, set the context by using `Context <marshmallow.experimental.context.Context>` as a context manager.
|
|
152
|
+
|
|
153
|
+
.. code-block:: python
|
|
126
154
|
|
|
127
|
-
schema = UserSchema()
|
|
128
155
|
|
|
129
156
|
user = User("Freddie Mercury", "fred@queen.com")
|
|
130
157
|
blog = Blog("Bicycle Blog", author=user)
|
|
131
158
|
|
|
132
|
-
schema
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
159
|
+
schema = UserSchema()
|
|
160
|
+
with Context({"blog": blog}):
|
|
161
|
+
result = schema.dump(user)
|
|
162
|
+
print(result["is_author"]) # => True
|
|
163
|
+
print(result["likes_bikes"]) # => True
|
|
136
164
|
|
|
137
165
|
|
|
138
166
|
Customizing error messages
|
|
@@ -199,7 +199,7 @@ Correspondingly, a representation of a ``Book`` will include its author represen
|
|
|
199
199
|
|
|
200
200
|
.. code-block:: python
|
|
201
201
|
|
|
202
|
-
from
|
|
202
|
+
from pprint import pprint
|
|
203
203
|
from mymodels import Author, Book
|
|
204
204
|
|
|
205
205
|
author = Author(name="William Faulkner")
|
|
@@ -244,7 +244,7 @@ There are a number of built-in validators in the :ref:`marshmallow.validate <api
|
|
|
244
244
|
You may implement your own validators.
|
|
245
245
|
A validator is a callable that accepts a single argument, the value to validate.
|
|
246
246
|
If validation fails, the callable should raise a :exc:`ValidationError <marshmallow.exceptions.ValidationError>`
|
|
247
|
-
with
|
|
247
|
+
with an error message.
|
|
248
248
|
|
|
249
249
|
.. code-block:: python
|
|
250
250
|
|
|
@@ -290,7 +290,7 @@ You may also pass a collection (list, tuple, generator) of callables to ``valida
|
|
|
290
290
|
Field validators as methods
|
|
291
291
|
+++++++++++++++++++++++++++
|
|
292
292
|
|
|
293
|
-
It is sometimes convenient to write validators as methods. Use the `validates <marshmallow.
|
|
293
|
+
It is sometimes convenient to write validators as methods. Use the `validates <marshmallow.validates>` decorator to register field validator methods.
|
|
294
294
|
|
|
295
295
|
.. code-block:: python
|
|
296
296
|
|
|
@@ -301,12 +301,30 @@ It is sometimes convenient to write validators as methods. Use the `validates <m
|
|
|
301
301
|
quantity = fields.Integer()
|
|
302
302
|
|
|
303
303
|
@validates("quantity")
|
|
304
|
-
def validate_quantity(self, value):
|
|
304
|
+
def validate_quantity(self, value: int, data_key: str) -> None:
|
|
305
305
|
if value < 0:
|
|
306
306
|
raise ValidationError("Quantity must be greater than 0.")
|
|
307
307
|
if value > 30:
|
|
308
308
|
raise ValidationError("Quantity must not be greater than 30.")
|
|
309
309
|
|
|
310
|
+
.. note::
|
|
311
|
+
|
|
312
|
+
You can pass multiple field names to the `validates <marshmallow.validates>` decorator.
|
|
313
|
+
|
|
314
|
+
.. code-block:: python
|
|
315
|
+
|
|
316
|
+
from marshmallow import Schema, fields, validates, ValidationError
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
class UserSchema(Schema):
|
|
320
|
+
name = fields.Str(required=True)
|
|
321
|
+
nickname = fields.Str(required=True)
|
|
322
|
+
|
|
323
|
+
@validates("name", "nickname")
|
|
324
|
+
def validate_names(self, value: str, data_key: str) -> None:
|
|
325
|
+
if len(value) < 3:
|
|
326
|
+
raise ValidationError("Too short")
|
|
327
|
+
|
|
310
328
|
|
|
311
329
|
Required fields
|
|
312
330
|
---------------
|
|
@@ -529,60 +547,6 @@ If you are consuming and producing data that does not match your schema, you can
|
|
|
529
547
|
# 'email': 'foo@bar.com'}
|
|
530
548
|
|
|
531
549
|
|
|
532
|
-
.. _meta_options:
|
|
533
|
-
|
|
534
|
-
Implicit field creation
|
|
535
|
-
-----------------------
|
|
536
|
-
|
|
537
|
-
.. warning::
|
|
538
|
-
|
|
539
|
-
Implicit field creation is deprecated and is removed in marshmallow 4.
|
|
540
|
-
Fields should be declared explicitly.
|
|
541
|
-
|
|
542
|
-
.. code-block:: python
|
|
543
|
-
|
|
544
|
-
# 3.x
|
|
545
|
-
class UserSchema(Schema):
|
|
546
|
-
class Meta:
|
|
547
|
-
fields = ("name", "birthdate")
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
# 4.x
|
|
551
|
-
class UserSchema(Schema):
|
|
552
|
-
name = fields.String()
|
|
553
|
-
email = fields.Date()
|
|
554
|
-
|
|
555
|
-
When your model has many attributes, specifying the field type for every attribute can get repetitive, especially when many of the attributes are already native Python datatypes.
|
|
556
|
-
|
|
557
|
-
The ``fields`` option allows you to specify implicitly-created fields. marshmallow will choose an appropriate field type based on the attribute's type.
|
|
558
|
-
|
|
559
|
-
Let's refactor our User schema to be more concise.
|
|
560
|
-
|
|
561
|
-
.. code-block:: python
|
|
562
|
-
|
|
563
|
-
class UserSchema(Schema):
|
|
564
|
-
uppername = fields.Function(lambda obj: obj.name.upper())
|
|
565
|
-
|
|
566
|
-
class Meta:
|
|
567
|
-
fields = ("name", "email", "created_at", "uppername")
|
|
568
|
-
|
|
569
|
-
Note that ``name`` will be automatically formatted as a :class:`String <marshmallow.fields.String>` and ``created_at`` will be formatted as a :class:`DateTime <marshmallow.fields.DateTime>`.
|
|
570
|
-
|
|
571
|
-
.. note::
|
|
572
|
-
|
|
573
|
-
If instead you want to specify which field names to include *in addition* to the explicitly declared fields, you can use the ``additional`` option.
|
|
574
|
-
|
|
575
|
-
The schema below is equivalent to above:
|
|
576
|
-
|
|
577
|
-
.. code-block:: python
|
|
578
|
-
|
|
579
|
-
class UserSchema(Schema):
|
|
580
|
-
uppername = fields.Function(lambda obj: obj.name.upper())
|
|
581
|
-
|
|
582
|
-
class Meta:
|
|
583
|
-
# No need to include 'uppername'
|
|
584
|
-
additional = ("name", "email", "created_at")
|
|
585
|
-
|
|
586
550
|
Next steps
|
|
587
551
|
----------
|
|
588
552
|
- Need to represent relationships between objects? See the :doc:`nesting` page.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
Top-level API
|
|
2
|
+
=============
|
|
3
|
+
|
|
4
|
+
.. automodule:: marshmallow
|
|
5
|
+
:members:
|
|
6
|
+
:autosummary:
|
|
7
|
+
:exclude-members: OPTIONS_CLASS
|
|
8
|
+
|
|
9
|
+
.. Can't use :autodata: here due to Sphinx bug: https://github.com/sphinx-doc/sphinx/issues/6495
|
|
10
|
+
.. data:: missing
|
|
11
|
+
|
|
12
|
+
Singleton value that indicates that a field's value is missing from input
|
|
13
|
+
dict passed to `Schema.load <marshmallow.Schema.load>`. If the field's value is not required,
|
|
14
|
+
its ``default`` value is used.
|
|
15
|
+
|
|
16
|
+
Constants for ``unknown``
|
|
17
|
+
-------------------------
|
|
18
|
+
|
|
19
|
+
.. seealso:: :ref:`unknown`
|
|
20
|
+
|
|
21
|
+
.. data:: EXCLUDE
|
|
22
|
+
|
|
23
|
+
Indicates that fields that are not explicitly declared on a schema should be
|
|
24
|
+
excluded from the deserialized result.
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
.. data:: INCLUDE
|
|
28
|
+
|
|
29
|
+
Indicates that fields that are not explicitly declared on a schema should be
|
|
30
|
+
included from the deserialized result.
|
|
31
|
+
|
|
32
|
+
.. data:: RAISE
|
|
33
|
+
|
|
34
|
+
Indicates that fields that are not explicitly declared on a schema should
|
|
35
|
+
result in an error.
|