Search code examples
pythonpyqtpython-typingmypy

Class cannot subclass 'QObject' (has type 'Any') using 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')" 

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.


Solution

  • 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.