【发布时间】:2020-04-06 16:15:39
【问题描述】:
我需要条目字段只有一个或两个元素的元组,但没有更多。 但是这样的设计会出错。
from decimal import Decimal as _Decimal
from dataclasses import field
from typing import Tuple
from typing import Union
import marshmallow
import marshmallow_dataclass
from marshmallow_dataclass import dataclass
from marshmallow_dataclass import NewType
Decimal = NewType("Decimal", _Decimal, field=marshmallow.fields.Decimal, as_string=True)
Entries = Union[Tuple[Decimal], Tuple[Decimal, Decimal]]
@dataclass(frozen=True)
class Signal(Model):
entries: Entries = field(default_factory=tuple)
...
@classmethod
@property
def Schema(cls):
return marshmallow_dataclass.class_schema(cls)
class Meta:
ordered = True
错误信息
super().__init__(*args, **kwargs)
File "/home/prefixet/.local/share/virtualenvs/telegram-bot-LL7aNOup/lib/python3.8/site-packages/marshmallow/fields.py", line 185, in __init__
raise ValueError("'missing' must not be set for required fields.")
ValueError: 'missing' must not be set for required fields.
Process finished with exit code 1
另外,我尝试使用 Tuple[Decimal, ...],但它不起作用。 谢谢。
【问题讨论】:
标签: python type-hinting marshmallow