gpt4 book ai didi

python - 是否有关于数据类属性 mypy 错误的解决方法?

转载 作者:太空宇宙 更新时间:2023-11-03 21:00:24 28 4
gpt4 key购买 nike

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

关于python - 是否有关于数据类属性 mypy 错误的解决方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55743514/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com