Skip to content

Commit 5681ae1

Browse files
author
desert
committed
Allow none
1 parent 721f859 commit 5681ae1

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/desert/_make.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ def field_for_schema(
201201

202202
if default is not marshmallow.missing:
203203
desert_metadata.setdefault("default", default)
204+
desert_metadata.setdefault('allow_none', True)
204205
if not desert_metadata.get(
205206
"required"
206207
): # 'missing' must not be set for required fields.
@@ -242,9 +243,11 @@ def field_for_schema(
242243
metadata[_DESERT_SENTINEL]["missing"] = metadata.get("missing", None)
243244
metadata[_DESERT_SENTINEL]["required"] = False
244245

246+
245247
field = field_for_schema(subtyp, metadata=metadata, default=None)
246248
field.default = None
247249
field.missing = None
250+
field.allow_none = True
248251

249252
elif typing_inspect.is_union_type(typ):
250253
subfields = [field_for_schema(subtyp) for subtyp in arguments]

tests/test_make.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ class A:
137137
data = desert.schema_class(A)().load({})
138138
assert data == A(None)
139139

140+
def test_optional_present(module):
141+
"""Setting an optional type allows passing None."""
142+
143+
@module.dataclass
144+
class A:
145+
x: t.Optional[int]
146+
147+
data = desert.schema_class(A)().load({"x": None})
148+
assert data == A(None)
149+
140150

141151
def test_custom_field(module):
142152
@module.dataclass

0 commit comments

Comments
 (0)