You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pydantic数据类中设置静态预期值并解决参数顺序错误

问题:dataclass设置默认值触发TypeError

报错详情

在为dataclass字段设置默认值时,出现以下错误:

TypeError: non-default argument 'breakdownAccount' follows default argument

报错代码示例

from dataclasses import dataclass
from typing import Literal

@dataclass
class DataInclude:
    currencyAccount: Literal["CNY счет", "AMD счет", "RUB счет", "USD счет", "EUR счет", "GBP счет", "CHF счет"]
    open: bool = True
    accountType: Literal["Текущий счет"]
    currencyCode: Literal[810, 826, 756, 978, 840, 51, 156]
    currency: Literal['CNY', 'AMD', 'RUB', 'USD', 'EUR', 'GBP', 'CHF']

正常运行代码示例

from dataclasses import dataclass
from typing import Literal

@dataclass
class DataInclude:
    currencyAccount: Literal["CNY счет", "AMD счет", "RUB счет", "USD счет", "EUR счет", "GBP счет", "CHF счет"]
    open: bool
    accountType: Literal["Текущий счет"]
    currencyCode: Literal[810, 826, 756, 978, 840, 51, 156]
    currency: Literal['CNY', 'AMD', 'RUB', 'USD', 'EUR', 'GBP', 'CHF']

需求:如何为字段设置静态预期默认值,用于API自动化测试?


问题原因

Python语法强制要求:所有带默认值的函数参数必须放在无默认值参数的后面。你给open字段设置了默认值=True,但后续的accountType、currencyCode、currency都没有默认值,违反了这个规则,因此触发TypeError。

解决方法

根据你的需求,提供两种可行方案:

方案1:调整字段顺序(推荐)

把带默认值的open字段移到所有无默认值字段的末尾,语法上即可合法设置默认值:

from dataclasses import dataclass
from typing import Literal

@dataclass
class DataInclude:
    currencyAccount: Literal["CNY счет", "AMD счет", "RUB счет", "USD счет", "EUR счет", "GBP счет", "CHF счет"]
    accountType: Literal["Текущий счет"]
    currencyCode: Literal[810, 826, 756, 978, 840, 51, 156]
    currency: Literal['CNY', 'AMD', 'RUB', 'USD', 'EUR', 'GBP', 'CHF']
    open: bool = True  # 带默认值的字段移至末尾

方案2:保留字段顺序,用field和__post_init__处理

如果需要保留原有字段顺序,可通过dataclasses.field配合__post_init__方法实现默认值设置,同时保证后续字段仍为必填项:

from dataclasses import dataclass, field
from typing import Literal

@dataclass
class DataInclude:
    currencyAccount: Literal["CNY счет", "AMD счет", "RUB счет", "USD счет", "EUR счет", "GBP счет", "CHF счет"]
    open: bool = field(default=None, init=True)  # 允许传入值,默认设为None
    accountType: Literal["Текущий счет"]
    currencyCode: Literal[810, 826, 756, 978, 840, 51, 156]
    currency: Literal['CNY', 'AMD', 'RUB', 'USD', 'EUR', 'GBP', 'CHF']
    
    def __post_init__(self):
        # 如果未传入open值,设置为预期默认值True
        if self.open is None:
            self.open = True

如果open字段不需要外部传入,仅需固定默认值,可简化为:

from dataclasses import dataclass, field
from typing import Literal

@dataclass
class DataInclude:
    currencyAccount: Literal["CNY счет", "AMD счет", "RUB счет", "USD счет", "EUR счет", "GBP счет", "CHF счет"]
    accountType: Literal["Текущий счет"]
    currencyCode: Literal[810, 826, 756, 978, 840, 51, 156]
    currency: Literal['CNY', 'AMD', 'RUB', 'USD', 'EUR', 'GBP', 'CHF']
    open: bool = field(default=True, init=False)  # 不参与构造函数,固定默认值

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 01:20:32