marshmallow_dataclass icon indicating copy to clipboard operation
marshmallow_dataclass copied to clipboard

TypeError: Ellipsis is not a dataclass and cannot be turned into one.

Open heckad opened this issue 5 years ago • 6 comments

from dataclasses import field
from decimal import Decimal
from typing import Tuple

from marshmallow_dataclass import dataclass


@dataclass(frozen=True)
class Signal:
    fields: Tuple[Decimal, ...] = field(default_factory=list)

This ellipsis means that all values in fields have Decimal type.

heckad avatar Jan 22 '20 11:01 heckad

Unfortunately, marshmallow does not seem to support variable length Tuple. As per the documentation:

class marshmallow.fields.Tuple: A tuple field, composed of a fixed number of other Field classes or instances

@sloria : Any opinion ?

lovasoa avatar Jan 22 '20 12:01 lovasoa

How to make frozen dataclass with a list?

heckad avatar Jan 22 '20 13:01 heckad

How to make frozen dataclass with a list?

I think your best bet for now is to create your own VariableLengthTuple field. See marshmallow's documentation on custom fields.

Then, you can have:

@dataclass(frozen=True)
class Signal:
    fields: Tuple[Decimal, ...] = field(
        default_factory=tuple,
        metadata = {"marshmallow_field":VariableLengthTuple()})

lovasoa avatar Jan 22 '20 13:01 lovasoa

desert (a fork of marshmallow_dataclass) added support for variadic tuples. The PR would be worth evalutating: https://github.com/python-desert/desert/pull/17

sloria avatar Jan 23 '20 00:01 sloria

@sloria : Would you consider adding VariadicTuple to marshmallow itself?

lovasoa avatar Jan 23 '20 08:01 lovasoa

We just ran into this as well. Support for variadic tuples would be cool but in the end the workaround is pretty simple (just using List now).

mikeholler avatar Apr 12 '21 14:04 mikeholler

I'm not sure when/how, but it seems to me like this is now resolved (I tried with version 8.5.10). Can anyone else confirm?

deansg avatar Jan 06 '23 09:01 deansg

I'm not sure when/how, but it seems to me like this is now resolved (I tried with version 8.5.10). Can anyone else confirm?

This was indeed fixed in #221, which is included as of release 8.5.10. (I was working on fixing #181 and independently noticed and fixed this issue as well, unaware of this ticket.)

dairiki avatar Jan 06 '23 18:01 dairiki