rnag/dataclass-wizard

Loading a Variadic Tuple fails for length 0

intentionally-left-nil opened this issue · 1 comments

  • Dataclass Wizard version: 0.22.2
  • Python version: both python 3.10 and python 3.12
  • Operating System: mac

Description

If you have a dataclass with:
my_tuple: tuple[int, ...]

then, trying to parse the following json fails {my_tuple: []}
because the tuple is empty. However, if there is at least one int, then this succeeds.

What I Did

from dataclasses import dataclass
from dataclass_wizard import fromdict
@dataclass
class Test:
    my_ints: tuple[int, ...]


fromdict(Test, {"my_ints": []})

fails with

dataclass_wizard.errors.ParseError: Failure parsing field `my_ints` in class `Test`. Expected a type [], got list.
  value: []
  error: Wrong number of elements.
  desired_count: '1 - inf'
  actual_count: 0
  json_object: '{"my_ints": []}'
rnag commented

@intentionally-left-nil you're totally right. I did a quick test with a new py file and confirmed that the IDE doesn't print any errors or warnings when a variadic tuple variable is assigned an empty tuple:

# no warnings
tup: tuple[int, ...] = ()

By comparison, the IDE highlighted a warning when an element in tuple was an incorrect type such as str, but I believe that's expected behavior.

# warning: Expected type 'tuple[int, ...]', got 'tuple[str]' instead
tup: tuple[int, ...] = ('23', )

So I believe you are totally right that variadic tuples can (and should) accept tuples of length 0. I guess it had never really crossed my mind before, that this case is 100% acceptable. Will take a look at the PR shortly. Thanks again for opening this issue.