konradhalas / dacite

Simple creation of data classes from dictionaries.
MIT License
1.72k stars 107 forks source link

InitVar with generic collections of nested structures not working. #116

Closed cmsxbc closed 3 years ago

cmsxbc commented 3 years ago

traceback

/home/lane/Code/which2rent/venv/bin/python /home/lane/Code/which2rent/nest.py
Traceback (most recent call last):
  File "/home/lane/Code/which2rent/nest.py", line 33, in <module>
    print(dacite.from_dict(C, testcase))
  File "/home/lane/Code/which2rent/venv/lib/python3.8/site-packages/dacite/core.py", line 65, in from_dict
    raise WrongTypeError(field_path=field.name, field_type=field.type, value=value)
dacite.exceptions.WrongTypeError: wrong value type for field "aa" - should be "dataclasses.InitVar[typing.List[__main__.A]]" instead of value "[{'a': 1}, {'a': 2}]" of type "list"
B(aa=[A(a=1), A(a=2)])

env

  1. Python: 3.8
  2. dancite: 1.5.1

testcase

import dataclasses
from typing import *
import dacite

@dataclasses.dataclass
class A:
    a: int

@dataclasses.dataclass
class B:
    aa: List[A]

@dataclasses.dataclass
class C:
    aa: dataclasses.InitVar[List[A]]
    real_aa: List[A] = dataclasses.field(init=False)

    def __post_init__(self, aa: List[A]):
        self.real_aa = [A(a=a.a+1) for a in aa]

testcase = {
    'aa': [
        {'a': 1},
        {'a': 2}
    ]
}

print(dacite.from_dict(B, testcase))  # working
print(dacite.from_dict(C, testcase))  # not working
konradhalas commented 3 years ago

@cmsxbc thank you for reporting this issue. It was definitely a bug. Fixed on master 🎉