dacite icon indicating copy to clipboard operation
dacite copied to clipboard

Tuple of mixed values from list of strings

Open plonerma opened this issue 2 years ago • 1 comments

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

  • Python version: 3.8.10 (though it should not be python specific)
  • dacite version: 1.8.0

Additional context

  • The problem lies in the fact that the tuple-specific treatment is only used, if the data also contains a tuple.

  • This error only occurs if cast includes tuple. Otherwise both examples would raise an exception due to an list being used.

plonerma avatar May 03 '23 09:05 plonerma

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

plonerma avatar May 03 '23 11:05 plonerma