diff --git a/dataclasses_json/core.py b/dataclasses_json/core.py index fb7f0e6b..d34e51d3 100644 --- a/dataclasses_json/core.py +++ b/dataclasses_json/core.py @@ -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: diff --git a/tests/entities.py b/tests/entities.py index 6c0db906..61b8af23 100644 --- a/tests/entities.py +++ b/tests/entities.py @@ -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: diff --git a/tests/test_nested.py b/tests/test_nested.py index 56dc9ba2..69c6456f 100644 --- a/tests/test_nested.py +++ b/tests/test_nested.py @@ -1,4 +1,6 @@ -from tests.entities import (DataClassWithDataClass, +import pytest +from tests.entities import (DataClassMappingBadDecode, + DataClassWithDataClass, DataClassWithList, DataClassWithNestedDictWithTupleKeys, DataClassX, @@ -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")))) + class TestNested: def test_tuple_dict_key(self):