Skip to content

Commit a0c715c

Browse files
authored
Merge pull request #65 from altendky/coverage_changes
tests and changes for coverage
2 parents fc89fa3 + a55193d commit a0c715c

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/desert/_make.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def field_for_schema(
256256
values=field_for_schema(arguments[1]),
257257
)
258258
elif typing_inspect.is_optional_type(typ):
259-
subtyp = next(t for t in arguments if t is not NoneType)
259+
[subtyp] = (t for t in arguments if t is not NoneType)
260260
# Treat optional types as types with a None default
261261
metadata[_DESERT_SENTINEL]["default"] = metadata.get("default", None)
262262
metadata[_DESERT_SENTINEL]["missing"] = metadata.get("missing", None)

tests/test_make.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import dataclasses
22
import datetime
3+
import decimal
34
import enum
5+
import re
46
import sys
57
import types
68
import typing as t
79

810
import attr
911
import marshmallow
12+
import marshmallow.fields
1013
import pytest
1114

1215
import desert
@@ -282,6 +285,40 @@ class A:
282285
assert attr.fields(A).x.metadata["foo"] == 1
283286

284287

288+
def test_non_init(module):
289+
"""Non-init attributes are not included in schema"""
290+
291+
@module.dataclass
292+
class A:
293+
x: int
294+
y: str = module.field(default="can't init this", init=False)
295+
296+
schema = desert.schema_class(A)()
297+
298+
assert "y" not in schema.fields
299+
300+
301+
def test_metadata_marshmallow_field_loads(module):
302+
"""Marshmallow field can be specified via metadata dict"""
303+
304+
@module.dataclass
305+
class A:
306+
x: decimal.Decimal = module.field(
307+
metadata={"marshmallow_field": marshmallow.fields.Decimal(as_string=True)}
308+
)
309+
310+
schema = desert.schema_class(A)()
311+
312+
assert schema.loads('{"x": "1.3"}') == A(decimal.Decimal("1.3"))
313+
314+
315+
def test_get_field_default_raises_for_non_field():
316+
"""Not attrs and not dataclasses field raises"""
317+
318+
with pytest.raises(TypeError, match=re.escape("None")):
319+
desert._make._get_field_default(field=None)
320+
321+
285322
@pytest.mark.parametrize(argnames=["value"], argvalues=[["X"], [5]])
286323
def test_union(module, value, assert_dump_load):
287324
"""Deserialize one of several types."""

0 commit comments

Comments
 (0)