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

fix: surface nested mapping errors #426

Merged
merged 4 commits into from
Jun 23, 2023
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
6 changes: 4 additions & 2 deletions dataclasses_json/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,12 @@ def _decode_generic(type_, value, infer_missing):

# get the constructor if using corresponding generic type in `typing`
# otherwise fallback on constructing using type_ itself
materialize_type = type_
try:
res = _get_type_cons(type_)(xs)
materialize_type = _get_type_cons(type_)
except (TypeError, AttributeError):
res = type_(xs)
pass
res = materialize_type(xs)
else: # Optional or Union
_args = _get_type_args(type_)
if _args is _NO_ARGS:
Expand Down
13 changes: 13 additions & 0 deletions tests/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,19 @@ class DataClassWithConfigHelper:
id: float = field(metadata=config(encoder=str))


@dataclass_json
@dataclass
class DataClassWithErroneousDecode:
# Accepts no arguments, so passing in a single argument will result in a TypeError.
id: float = field(metadata=config(decoder=lambda: None))


@dataclass_json
@dataclass
class DataClassMappingBadDecode:
map: Dict[str, DataClassWithErroneousDecode]


@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass
class DataClassWithConfigDecorator:
Expand Down
8 changes: 7 additions & 1 deletion tests/test_nested.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from tests.entities import (DataClassWithDataClass,
import pytest
from tests.entities import (DataClassMappingBadDecode,
DataClassWithDataClass,
DataClassWithList,
DataClassWithNestedDictWithTupleKeys,
DataClassX,
Expand All @@ -25,6 +27,10 @@ def test_nested_list_of_dataclasses(self):
assert (DataClassXs.from_json('{"xs": [{"x": 0}, {"x": 1}]}') ==
DataClassXs([DataClassX(0), DataClassX(1)]))

def test_nested_mapping_of_dataclasses(self):
with pytest.raises(TypeError, match="positional arguments"):
DataClassMappingBadDecode.from_dict(dict(map=dict(test=dict(id="irrelevant"))))
skeggse marked this conversation as resolved.
Show resolved Hide resolved


class TestNested:
def test_tuple_dict_key(self):
Expand Down