marshmallow 3.26.1__tar.gz → 4.0.1__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.
Files changed (100) hide show
  1. {marshmallow-3.26.1 → marshmallow-4.0.1}/CHANGELOG.rst +135 -2
  2. {marshmallow-3.26.1 → marshmallow-4.0.1}/CONTRIBUTING.rst +1 -2
  3. {marshmallow-3.26.1 → marshmallow-4.0.1}/PKG-INFO +9 -7
  4. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/api_reference.rst +1 -0
  5. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/conf.py +2 -2
  6. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/custom_fields.rst +50 -22
  7. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/extending/index.rst +0 -1
  8. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/install.rst +0 -2
  9. marshmallow-4.0.1/docs/marshmallow.experimental.context.rst +5 -0
  10. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/nesting.rst +1 -1
  11. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/quickstart.rst +21 -57
  12. marshmallow-4.0.1/docs/top_level.rst +35 -0
  13. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/upgrading.rst +333 -0
  14. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/why.rst +0 -33
  15. {marshmallow-3.26.1 → marshmallow-4.0.1}/pyproject.toml +14 -10
  16. marshmallow-4.0.1/src/marshmallow/__init__.py +30 -0
  17. marshmallow-4.0.1/src/marshmallow/constants.py +22 -0
  18. {marshmallow-3.26.1 → marshmallow-4.0.1}/src/marshmallow/decorators.py +56 -40
  19. {marshmallow-3.26.1 → marshmallow-4.0.1}/src/marshmallow/exceptions.py +3 -4
  20. marshmallow-4.0.1/src/marshmallow/experimental/__init__.py +5 -0
  21. marshmallow-4.0.1/src/marshmallow/experimental/context.py +73 -0
  22. {marshmallow-3.26.1 → marshmallow-4.0.1}/src/marshmallow/fields.py +385 -456
  23. {marshmallow-3.26.1 → marshmallow-4.0.1}/src/marshmallow/orderedset.py +1 -1
  24. {marshmallow-3.26.1 → marshmallow-4.0.1}/src/marshmallow/schema.py +135 -197
  25. {marshmallow-3.26.1 → marshmallow-4.0.1}/src/marshmallow/types.py +14 -6
  26. marshmallow-4.0.1/src/marshmallow/utils.py +170 -0
  27. {marshmallow-3.26.1 → marshmallow-4.0.1}/src/marshmallow/validate.py +2 -16
  28. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/base.py +20 -90
  29. marshmallow-4.0.1/tests/test_context.py +251 -0
  30. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/test_decorators.py +100 -63
  31. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/test_deserialization.py +161 -143
  32. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/test_fields.py +75 -132
  33. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/test_options.py +9 -138
  34. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/test_registry.py +27 -12
  35. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/test_schema.py +96 -551
  36. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/test_serialization.py +41 -99
  37. marshmallow-4.0.1/tests/test_utils.py +141 -0
  38. marshmallow-3.26.1/docs/extending/using_context.rst +0 -20
  39. marshmallow-3.26.1/docs/top_level.rst +0 -12
  40. marshmallow-3.26.1/src/marshmallow/__init__.py +0 -81
  41. marshmallow-3.26.1/src/marshmallow/base.py +0 -61
  42. marshmallow-3.26.1/src/marshmallow/utils.py +0 -380
  43. marshmallow-3.26.1/src/marshmallow/warnings.py +0 -10
  44. marshmallow-3.26.1/tests/test_utils.py +0 -283
  45. marshmallow-3.26.1/tests/test_version_attributes.py +0 -15
  46. {marshmallow-3.26.1 → marshmallow-4.0.1}/LICENSE +0 -0
  47. {marshmallow-3.26.1 → marshmallow-4.0.1}/NOTICE +0 -0
  48. {marshmallow-3.26.1 → marshmallow-4.0.1}/README.rst +0 -0
  49. {marshmallow-3.26.1 → marshmallow-4.0.1}/SECURITY.md +0 -0
  50. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/.gitignore +0 -0
  51. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/_static/apple-touch-icon.png +0 -0
  52. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/_static/custom.css +0 -0
  53. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/_static/favicon.ico +0 -0
  54. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/_static/marshmallow-logo-200.png +0 -0
  55. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/_static/marshmallow-logo-with-title-for-dark-theme.png +0 -0
  56. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/_static/marshmallow-logo-with-title.png +0 -0
  57. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/_static/marshmallow-logo.png +0 -0
  58. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/authors.rst +0 -0
  59. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/changelog.rst +0 -0
  60. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/code_of_conduct.rst +0 -0
  61. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/contributing.rst +0 -0
  62. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/dashing.json +0 -0
  63. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/donate.rst +0 -0
  64. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/examples/index.rst +0 -0
  65. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/examples/inflection.rst +0 -0
  66. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/examples/quotes_api.rst +0 -0
  67. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/examples/validating_package_json.rst +0 -0
  68. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/extending/custom_error_handling.rst +0 -0
  69. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/extending/custom_error_messages.rst +0 -0
  70. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/extending/custom_options.rst +0 -0
  71. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/extending/overriding_attribute_access.rst +0 -0
  72. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/extending/pre_and_post_processing_methods.rst +0 -0
  73. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/extending/schema_validation.rst +0 -0
  74. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/extending/using_original_input_data.rst +0 -0
  75. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/index.rst +0 -0
  76. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/kudos.rst +0 -0
  77. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/license.rst +0 -0
  78. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/marshmallow.class_registry.rst +0 -0
  79. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/marshmallow.decorators.rst +0 -0
  80. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/marshmallow.error_store.rst +0 -0
  81. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/marshmallow.exceptions.rst +0 -0
  82. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/marshmallow.fields.rst +0 -0
  83. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/marshmallow.schema.rst +0 -0
  84. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/marshmallow.types.rst +0 -0
  85. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/marshmallow.utils.rst +0 -0
  86. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/marshmallow.validate.rst +0 -0
  87. {marshmallow-3.26.1 → marshmallow-4.0.1}/docs/whos_using.rst +0 -0
  88. {marshmallow-3.26.1 → marshmallow-4.0.1}/src/marshmallow/class_registry.py +0 -0
  89. {marshmallow-3.26.1 → marshmallow-4.0.1}/src/marshmallow/error_store.py +0 -0
  90. {marshmallow-3.26.1 → marshmallow-4.0.1}/src/marshmallow/py.typed +0 -0
  91. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/__init__.py +0 -0
  92. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/conftest.py +0 -0
  93. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/foo_serializer.py +0 -0
  94. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/mypy_test_cases/test_class_registry.py +0 -0
  95. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/mypy_test_cases/test_schema.py +0 -0
  96. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/mypy_test_cases/test_validation_error.py +0 -0
  97. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/test_error_store.py +0 -0
  98. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/test_exceptions.py +0 -0
  99. {marshmallow-3.26.1 → marshmallow-4.0.1}/tests/test_validate.py +0 -0
  100. {marshmallow-3.26.1 → marshmallow-4.0.1}/tox.ini +0 -0
@@ -1,6 +1,139 @@
1
1
  Changelog
2
2
  ---------
3
3
 
4
+ 4.0.1 (2025-08-28)
5
+ ++++++++++++++++++
6
+
7
+ Bug fixes:
8
+
9
+ - Fix wildcard import of ``from marshmallow import *`` (:pr:`2823`).
10
+ Thanks :user:`Florian-Laport` for the PR.
11
+
12
+
13
+ 4.0.0 (2025-04-16)
14
+ ******************
15
+
16
+ See :ref:`upgrading_4_0` for a guide on updating your code.
17
+
18
+ Features:
19
+
20
+ - Typing: Add types to all `Field <marshmallow.fields.Field>` constructor kwargs (:issue:`2285`).
21
+ Thanks :user:`navignaw` for the suggestion.
22
+ - `DateTime <marshmallow.fields.DateTime>`, `Date <marshmallow.fields.Date>`, `Time <marshmallow.fields.Time>`,
23
+ `TimeDelta <marshmallow.fields.TimeDelta>`, and `Enum <marshmallow.fields.Enum>`
24
+ accept their internal value types as valid input (:issue:`1415`).
25
+ Thanks :user:`bitdancer` for the suggestion.
26
+ - `@validates <marshmallow.validates>` accepts multiple field names (:issue:`1960`).
27
+ *Backwards-incompatible*: Decorated methods now receive ``data_key`` as a keyword argument.
28
+ Thanks :user:`dpriskorn` for the suggestion and :user:`dharani7998` for the PR.
29
+
30
+ Other changes:
31
+
32
+ - Typing: `Field <marshmallow.fields.Field>` is now a generic type with a type argument for the internal value type.
33
+ - `marshmallow.fields.UUID` no longer subclasses `marshmallow.fields.String`.
34
+ - `marshmallow.Schema.load` no longer silently fails to call schema validators when a generator is passed (:issue:`1898`).
35
+ The typing of `data` is also updated to be more accurate.
36
+ Thanks :user:`ziplokk1` for reporting.
37
+ - *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`).
38
+ As a consequence of this change:
39
+ - Time with time offsets are now supported.
40
+ - YYYY-MM-DD is now accepted as a datetime and deserialized as naive 00:00 AM.
41
+ - `from_iso_date`, `from_iso_time` and `from_iso_datetime` are removed from `marshmallow.utils`.
42
+ - Remove `isoformat`, `to_iso_time` and `to_iso_datetime` from `marshmallow.utils` (:pr:`2766`).
43
+ - Remove `from_rfc`, and `rfcformat` from `marshmallow.utils` (:pr:`2767`).
44
+ - Remove `is_keyed_tuple` from `marshmallow.utils` (:pr:`2768`).
45
+ - Remove `get_fixed_timezone` from `marshmallow.utils` (:pr:`2773`).
46
+
47
+ - *Backwards-incompatible*: `marshmallow.fields.Boolean` no longer serializes non-boolean values (:pr:`2725`).
48
+ - *Backwards-incompatible*: Rename ``schema`` parameter to ``parent`` in `marshmallow.fields.Field._bind_to_schema` (:issue:`1360`).
49
+ - *Backwards-incompatible*: Rename ``pass_many`` parameter to ``pass_collection`` in pre/post processing methods (:issue:`1369`).
50
+ - *Backwards-incompatible*: `marshmallow.fields.TimeDelta` no longer truncates float values when
51
+ deserializing (:pr:`2654`). This allows microseconds to be preserved, e.g.
52
+
53
+ .. code-block:: python
54
+
55
+ from marshmallow import fields
56
+
57
+ field = fields.TimeDelta()
58
+
59
+ # Before
60
+ field.deserialize(12.9)
61
+ datetime.timedelta(seconds=12)
62
+ # datetime.timedelta(seconds=12)
63
+
64
+ # After
65
+ field.deserialize(12.9)
66
+ # datetime.timedelta(seconds=12, microseconds=900000)
67
+
68
+ - Improve performance and minimize float precision loss of `marshmallow.fields.TimeDelta` serialization (:pr:`2654`).
69
+ - *Backwards-incompatible*: Remove ``serialization_type`` parameter from
70
+ `marshmallow.fields.TimeDelta` (:pr:`2654`).
71
+
72
+ Thanks :user:`ddelange` for the PR.
73
+
74
+ - *Backwards-incompatible*: Remove `Schema <marshmallow.schema.Schema>`'s ``context`` attribute (deprecated since 3.24.0). Passing a context
75
+ should be done using `contextvars.ContextVar` (:issue:`1826`).
76
+ marshmallow 4 provides an experimental `Context <marshmallow.experimental.context.Context>`
77
+ manager class that can be used to both set and retrieve context.
78
+
79
+ .. code-block:: python
80
+
81
+ import typing
82
+
83
+ from marshmallow import Schema, fields
84
+ from marshmallow.experimental.context import Context
85
+
86
+
87
+ class UserContext(typing.TypedDict):
88
+ suffix: str
89
+
90
+
91
+ class UserSchema(Schema):
92
+ name_suffixed = fields.Function(
93
+ lambda obj: obj["name"] + Context[UserContext].get()["suffix"]
94
+ )
95
+
96
+
97
+ with Context[UserContext]({"suffix": "bar"}):
98
+ UserSchema().dump({"name": "foo"})
99
+ # {'name_suffixed': 'foobar'}
100
+
101
+ - Methods decorated with `marshmallow.pre_load`, `marshmallow.post_load`, `marshmallow.validates_schema`,
102
+ receive ``unknown`` as a keyword argument (:pr:`1632`).
103
+ Thanks :user:`jforand` for the PR.
104
+ - *Backwards-incompatible*: Arguments to `decorators <marshmallow.decorators>` are keyword-only arguments.
105
+ - *Backwards-incompatible*: Rename ``json_data`` parameter of `marshmallow.Schema.loads` to ``s``
106
+ for compatibility with most render module implementations (`json`, ``simplejson``, etc.) (:pr:`2764`).
107
+ Also make it a positional-only argument.
108
+ - Incorrectly declaring a field using a field class rather than instance
109
+ errors at class declaration time (previously happended when the schema was instantiated) (:pr:`2772`).
110
+ - Passing invalid values for ``unknown`` will cause an error in type checkers (:pr:`2771`).
111
+
112
+ Deprecations/Removals:
113
+
114
+ - *Backwards-incompatible*: Remove implicit field creation, i.e. using the ``fields`` or ``additional`` class Meta options with undeclared fields (:issue:`1356`).
115
+ - The `ordered` class Meta option is removed (:issue:`2146`). Field order is already preserved by default.
116
+ Set `Schema.dict_class` to `OrderedDict` to maintain the previous behavior.
117
+ - The `marshmallow.base` module is removed (:pr:`2722`).
118
+
119
+ Previously-deprecated APIs have been removed, including:
120
+
121
+ - The ``ordered`` `class Meta <marshmallow.Schema.Meta>` option is removed (:issue:`2146`) (deprecated in 3.26.0).
122
+ - *Backwards-incompatible*: `marshmallow.fields.Number` is no longer usable as a field in a schema (deprecated in 3.24.0).
123
+ Use `marshmallow.fields.Integer`, `marshmallow.fields.Float`, or `marshmallow.fields.Decimal` instead.
124
+ - *Backwards-incompatible*: `marshmallow.fields.Mapping` is no longer usable as a field in a schema (deprecated in 3.24.0).
125
+ - *Backwards-incompatible*: Custom validators must raise a `ValidationError <marshmallow.exceptions.ValidationError>` for invalid values (deprecated in 3.24.0).
126
+ Returning `False` is no longer supported (:issue:`1775`).
127
+ Use `marshmallow.fields.Dict` instead.
128
+ - Remove ``__version__``, ``__parsed_version__``, and ``__version_info__`` attributes (deprecated in 3.21.0).
129
+ - `default` and `missing` parameters, which were replaced by `dump_default` and `load_default` in 3.13.0 (:pr:`1742`, :pr:`2700`).
130
+ - Passing field metadata via keyword arguments (deprecated in 3.10.0). Use the explicit ``metadata=...``
131
+ argument instead (:issue:`1350`).
132
+ - `marshmallow.utils.pprint` (deprecated in 3.7.0). Use `pprint.pprint` instead.
133
+ - Passing `"self"` to `fields.Nested` (deprecated in 3.3.0). Use a callable instead.
134
+ - ``Field.fail``, which was replaced by ``Field.make_error`` in 3.0.0.
135
+ - `json_module` class Meta option (deprecated in 3.0.0b3). Use `render_module` instead.
136
+
4
137
  3.26.1 (2025-02-03)
5
138
  *******************
6
139
 
@@ -26,7 +159,7 @@ Features:
26
159
 
27
160
  Bug fixes:
28
161
 
29
- - Respect ``data_key`` when schema validators raise a `ValidationError <marshmallow.exceptions.ValidationError>`
162
+ - Respect ``data_key`` when schema validators raise a `ValidationError <marshmallow.exceptions.ValidationError>`
30
163
  with a ``field_name`` argument (:issue:`2170`). Thanks :user:`matejsp` for reporting.
31
164
  - Correctly handle multiple `@post_load <marshmallow.post_load>` methods where one method appends to
32
165
  the data and another passes ``pass_original=True`` (:issue:`1755`).
@@ -109,7 +242,7 @@ Features:
109
242
 
110
243
  Bug fixes:
111
244
 
112
- - Typing: Fix type hint for ``nested`` parameter of `Nested <marshmallow.fields.Nested>`.
245
+ - Typing: Fix type hint for ``nested`` parameter of `Nested <marshmallow.fields.Nested>` (:pr:`2721`).
113
246
 
114
247
  Deprecations:
115
248
 
@@ -26,8 +26,7 @@ Ways to contribute
26
26
 
27
27
  - Comment on some of marshmallow's `open issues <https://github.com/marshmallow-code/marshmallow/issues>`_ (especially those `labeled "feedback welcome" <https://github.com/marshmallow-code/marshmallow/issues?q=is%3Aopen+is%3Aissue+label%3A%22feedback+welcome%22>`_). Share a solution or workaround. Make a suggestion for how a feature can be made better. Opinions are welcome!
28
28
  - Improve `the docs <https://marshmallow.readthedocs.io>`_.
29
- For straightforward edits,
30
- click the ReadTheDocs menu button in the bottom-right corner of the page and click "Edit".
29
+ For straightforward edits, click the edit button in the top-right corner of the page.
31
30
  See the :ref:`Documentation <contributing_documentation>` section of this page if you want to build the docs locally.
32
31
  - If you think you've found a bug, `open an issue <https://github.com/marshmallow-code/marshmallow/issues>`_.
33
32
  - Contribute an :ref:`example usage <contributing_examples>` of marshmallow.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: marshmallow
3
- Version: 3.26.1
3
+ Version: 4.0.1
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
- Requires-Dist: packaging>=17.0
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
- Requires-Dist: furo==2024.8.6 ; extra == "docs"
25
+ Requires-Dist: furo==2025.7.19 ; extra == "docs"
24
26
  Requires-Dist: sphinx-copybutton==0.5.2 ; extra == "docs"
25
- Requires-Dist: sphinx-issues==5.0.0 ; extra == "docs"
26
- Requires-Dist: sphinx==8.1.3 ; extra == "docs"
27
- Requires-Dist: sphinxext-opengraph==0.9.1 ; extra == "docs"
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.12.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
@@ -9,6 +9,7 @@
9
9
  marshmallow.decorators
10
10
  marshmallow.validate
11
11
  marshmallow.utils
12
+ marshmallow.experimental.context
12
13
  marshmallow.exceptions
13
14
 
14
15
  Private API
@@ -37,14 +37,14 @@ 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
- "source_branch": "3.x-line",
40
+ "source_branch": "dev",
41
41
  "source_directory": "docs/",
42
42
  "sidebar_hide_name": True,
43
43
  "light_css_variables": {
44
44
  # Serif system font stack: https://systemfontstack.com/
45
45
  "font-stack": "Iowan Old Style, Apple Garamond, Baskerville, Times New Roman, Droid Serif, Times, Source Serif Pro, serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol;",
46
46
  },
47
- "top_of_page_buttons": ["view"],
47
+ "top_of_page_buttons": ["view", "edit"],
48
48
  }
49
49
  pygments_dark_style = "lightbulb"
50
50
  html_favicon = "_static/favicon.ico"
@@ -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
- .. _adding-context:
98
+ .. _using_context:
98
99
 
99
- Adding context to `Method` and `Function` fields
100
- ------------------------------------------------
100
+ Using context
101
+ -------------
101
102
 
102
- .. warning::
103
+ A field may need information about its environment to know how to (de)serialize a value.
103
104
 
104
- The ``context`` attribute is deprecated and will be removed in marshmallow 4.
105
- Use `contextvars.ContextVar` for passing context to fields, pre-/post-processing methods, and validators instead.
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
- A :class:`Function <marshmallow.fields.Function>` or :class:`Method <marshmallow.fields.Method>` field may need information about its environment to know how to serialize a value.
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
- In these cases, you can set the ``context`` attribute (a dictionary) of a `Schema <marshmallow.Schema>`. :class:`Function <marshmallow.fields.Function>` and :class:`Method <marshmallow.fields.Method>` fields will have access to this dictionary.
112
+ .. code-block:: python
112
113
 
113
- As an example, you might want your ``UserSchema`` to output whether or not a ``User`` is the author of a ``Blog`` or whether a certain word appears in a ``Blog's`` title.
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
- # Function fields optionally receive context argument
120
- is_author = fields.Function(lambda user, context: user == context["blog"].author)
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 self.context["blog"].title.lower()
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.context = {"blog": blog}
133
- result = schema.dump(user)
134
- result["is_author"] # => True
135
- result["likes_bikes"] # => True
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
@@ -12,5 +12,4 @@ The guides below demonstrate how to extend schemas in various ways.
12
12
  overriding_attribute_access
13
13
  custom_error_handling
14
14
  custom_options
15
- using_context
16
15
  custom_error_messages
@@ -1,8 +1,6 @@
1
1
  Installation
2
2
  ============
3
3
 
4
- **marshmallow** has no external dependencies other than the `packaging` library.
5
-
6
4
  Installing/upgrading from the PyPI
7
5
  ----------------------------------
8
6
 
@@ -0,0 +1,5 @@
1
+ Context (experimental)
2
+ ======================
3
+
4
+ .. automodule:: marshmallow.experimental.context
5
+ :members:
@@ -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 marshmallow import pprint
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 a useful error message or return ``False`` (for a generic error message).
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.decorators.validates>` decorator to register field validator methods.
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.