11

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.

Notbad
  • 5,936
  • 12
  • 54
  • 100
  • Never used mypy. But the -> looks wonky. An initializer doesn’t return anything. – deets Apr 17 '18 at 22:16
  • 2
    Sorry but, You are wrong. __ init __ is required to return None. In fact it does, but implicitly in this case. – Notbad Apr 17 '18 at 22:20
  • Just saw that - yep. As I said, not a user. Good luck :) – deets Apr 17 '18 at 22:30
  • 1
    Have a look at this issue that appears to be the same as yours: https://github.com/python/mypy/issues/4180 and is related to accessing modules in other files. Guido says, "You need to tell mypy to check both files." – Lindsay Ward Apr 17 '18 at 23:23
  • This helped me. Did not solved my problem but made me achieve a "solution" – Notbad Apr 18 '18 at 10:30

3 Answers3

13

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.

ethanhs
  • 1,483
  • 9
  • 12
7

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.

Marcin Możejko
  • 39,542
  • 10
  • 109
  • 120
Notbad
  • 5,936
  • 12
  • 54
  • 100
  • 1
    "until type hinting is better supported for this lib" - if you did own the lib, how would you have fixed it? – marengaz Apr 30 '21 at 21:12
0

This is happening to me in a library that was fully typed. The problem was that these objects were not defined as public in the __all__ variable inside of the library (doc)