Mypy showing "Callable... has no attribute "connect" on every connect() in PyQT

Viewed 538

Writing a code using PyQt5, using mypy to find type errors. But mypy finds errors on every connect(), for example on this simple file:

from PyQt5 import QtWidgets

class TestClass(QtWidgets.QDialog):
    def __init__(self) -> None:
        super().__init__()
        self.accepted.connect(self.accept)

I have mypy error: 1.py:6: error: "Callable[[], None]" has no attribute "connect"

Is there any way to explain mypy it's correct code? I'd like not to use "ignore" comments for him...

1 Answers

Install the PyQt5-stubs type stubs for PyQt5.

>mypy test.py
test.py:6: error: "Callable[[], None]" has no attribute "connect"
Found 1 error in 1 file (checked 1 source file)

>pip install PyQt5-stubs
Collecting PyQt5-stubs
  Downloading PyQt5_stubs-5.15.2.0-py3-none-any.whl (371 kB)
     |████████████████████████████████| 371 kB 3.3 MB/s
Installing collected packages: PyQt5-stubs
Successfully installed PyQt5-stubs-5.15.2.0

>mypy test.py
Success: no issues found in 1 source file
Related