Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for unknown generics. #10

Merged
merged 4 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
2019.12.18
--------------

Changes
^^^^^^^

- Improve error message for unknown generics.
`#10 <https://github.com/python-desert/desert/pull/10>`_

2019.12.10
--------------

Expand Down
6 changes: 6 additions & 0 deletions src/desert/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_make.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dataclasses
import datetime
import enum
import sys
import types
import typing as t

Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ skip_install = true
commands =
coverage report
coverage html
cuv graph


[testenv:clean]
commands = coverage erase
Expand Down