konradhalas / dacite

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

regression for tuples w/ mixed types after 1.4.0 #135

Closed ccwienk closed 3 years ago

ccwienk commented 3 years ago

Up to 1.4.0, it was possible to deserialise tuples w/ mixed elements. Starting from 1.5.0, this no longer works.

Reproducer

import dataclasses
import typing

import dacite

@dataclasses.dataclass
class A:
 seq: typing.Tuple[typing.Union[int, str]

dacite.from_dict(
 data_class=A,
 data={'seq': [1, 'foo']},
 config=dacite.Config(cast=[tuple]),
)

--> works w/ dacite 1.4.0. Greater versions will return:

dacite.exceptions.WrongTypeError: wrong value type for field "seq" - should be "typing.Tuple[typing.Union[int, str]]" instead of value "(1, 'foo')" of type "tuple"

ccwienk commented 3 years ago

nevermind. Turns out I should have read the python-documentation more thoroughly. Using the ellipsis (...) like so will work (only tested for dacite 1.6.0:

@dataclasses.dataclass
class A:
 seq: typing.Tuple[typing.Union[int, str], ...]