gpt4 book ai didi

Python + mypy + mixin = has no attribute 报错

转载 作者:行者123 更新时间:2023-12-05 08:48:43 25 4
gpt4 key购买 nike

我在UI界面出现Vscode + mypy报错:"EngineMixin"has no attribute "engine"为下一个 Mixin 类

class EngineMixin:
def prepare_start(self):
self.engine.start()

def prepare_stop(self):
self.engine.stop()

def __str__(self):
output = super().__str__()
output += f'\n{self.__class__.__name__} characteristics. Engine [{self.engine}]' # noqa
return output

其实这个错误只出现在Vscode UI界面。当我在 cli 中运行 mypy 时,没有错误。一般来说,我明白为什么会出现这个错误,但是有什么办法可以在 vscode 中抑制它吗?

编辑(添加屏幕): screenshot

最佳答案

对于引用它们被混入的类的 mixins,您可以使用类型 stub 让 mypy 知道 self.engine 应该存在

class EngineMixin:

# Like this
engine: Engine

def prepare_start(self):
self.engine.start()

def prepare_stop(self):
self.engine.stop()

def __str__(self):
output = super().__str__()
output += f'\n{self.__class__.__name__} characteristics. Engine [{self.engine}]' # noqa
return output

关于Python + mypy + mixin = has no attribute 报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65552132/

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