konradhalas / dacite

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

Impossible to set a field value whose annotated type is an abstract collection #54

Closed NoiseByNorthwest closed 5 years ago

NoiseByNorthwest commented 5 years ago
from dataclasses import dataclass
from typing import Sequence

import dacite

@dataclass(frozen=True)
class Foo:
    bar: Sequence[int]

dacite.from_dict(Foo, {"bar": []}) # FAIL: TypeError("object() takes no parameters")
                                   #    raised in dacite/core.py:105

The error is raised right here https://github.com/konradhalas/dacite/blob/55051de7c230c787495b9aacff815eb0b282aa0e/dacite/core.py#L105

This is due to the fact that collection_cls is resolved in this case to collections.abc.Sequence.

So I cannot initialize a field hinted as an abstract type whereas the given value is obviously of a concrete & compatible type ?

Why such a limitation ? Why simply not use the value type as long as it is compatible with the annotated type ?

konradhalas commented 5 years ago

@NoiseByNorthwest thank you for reporting this issue. You have right, we should allow such types. I will figure out how to handle this case.