marshmallow 3.25.0__py3-none-any.whl → 3.26.0__py3-none-any.whl
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/__init__.py +6 -6
- marshmallow/class_registry.py +8 -9
- marshmallow/decorators.py +16 -11
- marshmallow/error_store.py +3 -3
- marshmallow/fields.py +98 -81
- marshmallow/orderedset.py +1 -1
- marshmallow/schema.py +191 -101
- marshmallow/types.py +29 -0
- marshmallow/utils.py +20 -18
- marshmallow/validate.py +37 -23
- {marshmallow-3.25.0.dist-info → marshmallow-3.26.0.dist-info}/METADATA +16 -7
- marshmallow-3.26.0.dist-info/RECORD +18 -0
- marshmallow-3.25.0.dist-info/RECORD +0 -18
- {marshmallow-3.25.0.dist-info → marshmallow-3.26.0.dist-info}/LICENSE +0 -0
- {marshmallow-3.25.0.dist-info → marshmallow-3.26.0.dist-info}/WHEEL +0 -0
marshmallow/__init__.py
CHANGED
|
@@ -68,14 +68,14 @@ __all__ = [
|
|
|
68
68
|
"RAISE",
|
|
69
69
|
"Schema",
|
|
70
70
|
"SchemaOpts",
|
|
71
|
+
"ValidationError",
|
|
71
72
|
"fields",
|
|
72
|
-
"
|
|
73
|
-
"validates_schema",
|
|
74
|
-
"pre_dump",
|
|
73
|
+
"missing",
|
|
75
74
|
"post_dump",
|
|
76
|
-
"pre_load",
|
|
77
75
|
"post_load",
|
|
78
76
|
"pprint",
|
|
79
|
-
"
|
|
80
|
-
"
|
|
77
|
+
"pre_dump",
|
|
78
|
+
"pre_load",
|
|
79
|
+
"validates",
|
|
80
|
+
"validates_schema",
|
|
81
81
|
]
|
marshmallow/class_registry.py
CHANGED
|
@@ -7,6 +7,7 @@ class:`fields.Nested <marshmallow.fields.Nested>`.
|
|
|
7
7
|
This module is treated as private API.
|
|
8
8
|
Users should not need to use this module directly.
|
|
9
9
|
"""
|
|
10
|
+
# ruff: noqa: ERA001
|
|
10
11
|
|
|
11
12
|
from __future__ import annotations
|
|
12
13
|
|
|
@@ -49,7 +50,7 @@ def register(classname: str, cls: SchemaType) -> None:
|
|
|
49
50
|
module = cls.__module__
|
|
50
51
|
# Full module path to the class
|
|
51
52
|
# e.g. user.schemas.UserSchema
|
|
52
|
-
fullpath = "."
|
|
53
|
+
fullpath = f"{module}.{classname}"
|
|
53
54
|
# If the class is already registered; need to check if the entries are
|
|
54
55
|
# in the same module as cls to avoid having multiple instances of the same
|
|
55
56
|
# class in the registry
|
|
@@ -66,23 +67,22 @@ def register(classname: str, cls: SchemaType) -> None:
|
|
|
66
67
|
else:
|
|
67
68
|
# If fullpath does exist, replace existing entry
|
|
68
69
|
_registry[fullpath] = [cls]
|
|
69
|
-
return None
|
|
70
70
|
|
|
71
71
|
|
|
72
72
|
@typing.overload
|
|
73
|
-
def get_class(classname: str, all: typing.Literal[False] = ...) -> SchemaType: ...
|
|
73
|
+
def get_class(classname: str, *, all: typing.Literal[False] = ...) -> SchemaType: ...
|
|
74
74
|
|
|
75
75
|
|
|
76
76
|
@typing.overload
|
|
77
77
|
def get_class(
|
|
78
|
-
classname: str, all: typing.Literal[True] = ...
|
|
79
|
-
) -> list[SchemaType]
|
|
78
|
+
classname: str, *, all: typing.Literal[True] = ...
|
|
79
|
+
) -> list[SchemaType]: ...
|
|
80
80
|
|
|
81
81
|
|
|
82
|
-
def get_class(classname: str, all: bool = False) -> list[SchemaType] | SchemaType:
|
|
82
|
+
def get_class(classname: str, *, all: bool = False) -> list[SchemaType] | SchemaType: # noqa: A002
|
|
83
83
|
"""Retrieve a class from the registry.
|
|
84
84
|
|
|
85
|
-
:raises: marshmallow.exceptions.RegistryError if the class cannot be found
|
|
85
|
+
:raises: `marshmallow.exceptions.RegistryError` if the class cannot be found
|
|
86
86
|
or if there are multiple entries for the given class name.
|
|
87
87
|
"""
|
|
88
88
|
try:
|
|
@@ -100,5 +100,4 @@ def get_class(classname: str, all: bool = False) -> list[SchemaType] | SchemaTyp
|
|
|
100
100
|
"were found. Please use the full, "
|
|
101
101
|
"module-qualified path."
|
|
102
102
|
)
|
|
103
|
-
|
|
104
|
-
return _registry[classname][0]
|
|
103
|
+
return _registry[classname][0]
|
marshmallow/decorators.py
CHANGED
|
@@ -86,16 +86,16 @@ class MarshmallowHook:
|
|
|
86
86
|
def validates(field_name: str) -> Callable[..., Any]:
|
|
87
87
|
"""Register a field validator.
|
|
88
88
|
|
|
89
|
-
:param
|
|
89
|
+
:param field_name: Name of the field that the method validates.
|
|
90
90
|
"""
|
|
91
91
|
return set_hook(None, VALIDATES, field_name=field_name)
|
|
92
92
|
|
|
93
93
|
|
|
94
94
|
def validates_schema(
|
|
95
95
|
fn: Callable[..., Any] | None = None,
|
|
96
|
-
pass_many: bool = False,
|
|
97
|
-
pass_original: bool = False,
|
|
98
|
-
skip_on_field_errors: bool = True,
|
|
96
|
+
pass_many: bool = False, # noqa: FBT001, FBT002
|
|
97
|
+
pass_original: bool = False, # noqa: FBT001, FBT002
|
|
98
|
+
skip_on_field_errors: bool = True, # noqa: FBT001, FBT002
|
|
99
99
|
) -> Callable[..., Any]:
|
|
100
100
|
"""Register a schema-level validator.
|
|
101
101
|
|
|
@@ -126,7 +126,8 @@ def validates_schema(
|
|
|
126
126
|
|
|
127
127
|
|
|
128
128
|
def pre_dump(
|
|
129
|
-
fn: Callable[..., Any] | None = None,
|
|
129
|
+
fn: Callable[..., Any] | None = None,
|
|
130
|
+
pass_many: bool = False, # noqa: FBT001, FBT002
|
|
130
131
|
) -> Callable[..., Any]:
|
|
131
132
|
"""Register a method to invoke before serializing an object. The method
|
|
132
133
|
receives the object to be serialized and returns the processed object.
|
|
@@ -143,8 +144,8 @@ def pre_dump(
|
|
|
143
144
|
|
|
144
145
|
def post_dump(
|
|
145
146
|
fn: Callable[..., Any] | None = None,
|
|
146
|
-
pass_many: bool = False,
|
|
147
|
-
pass_original: bool = False,
|
|
147
|
+
pass_many: bool = False, # noqa: FBT001, FBT002
|
|
148
|
+
pass_original: bool = False, # noqa: FBT001, FBT002
|
|
148
149
|
) -> Callable[..., Any]:
|
|
149
150
|
"""Register a method to invoke after serializing an object. The method
|
|
150
151
|
receives the serialized object and returns the processed object.
|
|
@@ -163,7 +164,8 @@ def post_dump(
|
|
|
163
164
|
|
|
164
165
|
|
|
165
166
|
def pre_load(
|
|
166
|
-
fn: Callable[..., Any] | None = None,
|
|
167
|
+
fn: Callable[..., Any] | None = None,
|
|
168
|
+
pass_many: bool = False, # noqa: FBT001, FBT002
|
|
167
169
|
) -> Callable[..., Any]:
|
|
168
170
|
"""Register a method to invoke before deserializing an object. The method
|
|
169
171
|
receives the data to be deserialized and returns the processed data.
|
|
@@ -181,8 +183,8 @@ def pre_load(
|
|
|
181
183
|
|
|
182
184
|
def post_load(
|
|
183
185
|
fn: Callable[..., Any] | None = None,
|
|
184
|
-
pass_many: bool = False,
|
|
185
|
-
pass_original: bool = False,
|
|
186
|
+
pass_many: bool = False, # noqa: FBT001, FBT002
|
|
187
|
+
pass_original: bool = False, # noqa: FBT001, FBT002
|
|
186
188
|
) -> Callable[..., Any]:
|
|
187
189
|
"""Register a method to invoke after deserializing an object. The method
|
|
188
190
|
receives the deserialized data and returns the processed data.
|
|
@@ -202,7 +204,10 @@ def post_load(
|
|
|
202
204
|
|
|
203
205
|
|
|
204
206
|
def set_hook(
|
|
205
|
-
fn: Callable[..., Any] | None,
|
|
207
|
+
fn: Callable[..., Any] | None,
|
|
208
|
+
tag: str,
|
|
209
|
+
many: bool = False, # noqa: FBT001, FBT002
|
|
210
|
+
**kwargs: Any,
|
|
206
211
|
) -> Callable[..., Any]:
|
|
207
212
|
"""Mark decorated function as a hook to be picked up later.
|
|
208
213
|
You should not need to use this method directly.
|
marshmallow/error_store.py
CHANGED
|
@@ -25,7 +25,7 @@ class ErrorStore:
|
|
|
25
25
|
self.errors = merge_errors(self.errors, messages)
|
|
26
26
|
|
|
27
27
|
|
|
28
|
-
def merge_errors(errors1, errors2):
|
|
28
|
+
def merge_errors(errors1, errors2): # noqa: PLR0911
|
|
29
29
|
"""Deeply merge two error messages.
|
|
30
30
|
|
|
31
31
|
The format of ``errors1`` and ``errors2`` matches the ``message``
|
|
@@ -40,7 +40,7 @@ def merge_errors(errors1, errors2):
|
|
|
40
40
|
return errors1 + errors2
|
|
41
41
|
if isinstance(errors2, dict):
|
|
42
42
|
return dict(errors2, **{SCHEMA: merge_errors(errors1, errors2.get(SCHEMA))})
|
|
43
|
-
return errors1
|
|
43
|
+
return [*errors1, errors2]
|
|
44
44
|
if isinstance(errors1, dict):
|
|
45
45
|
if isinstance(errors2, list):
|
|
46
46
|
return dict(errors1, **{SCHEMA: merge_errors(errors1.get(SCHEMA), errors2)})
|
|
@@ -54,7 +54,7 @@ def merge_errors(errors1, errors2):
|
|
|
54
54
|
return errors
|
|
55
55
|
return dict(errors1, **{SCHEMA: merge_errors(errors1.get(SCHEMA), errors2)})
|
|
56
56
|
if isinstance(errors2, list):
|
|
57
|
-
return [errors1
|
|
57
|
+
return [errors1, *errors2]
|
|
58
58
|
if isinstance(errors2, dict):
|
|
59
59
|
return dict(errors2, **{SCHEMA: merge_errors(errors1, errors2.get(SCHEMA))})
|
|
60
60
|
return [errors1, errors2]
|
marshmallow/fields.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
# ruff: noqa: F841, SLF001
|
|
1
2
|
from __future__ import annotations
|
|
2
3
|
|
|
3
4
|
import collections
|
|
@@ -11,7 +12,6 @@ import typing
|
|
|
11
12
|
import uuid
|
|
12
13
|
import warnings
|
|
13
14
|
from collections.abc import Mapping as _Mapping
|
|
14
|
-
from enum import Enum as EnumType
|
|
15
15
|
|
|
16
16
|
from marshmallow import class_registry, types, utils, validate
|
|
17
17
|
from marshmallow.base import FieldABC
|
|
@@ -35,54 +35,54 @@ from marshmallow.warnings import (
|
|
|
35
35
|
)
|
|
36
36
|
|
|
37
37
|
if typing.TYPE_CHECKING:
|
|
38
|
+
from enum import Enum as EnumType
|
|
39
|
+
|
|
38
40
|
from marshmallow.schema import Schema, SchemaMeta
|
|
39
41
|
|
|
40
42
|
|
|
41
43
|
__all__ = [
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"Nested",
|
|
45
|
-
"Mapping",
|
|
46
|
-
"Dict",
|
|
47
|
-
"List",
|
|
48
|
-
"Tuple",
|
|
49
|
-
"String",
|
|
44
|
+
"IP",
|
|
45
|
+
"URL",
|
|
50
46
|
"UUID",
|
|
51
|
-
"Number",
|
|
52
|
-
"Integer",
|
|
53
|
-
"Decimal",
|
|
54
|
-
"Boolean",
|
|
55
|
-
"Float",
|
|
56
|
-
"DateTime",
|
|
57
|
-
"NaiveDateTime",
|
|
58
47
|
"AwareDateTime",
|
|
59
|
-
"
|
|
48
|
+
"Bool",
|
|
49
|
+
"Boolean",
|
|
50
|
+
"Constant",
|
|
60
51
|
"Date",
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
52
|
+
"DateTime",
|
|
53
|
+
"Decimal",
|
|
54
|
+
"Dict",
|
|
64
55
|
"Email",
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
56
|
+
"Enum",
|
|
57
|
+
"Field",
|
|
58
|
+
"Float",
|
|
59
|
+
"Function",
|
|
68
60
|
"IPInterface",
|
|
61
|
+
"IPv4",
|
|
69
62
|
"IPv4Interface",
|
|
63
|
+
"IPv6",
|
|
70
64
|
"IPv6Interface",
|
|
71
|
-
"Enum",
|
|
72
|
-
"Method",
|
|
73
|
-
"Function",
|
|
74
|
-
"Str",
|
|
75
|
-
"Bool",
|
|
76
65
|
"Int",
|
|
77
|
-
"
|
|
66
|
+
"Integer",
|
|
67
|
+
"List",
|
|
68
|
+
"Mapping",
|
|
69
|
+
"Method",
|
|
70
|
+
"NaiveDateTime",
|
|
71
|
+
"Nested",
|
|
72
|
+
"Number",
|
|
78
73
|
"Pluck",
|
|
74
|
+
"Raw",
|
|
75
|
+
"Str",
|
|
76
|
+
"String",
|
|
77
|
+
"Time",
|
|
78
|
+
"TimeDelta",
|
|
79
|
+
"Tuple",
|
|
80
|
+
"Url",
|
|
79
81
|
]
|
|
80
82
|
|
|
81
83
|
|
|
82
84
|
class Field(FieldABC):
|
|
83
|
-
"""
|
|
84
|
-
formatting by default, and should only be used in cases where
|
|
85
|
-
data does not need to be formatted before being serialized or deserialized.
|
|
85
|
+
"""Base field from which other fields inherit.
|
|
86
86
|
|
|
87
87
|
:param dump_default: If set, this value will be used during serialization if the
|
|
88
88
|
input value is missing. If not set, the field will be excluded from the
|
|
@@ -114,7 +114,7 @@ class Field(FieldABC):
|
|
|
114
114
|
:param dump_only: If `True` skip this field during deserialization, otherwise
|
|
115
115
|
its value will be present in the deserialized object. In the context of an
|
|
116
116
|
HTTP API, this effectively marks the field as "read-only".
|
|
117
|
-
:param
|
|
117
|
+
:param error_messages: Overrides for `Field.default_error_messages`.
|
|
118
118
|
:param metadata: Extra information to be stored as field metadata.
|
|
119
119
|
|
|
120
120
|
.. versionchanged:: 3.0.0b8
|
|
@@ -123,6 +123,10 @@ class Field(FieldABC):
|
|
|
123
123
|
|
|
124
124
|
.. versionchanged:: 3.13.0
|
|
125
125
|
Replace ``missing`` and ``default`` parameters with ``load_default`` and ``dump_default``.
|
|
126
|
+
|
|
127
|
+
.. versionchanged:: 3.24.0
|
|
128
|
+
`Field <marshmallow.fields.Field>` should no longer be used as a field within a `Schema <marshmallow.Schema>`.
|
|
129
|
+
Use `Raw <marshmallow.fields.Raw>` or another `Field <marshmallow.fields.Field>` subclass instead.
|
|
126
130
|
"""
|
|
127
131
|
|
|
128
132
|
# Some fields, such as Method fields and Function fields, are not expected
|
|
@@ -148,11 +152,7 @@ class Field(FieldABC):
|
|
|
148
152
|
default: typing.Any = missing_,
|
|
149
153
|
data_key: str | None = None,
|
|
150
154
|
attribute: str | None = None,
|
|
151
|
-
validate:
|
|
152
|
-
typing.Callable[[typing.Any], typing.Any]
|
|
153
|
-
| typing.Iterable[typing.Callable[[typing.Any], typing.Any]]
|
|
154
|
-
| None
|
|
155
|
-
) = None,
|
|
155
|
+
validate: types.Validator | typing.Iterable[types.Validator] | None = None,
|
|
156
156
|
required: bool = False,
|
|
157
157
|
allow_none: bool | None = None,
|
|
158
158
|
load_only: bool = False,
|
|
@@ -260,9 +260,9 @@ class Field(FieldABC):
|
|
|
260
260
|
):
|
|
261
261
|
"""Return the value for a given key from an object.
|
|
262
262
|
|
|
263
|
-
:param
|
|
264
|
-
:param
|
|
265
|
-
:param
|
|
263
|
+
:param obj: The object to get the value from.
|
|
264
|
+
:param attr: The attribute/key in `obj` to get the value from.
|
|
265
|
+
:param accessor: A callable used to retrieve the value of `attr` from
|
|
266
266
|
the object `obj`. Defaults to `marshmallow.utils.get_value`.
|
|
267
267
|
"""
|
|
268
268
|
accessor_func = accessor or utils.get_value
|
|
@@ -381,8 +381,8 @@ class Field(FieldABC):
|
|
|
381
381
|
"""Update field with values from its parent schema. Called by
|
|
382
382
|
`Schema._bind_field <marshmallow.Schema._bind_field>`.
|
|
383
383
|
|
|
384
|
-
:param
|
|
385
|
-
:param
|
|
384
|
+
:param field_name: Field name set in schema.
|
|
385
|
+
:param schema: Parent object.
|
|
386
386
|
"""
|
|
387
387
|
self.parent = self.parent or schema
|
|
388
388
|
self.name = self.name or field_name
|
|
@@ -405,9 +405,9 @@ class Field(FieldABC):
|
|
|
405
405
|
return str(value).title()
|
|
406
406
|
|
|
407
407
|
:param value: The value to be serialized.
|
|
408
|
-
:param
|
|
409
|
-
:param
|
|
410
|
-
:param
|
|
408
|
+
:param attr: The attribute or key on the object to be serialized.
|
|
409
|
+
:param obj: The object the value was pulled from.
|
|
410
|
+
:param kwargs: Field-specific keyword arguments.
|
|
411
411
|
:return: The serialized value
|
|
412
412
|
"""
|
|
413
413
|
return value
|
|
@@ -500,7 +500,9 @@ class Nested(Field):
|
|
|
500
500
|
name = fields.Str()
|
|
501
501
|
# Use lambda functions when you need two-way nesting or self-nesting
|
|
502
502
|
parent = fields.Nested(lambda: ParentSchema(only=("id",)), dump_only=True)
|
|
503
|
-
siblings = fields.List(
|
|
503
|
+
siblings = fields.List(
|
|
504
|
+
fields.Nested(lambda: ChildSchema(only=("id", "name")))
|
|
505
|
+
)
|
|
504
506
|
|
|
505
507
|
|
|
506
508
|
class ParentSchema(Schema):
|
|
@@ -710,9 +712,9 @@ class Pluck(Nested):
|
|
|
710
712
|
loaded = AlbumSchema().load(in_data) # => {'artist': {'id': 42}}
|
|
711
713
|
dumped = AlbumSchema().dump(loaded) # => {'artist': 42}
|
|
712
714
|
|
|
713
|
-
:param
|
|
715
|
+
:param nested: The Schema class or class name (string)
|
|
714
716
|
to nest, or ``"self"`` to nest the `Schema <marshmallow.Schema>` within itself.
|
|
715
|
-
:param
|
|
717
|
+
:param field_name: The key to pluck a value from.
|
|
716
718
|
:param kwargs: The same keyword arguments that :class:`Nested` receives.
|
|
717
719
|
"""
|
|
718
720
|
|
|
@@ -720,9 +722,14 @@ class Pluck(Nested):
|
|
|
720
722
|
self,
|
|
721
723
|
nested: Schema | SchemaMeta | str | typing.Callable[[], Schema],
|
|
722
724
|
field_name: str,
|
|
725
|
+
*,
|
|
726
|
+
many: bool = False,
|
|
727
|
+
unknown: str | None = None,
|
|
723
728
|
**kwargs,
|
|
724
729
|
):
|
|
725
|
-
super().__init__(
|
|
730
|
+
super().__init__(
|
|
731
|
+
nested, only=(field_name,), many=many, unknown=unknown, **kwargs
|
|
732
|
+
)
|
|
726
733
|
self.field_name = field_name
|
|
727
734
|
|
|
728
735
|
@property
|
|
@@ -832,11 +839,15 @@ class Tuple(Field):
|
|
|
832
839
|
#: Default error messages.
|
|
833
840
|
default_error_messages = {"invalid": "Not a valid tuple."}
|
|
834
841
|
|
|
835
|
-
def __init__(
|
|
836
|
-
|
|
842
|
+
def __init__(
|
|
843
|
+
self,
|
|
844
|
+
tuple_fields: typing.Iterable[Field] | typing.Iterable[type[Field]],
|
|
845
|
+
**kwargs,
|
|
846
|
+
):
|
|
847
|
+
super().__init__(**kwargs)
|
|
837
848
|
if not utils.is_collection(tuple_fields):
|
|
838
849
|
raise ValueError(
|
|
839
|
-
"tuple_fields must be an iterable of Field classes or
|
|
850
|
+
"tuple_fields must be an iterable of Field classes or instances."
|
|
840
851
|
)
|
|
841
852
|
|
|
842
853
|
try:
|
|
@@ -856,9 +867,9 @@ class Tuple(Field):
|
|
|
856
867
|
super()._bind_to_schema(field_name, schema)
|
|
857
868
|
new_tuple_fields = []
|
|
858
869
|
for field in self.tuple_fields:
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
new_tuple_fields.append(
|
|
870
|
+
new_field = copy.deepcopy(field)
|
|
871
|
+
new_field._bind_to_schema(field_name, self)
|
|
872
|
+
new_tuple_fields.append(new_field)
|
|
862
873
|
|
|
863
874
|
self.tuple_fields = new_tuple_fields
|
|
864
875
|
|
|
@@ -948,8 +959,12 @@ _NumType = typing.TypeVar("_NumType")
|
|
|
948
959
|
class Number(Field, typing.Generic[_NumType]):
|
|
949
960
|
"""Base class for number fields.
|
|
950
961
|
|
|
951
|
-
:param
|
|
962
|
+
:param as_string: If `True`, format the serialized value as a string.
|
|
952
963
|
:param kwargs: The same keyword arguments that :class:`Field` receives.
|
|
964
|
+
|
|
965
|
+
.. versionchanged:: 3.24.0
|
|
966
|
+
`Number <marshmallow.fields.Number>` should no longer be used as a field within a `Schema <marshmallow.Schema>`.
|
|
967
|
+
Use `Integer <marshmallow.fields.Integer>`, `Float <marshmallow.fields.Float>`, or `Decimal <marshmallow.fields.Decimal>` instead.
|
|
953
968
|
"""
|
|
954
969
|
|
|
955
970
|
num_type: type = float
|
|
@@ -1027,9 +1042,9 @@ class Integer(Number[int]):
|
|
|
1027
1042
|
class Float(Number[float]):
|
|
1028
1043
|
"""A double as an IEEE-754 double precision string.
|
|
1029
1044
|
|
|
1030
|
-
:param
|
|
1045
|
+
:param allow_nan: If `True`, `NaN`, `Infinity` and `-Infinity` are allowed,
|
|
1031
1046
|
even though they are illegal according to the JSON specification.
|
|
1032
|
-
:param
|
|
1047
|
+
:param as_string: If `True`, format the value as a string.
|
|
1033
1048
|
:param kwargs: The same keyword arguments that :class:`Number` receives.
|
|
1034
1049
|
"""
|
|
1035
1050
|
|
|
@@ -1196,8 +1211,8 @@ class Boolean(Field):
|
|
|
1196
1211
|
def __init__(
|
|
1197
1212
|
self,
|
|
1198
1213
|
*,
|
|
1199
|
-
truthy:
|
|
1200
|
-
falsy:
|
|
1214
|
+
truthy: typing.Iterable | None = None,
|
|
1215
|
+
falsy: typing.Iterable | None = None,
|
|
1201
1216
|
**kwargs,
|
|
1202
1217
|
):
|
|
1203
1218
|
super().__init__(**kwargs)
|
|
@@ -1283,7 +1298,7 @@ class DateTime(Field):
|
|
|
1283
1298
|
"format": '"{input}" cannot be formatted as a {obj_type}.',
|
|
1284
1299
|
}
|
|
1285
1300
|
|
|
1286
|
-
def __init__(self, format: str | None = None, **kwargs) -> None:
|
|
1301
|
+
def __init__(self, format: str | None = None, **kwargs) -> None: # noqa: A002
|
|
1287
1302
|
super().__init__(**kwargs)
|
|
1288
1303
|
# Allow this to be None. It may be set later in the ``_serialize``
|
|
1289
1304
|
# or ``_deserialize`` methods. This allows a Schema to dynamically set the
|
|
@@ -1341,7 +1356,7 @@ class NaiveDateTime(DateTime):
|
|
|
1341
1356
|
|
|
1342
1357
|
def __init__(
|
|
1343
1358
|
self,
|
|
1344
|
-
format: str | None = None,
|
|
1359
|
+
format: str | None = None, # noqa: A002
|
|
1345
1360
|
*,
|
|
1346
1361
|
timezone: dt.timezone | None = None,
|
|
1347
1362
|
**kwargs,
|
|
@@ -1378,7 +1393,7 @@ class AwareDateTime(DateTime):
|
|
|
1378
1393
|
|
|
1379
1394
|
def __init__(
|
|
1380
1395
|
self,
|
|
1381
|
-
format: str | None = None,
|
|
1396
|
+
format: str | None = None, # noqa: A002
|
|
1382
1397
|
*,
|
|
1383
1398
|
default_timezone: dt.tzinfo | None = None,
|
|
1384
1399
|
**kwargs,
|
|
@@ -1537,7 +1552,7 @@ class TimeDelta(Field):
|
|
|
1537
1552
|
delta = utils.timedelta_to_microseconds(value)
|
|
1538
1553
|
unit = utils.timedelta_to_microseconds(base_unit)
|
|
1539
1554
|
return delta // unit
|
|
1540
|
-
assert self.serialization_type is float
|
|
1555
|
+
assert self.serialization_type is float # noqa: S101
|
|
1541
1556
|
return value.total_seconds() / base_unit.total_seconds()
|
|
1542
1557
|
|
|
1543
1558
|
def _deserialize(self, value, attr, data, **kwargs):
|
|
@@ -1555,7 +1570,7 @@ class TimeDelta(Field):
|
|
|
1555
1570
|
|
|
1556
1571
|
|
|
1557
1572
|
class Mapping(Field):
|
|
1558
|
-
"""An abstract class for objects with key-value pairs.
|
|
1573
|
+
"""An abstract class for objects with key-value pairs. This class should not be used within schemas.
|
|
1559
1574
|
|
|
1560
1575
|
:param keys: A field class or instance for dict keys.
|
|
1561
1576
|
:param values: A field class or instance for dict values.
|
|
@@ -1566,6 +1581,9 @@ class Mapping(Field):
|
|
|
1566
1581
|
`keys` and `values` arguments to prevent content validation.
|
|
1567
1582
|
|
|
1568
1583
|
.. versionadded:: 3.0.0rc4
|
|
1584
|
+
.. versionchanged:: 3.24.0
|
|
1585
|
+
`Mapping <marshmallow.fields.Mapping>` should no longer be used as a field within a `Schema <marshmallow.Schema>`.
|
|
1586
|
+
Use `Dict <marshmallow.fields.Dict>` instead.
|
|
1569
1587
|
"""
|
|
1570
1588
|
|
|
1571
1589
|
mapping_type = dict
|
|
@@ -1629,16 +1647,15 @@ class Mapping(Field):
|
|
|
1629
1647
|
if not self.value_field and not self.key_field:
|
|
1630
1648
|
return self.mapping_type(value)
|
|
1631
1649
|
|
|
1632
|
-
#
|
|
1650
|
+
# Serialize keys
|
|
1633
1651
|
if self.key_field is None:
|
|
1634
|
-
keys = {k: k for k in value
|
|
1652
|
+
keys = {k: k for k in value}
|
|
1635
1653
|
else:
|
|
1636
1654
|
keys = {
|
|
1637
|
-
k: self.key_field._serialize(k, None, None, **kwargs)
|
|
1638
|
-
for k in value.keys()
|
|
1655
|
+
k: self.key_field._serialize(k, None, None, **kwargs) for k in value
|
|
1639
1656
|
}
|
|
1640
1657
|
|
|
1641
|
-
#
|
|
1658
|
+
# Serialize values
|
|
1642
1659
|
result = self.mapping_type()
|
|
1643
1660
|
if self.value_field is None:
|
|
1644
1661
|
for k, v in value.items():
|
|
@@ -1658,18 +1675,18 @@ class Mapping(Field):
|
|
|
1658
1675
|
|
|
1659
1676
|
errors = collections.defaultdict(dict)
|
|
1660
1677
|
|
|
1661
|
-
#
|
|
1678
|
+
# Deserialize keys
|
|
1662
1679
|
if self.key_field is None:
|
|
1663
|
-
keys = {k: k for k in value
|
|
1680
|
+
keys = {k: k for k in value}
|
|
1664
1681
|
else:
|
|
1665
1682
|
keys = {}
|
|
1666
|
-
for key in value
|
|
1683
|
+
for key in value:
|
|
1667
1684
|
try:
|
|
1668
1685
|
keys[key] = self.key_field.deserialize(key, **kwargs)
|
|
1669
1686
|
except ValidationError as error:
|
|
1670
1687
|
errors[key]["key"] = error.messages
|
|
1671
1688
|
|
|
1672
|
-
#
|
|
1689
|
+
# Deserialize values
|
|
1673
1690
|
result = self.mapping_type()
|
|
1674
1691
|
if self.value_field is None:
|
|
1675
1692
|
for k, v in value.items():
|
|
@@ -1769,7 +1786,7 @@ class Email(String):
|
|
|
1769
1786
|
class IP(Field):
|
|
1770
1787
|
"""A IP address field.
|
|
1771
1788
|
|
|
1772
|
-
:param
|
|
1789
|
+
:param exploded: If `True`, serialize ipv6 address in long form, ie. with groups
|
|
1773
1790
|
consisting entirely of zeros included.
|
|
1774
1791
|
|
|
1775
1792
|
.. versionadded:: 3.8.0
|
|
@@ -1835,7 +1852,7 @@ class IPInterface(Field):
|
|
|
1835
1852
|
|
|
1836
1853
|
see https://python.readthedocs.io/en/latest/library/ipaddress.html#interface-objects
|
|
1837
1854
|
|
|
1838
|
-
:param
|
|
1855
|
+
:param exploded: If `True`, serialize ipv6 interface in long form, ie. with groups
|
|
1839
1856
|
consisting entirely of zeros included.
|
|
1840
1857
|
"""
|
|
1841
1858
|
|
|
@@ -1886,8 +1903,8 @@ class IPv6Interface(IPInterface):
|
|
|
1886
1903
|
class Enum(Field):
|
|
1887
1904
|
"""An Enum field (de)serializing enum members by symbol (name) or by value.
|
|
1888
1905
|
|
|
1889
|
-
:param enum
|
|
1890
|
-
:param
|
|
1906
|
+
:param enum: Enum class
|
|
1907
|
+
:param by_value: Whether to (de)serialize by value or by name,
|
|
1891
1908
|
or Field class or instance to use to (de)serialize by value. Defaults to False.
|
|
1892
1909
|
|
|
1893
1910
|
If `by_value` is `False` (default), enum members are (de)serialized by symbol (name).
|
|
@@ -1959,10 +1976,10 @@ class Enum(Field):
|
|
|
1959
1976
|
class Method(Field):
|
|
1960
1977
|
"""A field that takes the value returned by a `Schema <marshmallow.Schema>` method.
|
|
1961
1978
|
|
|
1962
|
-
:param
|
|
1979
|
+
:param serialize: The name of the Schema method from which
|
|
1963
1980
|
to retrieve the value. The method must take an argument ``obj``
|
|
1964
1981
|
(in addition to self) that is the object to be serialized.
|
|
1965
|
-
:param
|
|
1982
|
+
:param deserialize: Optional name of the Schema method for deserializing
|
|
1966
1983
|
a value The method must take a single argument ``value``, which is the
|
|
1967
1984
|
value to deserialize.
|
|
1968
1985
|
|