Skip to content

Commit 2f51e08

Browse files
author
python-desert
authored
Improve error message for unknown generics.
2 parents 9fc42d7 + 4b2af41 commit 2f51e08

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

CHANGELOG.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2019.12.18
2+
--------------
3+
4+
Changes
5+
^^^^^^^
6+
7+
- Improve error message for unknown generics.
8+
`#10 <https://github.com/python-desert/desert/pull/10>`_
9+
110
2019.12.10
211
--------------
312

src/desert/_make.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ def class_schema(clazz: type, meta: Dict[str, Any] = {}) -> Type[marshmallow.Sch
102102
"""
103103

104104
fields: Union[Tuple[dataclasses.Field], Tuple[attr.Attribute]]
105+
106+
if not isinstance(clazz, type):
107+
raise desert.exceptions.UnknownType(
108+
f"Desert failed to infer the field type for {clazz}.\n"
109+
+ "Explicitly pass a Marshmallow field type."
110+
)
105111
if dataclasses.is_dataclass(clazz):
106112
fields = dataclasses.fields(clazz)
107113
elif attr.has(clazz):

tests/test_make.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import dataclasses
22
import datetime
33
import enum
4+
import sys
45
import types
56
import typing as t
67

@@ -355,3 +356,17 @@ class A:
355356

356357
with pytest.raises(desert.exceptions.UnknownType):
357358
desert.schema_class(A)
359+
360+
361+
@pytest.mark.skipif(
362+
sys.version_info[:2] <= (3, 6), reason="3.6 has isinstance(t.Sequence[int], type)."
363+
)
364+
def test_raise_unknown_generic(module):
365+
"""Raise UnknownType for unknown generics."""
366+
367+
@module.dataclass
368+
class A:
369+
x: t.Sequence[int]
370+
371+
with pytest.raises(desert.exceptions.UnknownType):
372+
desert.schema_class(A)

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ skip_install = true
8585
commands =
8686
coverage report
8787
coverage html
88-
cuv graph
88+
8989

9090
[testenv:clean]
9191
commands = coverage erase

0 commit comments

Comments
 (0)