【发布时间】:2021-12-29 03:49:01
【问题描述】:
我有一个类,我想为其添加类型提示,如下所示:
import yaml
class TestClass(dict):
@classmethod
def load(cls, fname) -> "TestClass":
return cls(yaml.safe_load(""))
@property
@abc.abstractmethod
def test(self):
raise
当我在仅包含此类的模块上运行 mypy 时,我收到以下错误消息:
error: Cannot instantiate abstract class 'TestClass' with abstract attribute 'test'
根据我从其他帖子中了解到的情况,这与在执行加载方法时实例化“测试”方法有关。有没有办法单独使用 typehints 来解决这个问题,还是我需要在这里调整我的代码?
【问题讨论】:
-
你能澄清你想要做什么吗?在不是
abc.ABC的类上使用abc.abstractmethod没有多大意义。
标签: python type-hinting mypy