konradhalas / dacite

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

Tuple of mixed values from list of strings #227

Open plonerma opened 1 year ago

plonerma commented 1 year ago

Describe the bug This is issue is potentially related to #73.

If a tuple is expected, but the data contains another collection (e.g. a list), the tuple is treated as a collection, as opposed to the more specific treatment of tuples: If the tuple is expected to contain mixed values, these types are not cast correctly.

To Reproduce

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

@dataclass
class A:
    values: typing.Tuple[int, ...]

@dataclass
class B:
    values: typing.Tuple[str, int]

from_dict(A, {"values": ["1", "2"]}, config=Config(cast=[tuple, int]))  # works
from_dict(B, {"values": ["1", "2"]}, config=Config(cast=[tuple, int]))  # raises WrongTypeError

Expected behavior Neither of these examples should raise an exception.

Environment

Additional context

plonerma commented 1 year ago

I took the liberty to open a PR with an attempt to fix this issue (#228).