Repository: lidatong/dataclasses-json

Issue:
[BUG] common errors from decoder on inner dataclass silently ignored
### Description

If you have a dataclass with a mapping field that includes a dataclass member, and you try to `from_dict` the outer dataclass, some errors from a custom `decoder` implementation will get silently ignored.

### Code snippet that reproduces the issue

```py
from dataclasses import dataclass, field
from dataclasses_json import config, dataclass_json


def implementation_issue(value: str):
    raise TypeError("improper implementation")


@dataclass_json
@dataclass
class InnerExample:
    example_inner: int = field(metadata=config(decoder=implementation_issue))


@dataclass_json
@dataclass
class Example:
    example_field: dict[str, InnerExample]


# This should fail, but it doesn't. The new Example instance's example_field
# value is just empty instead.
Example.from_dict(dict(example_field=dict(test=dict(example_inner="what"))))
```

### Describe the results you expected

```
Traceback (most recent call last):
  File "/private/tmp/t.py", line 23, in <module>
    Example.from_dict(dict(example_field=dict(test=dict(example_inner="what"))))
  File "/usr/local/lib/python3.9/site-packages/dataclasses_json/api.py", line 70, in from_dict
    return _decode_dataclass(cls, kvs, infer_missing)
  File "/usr/local/lib/python3.9/site-packages/dataclasses_json/core.py", line 210, in _decode_dataclass
    init_kwargs[field.name] = _decode_generic(field_type,
  File "/usr/local/lib/python3.9/site-packages/dataclasses_json/core.py", line 279, in _decode_generic
    res = t(xs)
  File "/usr/local/lib/python3.9/site-packages/dataclasses_json/core.py", line 336, in <genexpr>
    items = (_decode_dataclass(type_arg, x, infer_missing)
  File "/usr/local/lib/python3.9/site-packages/dataclasses_json/core.py", line 196, in _decode_dataclass
    init_kwargs[field.name] = overrides[field.name].decoder(
  File "/private/tmp/t.py", line 6, in implementation_issue
    raise TypeError("improper implementation")
TypeError: improper implementation
```

### Python version you are using

3.9.16

### Environment description

irrelevant, cause is clear

Discussion:
skeggse: The mypy errors _look_ like they're already present on `master`, but I haven't carefully examined them.
george-zubrienko: > The mypy errors _look_ like they're already present on `master`, but I haven't carefully examined them.

This is not a required check yet. You can ignore this for now :)

Patch:
diff --git a/dataclasses_json/core.py b/dataclasses_json/core.py
index fb7f0e6..d34e51d 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:


Test patch:
diff --git a/tests/entities.py b/tests/entities.py
index 6c0db90..61b8af2 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 56dc9ba..69c6456 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 @@ class TestDecoder:
         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):