Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 481
Notbad Avatar asked Apr 17 '18 22:04

Notbad


2 Answers

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.

like image 94
ethanhs Avatar answered Nov 17 '22 17:11

ethanhs


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.

like image 5
Notbad Avatar answered Nov 17 '22 16:11

Notbad