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错误的方法?
使用# type: ignore
可以避免此错误
@dataclasses.dataclass
class MyFoo(Crud):
name: str # type: ignore