问题
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')"
At the moment I am totally stuck. I Have been reading the mypy docs but couldn't find where the error was.
Here the code:
from PyQt5.QtCore import QObject
class ServiceLocator(QObject):
def __init__(self) -> None:
super().__init__()
...
Cheers.
回答1:
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.
回答2:
In order to leave a record on how I get around this I will answer my own question.
As the previous comment suggests, the error arise because mypy
doesn't have information about QObject
. I tried to add the .pyi
files to mypy
in the third-party folder from here or you can try building from sources PyQt5
.
Everything worked but a lot of other errors arose so I finally decided to use:
#type: ignore
on this lines and get rid of the error until type hinting is better supported for this lib.
Cheers.
来源:https://stackoverflow.com/questions/49888155/class-cannot-subclass-qobject-has-type-any-using-mypy