konradhalas / dacite

Simple creation of data classes from dictionaries.
MIT License
1.76k stars 106 forks source link

Regression with nested classes #226

Open quimey opened 1 year ago

quimey commented 1 year ago

Describe the bug

Dacite failes to recognize a nested class structure involving tuples and lists.

To Reproduce

from typing import List, Tuple
from dataclasses import dataclass
from dacite import from_dict, Config

@dataclass
class A:
    i: List[str]
    t: str

@dataclass
class B:
    m: Tuple[str, A]

assert from_dict(
    data_class=B,
    data={"m": ["a", {"i": [], "t": "foo"}]},
    config=Config(cast=[tuple])
) == B(m=('a', A(i=[], t='foo')))

Running the code yields:

  File ".venv/lib/python3.10/site-packages/dacite/core.py", line 69, in from_dict
    raise WrongTypeError(field_path=field.name, field_type=field_type, value=value)
dacite.exceptions.WrongTypeError: wrong value type for field "m" - should be "Tuple" instead of value "('a', {'i': [], 't': 'foo'})" of type "tuple"

Expected behavior No errors Environment

Additional context The code works just fine with dacite 1.6.0