Class cannot subclass 'QObject' (has type 'Any') using mypy
Asked Answered
T

5

13

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.

Triode answered 17/4, 2018 at 22:3 Comment(2)
Have a look at this issue that appears to be the same as yours: 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."Patristic
This helped me. Did not solved my problem but made me achieve a "solution"Triode
W
19

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.

Wadsworth answered 23/4, 2018 at 22:48 Comment(1)
I recommend to use more specific error type (# type: ignore[MYPY_ERR_TYPE], [MYPY_ERR_TYPE] would be [misc] in this case) to prevent possible ignoring of all other mypy errors.Bonneau
T
8

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.

Triode answered 18/4, 2018 at 10:34 Comment(1)
"until type hinting is better supported for this lib" - if you did own the lib, how would you have fixed it?Herrod
F
1

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)

Filemon answered 20/12, 2022 at 14:53 Comment(0)
S
0

I had a similar problem with mypy. Fixed by adding a py.typed to my own lib from which I was importing base class.

Sovereignty answered 12/4 at 6:18 Comment(0)
P
0

Just for some extra info if you encounter this - to use a config alternative but still have the full strict checking you can do the following in your mypy config:

[mypy]
# enable all additional optional checks
strict = True
# do not validate subclassing any types (without any knowledge)
disallow_subclassing_any = False

Note the order in using config and command line options with the latter ones taking precedence. This means that using --strict on the command line will ignore your config option unless you define both within the same config. I hope this helps!

Polytypic answered 27/6 at 11:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.