diff --git a/CHANGELOG.rst b/CHANGELOG.rst index bf1a800..146b8d8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,12 @@ +2019.12.18 +-------------- + +Changes +^^^^^^^ + +- Improve error message for unknown generics. + `#10 `_ + 2019.12.10 -------------- diff --git a/src/desert/_make.py b/src/desert/_make.py index d1009c1..faab7b0 100644 --- a/src/desert/_make.py +++ b/src/desert/_make.py @@ -102,6 +102,12 @@ def class_schema(clazz: type, meta: Dict[str, Any] = {}) -> Type[marshmallow.Sch """ fields: Union[Tuple[dataclasses.Field], Tuple[attr.Attribute]] + + if not isinstance(clazz, type): + raise desert.exceptions.UnknownType( + f"Desert failed to infer the field type for {clazz}.\n" + + "Explicitly pass a Marshmallow field type." + ) if dataclasses.is_dataclass(clazz): fields = dataclasses.fields(clazz) elif attr.has(clazz): diff --git a/tests/test_make.py b/tests/test_make.py index 35fb8f7..8dd087f 100644 --- a/tests/test_make.py +++ b/tests/test_make.py @@ -1,6 +1,7 @@ import dataclasses import datetime import enum +import sys import types import typing as t @@ -355,3 +356,17 @@ class A: with pytest.raises(desert.exceptions.UnknownType): desert.schema_class(A) + + +@pytest.mark.skipif( + sys.version_info[:2] <= (3, 6), reason="3.6 has isinstance(t.Sequence[int], type)." +) +def test_raise_unknown_generic(module): + """Raise UnknownType for unknown generics.""" + + @module.dataclass + class A: + x: t.Sequence[int] + + with pytest.raises(desert.exceptions.UnknownType): + desert.schema_class(A) diff --git a/tox.ini b/tox.ini index 79f3792..3c8f703 100644 --- a/tox.ini +++ b/tox.ini @@ -85,7 +85,7 @@ skip_install = true commands = coverage report coverage html - cuv graph + [testenv:clean] commands = coverage erase