是否有关于dataclass属性mypy错误的解决方法?

问题描述 投票:0回答:1

Mypy使用此数据类继承产生错误:

import dataclasses
import datetime

import typing


@dataclasses.dataclass
class Crud:
    creation_datetime: typing.Optional[datetime.datetime] = dataclasses.field(init=False)

    def __post_init__(self) -> None:
        self.creation_datetime = getattr(self, "creation_datetime", datetime.datetime.utcnow())


@dataclasses.dataclass
class MyFoo(Crud):
    name: str
t.py:17: error: Attributes without a default cannot follow attributes with one

是否存在一种压制此错误或以不同方式设计代码以避免mypy错误的方法?

python mypy
1个回答
0
投票

使用# type: ignore可以避免此错误

@dataclasses.dataclass
class MyFoo(Crud):
    name: str  # type: ignore
© www.soinside.com 2019 - 2024. All rights reserved.