question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError on from_dict on dataclass with nested dict and tuple

See original GitHub issue

When creating a dataclass with a nested dict and a tuple, from_dict() does not work.

For a minimal working example:

from typing import Dict, Tuple
from dataclasses import dataclass
from dataclasses_json import DataClassJsonMixin

@dataclass
class TestClass(DataClassJsonMixin):
    nested_dict: Dict[str, Dict[Tuple[str], int]]

my_test = TestClass.from_dict({"nested_dict": {"a":{("b", "c"): 1, "c": 2}, "d": {}}})

This yields:

Traceback (most recent call last):
  File "[PROJECT_PATH]python3.8/site-packages/dataclasses_json/core.py", line 263, in _decode_generic
    res = _get_type_cons(type_)(xs)
  File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/typing.py", line 727, in __call__
    raise TypeError(f"Type {self._name} cannot be instantiated; "
TypeError: Type Tuple cannot be instantiated; use tuple() instead
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "[PROJECT_PATH]python3.8/site-packages/dataclasses_json/core.py", line 263, in _decode_generic
    res = _get_type_cons(type_)(xs)
  File "[PROJECT_PATH]python3.8/site-packages/dataclasses_json/core.py", line 309, in <genexpr>
    items = (_decode_generic(type_arg, x, infer_missing) for x in xs)
  File "[PROJECT_PATH]python3.8/site-packages/dataclasses_json/core.py", line 265, in _decode_generic
    res = type_(xs)
  File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/typing.py", line 727, in __call__
    raise TypeError(f"Type {self._name} cannot be instantiated; "
TypeError: Type Dict cannot be instantiated; use dict() instead
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "[PROJECT_PATH]python3.8/site-packages/IPython/core/interactiveshell.py", line 3437, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-15-a734de727315>", line 1, in <module>
    bob = TestClass.from_dict({"nested_dict": {"a":{("b", "c"): 1, "c": 2}, "d": {}}})
  File "[PROJECT_PATH]python3.8/site-packages/dataclasses_json/api.py", line 83, in from_dict
    return _decode_dataclass(cls, kvs, infer_missing)
  File "[PROJECT_PATH]python3.8/site-packages/dataclasses_json/core.py", line 201, in _decode_dataclass
    init_kwargs[field.name] = _decode_generic(field_type,
  File "[PROJECT_PATH]python3.8/site-packages/dataclasses_json/core.py", line 265, in _decode_generic
    res = type_(xs)
  File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/typing.py", line 727, in __call__
    raise TypeError(f"Type {self._name} cannot be instantiated; "
TypeError: Type Dict cannot be instantiated; use dict() instead

Is there anything I am obviously doing wrong or a workaround without getting rid of the type annotations?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
christian-steinmeyercommented, Jun 5, 2021

Yes, thanks!

0reactions
pearceeverydaylabscommented, Jun 5, 2021

@christian-steinmeyer can this be closed now that #297 is merged?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python dataclass from a nested dict - Stack Overflow
I'm the author of dacite - the tool that simplifies creation of data classes from dictionaries. This library has only one function from_dict...
Read more >
All You Need To Know About Data Classes in Python - Jerry Ng
TL;DR; Named Tuple To The Rescue; Python Class; Enter Data Classes ... Plus, a deep-nested Dictionary tends to be very messy to work...
Read more >
dataclasses — Data Classes — Python 3.11.1 documentation
Each dataclass is converted to a dict of its fields, as name: value pairs. dataclasses, dicts, lists, and tuples are recursed into. Other...
Read more >
Data Classes in Python 3.7+ (Guide)
One new and exciting feature coming in Python 3.7 is the data class. ... In addition to tuple , dict , namedtuple ,...
Read more >
flytekit.core.type_engine - Flyte
from __future__ import annotations import collections import dataclasses ... functools import lru_cache from typing import Dict, NamedTuple, Optional, Type, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found