【发布时间】:2021-11-29 12:23:38
【问题描述】:
此代码包含一个巨大的类型错误,mypy 错过了:
from dataclasses import dataclass
class WrongThing:
def do_wrong_way(self):
pass
class RightThing:
def do_right_way(self):
pass
@dataclass
class Holder:
thing: RightThing
def fill_thing(self):
self.thing.do_wrong_way() # <--- This is wrong
m = Holder(RightThing())
m.fill_thing()
为什么 mypy 不反对 self.thing 缺少 do_wrong_way 属性?我做错了什么,呃,错了吗?
mypy 确实正确地用 [attr-defined] 错误标记此代码:
r = RightThing()
r.do_wrong_way() # <--- [attr-defined] error
【问题讨论】:
标签: python-3.x type-hinting mypy python-typing python-dataclasses