Skip to content

Commit aa815b5

Browse files
author
python-desert
authored
Raise UnknownType exception with better error message.
2 parents 343a778 + ddba6f9 commit aa815b5

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

CHANGELOG.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
2019.12.10
2+
--------------
3+
4+
Changes
5+
^^^^^^^
6+
7+
- Add ``UnknownType`` exception with better error message for types that should be generic.
8+
`#8 <https://github.com/python-desert/desert/issues/8>`_
9+
10+
11+
112
2019.12.09
213
--------------
314

src/desert/_make.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ def class_schema(clazz: type, meta: Dict[str, Any] = {}) -> Type[marshmallow.Sch
106106
fields = dataclasses.fields(clazz)
107107
elif attr.has(clazz):
108108
fields = attr.fields(clazz)
109+
elif issubclass(clazz, (list, dict)):
110+
raise desert.exceptions.UnknownType(
111+
"Use parametrized generics like typing.List[int] or typing.Dict[str, int] "
112+
f"instead of list and dict. Got {clazz}"
113+
)
109114
else:
110115
raise desert.exceptions.NotAnAttrsClassOrDataclass(clazz)
111116

src/desert/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2019.12.09"
1+
__version__ = "2019.12.10"

src/desert/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ class DesertException(Exception):
99

1010
class NotAnAttrsClassOrDataclass(DesertException):
1111
"""Raised for dataclass operations on non-dataclasses."""
12+
13+
14+
class UnknownType(DesertException):
15+
"""Raised for a type with unknown serialization equivalent."""

tests/test_make.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,14 @@ class A:
344344
schema = schema_class()
345345
data = schema.load({"x": 1, "y": 2})
346346
assert data == A(x=1)
347+
348+
349+
def test_raise_unknown_type(module):
350+
"""Raise UnknownType for failed inferences."""
351+
352+
@module.dataclass
353+
class A:
354+
x: list
355+
356+
with pytest.raises(desert.exceptions.UnknownType):
357+
desert.schema_class(A)

0 commit comments

Comments
 (0)