类不能使用 mypy 子类化“QObject"(类型为“Any") [英] Class cannot subclass 'QObject' (has type 'Any') using mypy

查看:108
本文介绍了类不能使用 mypy 子类化“QObject"(类型为“Any")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个继承 QObject 的类.一切正常,但是当我在其上运行 mypy 时出现错误:

I have a class that subclasses QObject. Everyting works fine but when I run mypy on it I get the error:

"error: Class cannot subclass 'QObject' (has type 'Any')" 

此刻我完全被困住了.我一直在阅读 mypy 文档,但找不到错误所在.

At the moment I am totally stuck. I Have been reading the mypy docs but couldn't find where the error was.

代码如下:

from PyQt5.QtCore import QObject

class ServiceLocator(QObject):

    def __init__(self) -> None:
        super().__init__()
        ...

干杯.

推荐答案

当 mypy 没有类的类型信息(在您的情况下是由于缺少存根)并且您有 --disallow-subclassing-any 打开.您可以禁用此标志,添加输入信息,或者,如您所指出的,放置 # type: ignore 以消除错误.

This error occurs when mypy doesn't have type information for a class (in your case due to a lack of stubs) and you have --disallow-subclassing-any turned on. You can either disable this flag, add typing information, or, as you pointed out, put a # type: ignore to silence the error.

这篇关于类不能使用 mypy 子类化“QObject"(类型为“Any")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆