简体   繁体   中英

Is there a workaround about dataclass attribute mypy error?

Mypy produce an error with this dataclass inheritance:

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

Does exist a way to supress this error or design the code differently to avoid mypy error?

For information, this error can be avoided with # type: ignore

@dataclasses.dataclass
class MyFoo(Crud):
    name: str  # type: ignore

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM