Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Tuple of mixed values from list of strings #227

Open
plonerma opened this issue May 3, 2023 · 1 comment
Open

Tuple of mixed values from list of strings #227

plonerma opened this issue May 3, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@plonerma
Copy link

plonerma commented May 3, 2023

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 plonerma added the bug Something isn't working label May 3, 2023
plonerma added a commit to plonerma/dacite that referenced this issue May 3, 2023
@plonerma
Copy link
Author

plonerma commented May 3, 2023

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant