Status:
PyQt
library from here www.pythonguis.com. Below is the simplified/part of code from this tutorial.mypy
. (Shown on the 2-nd source-code block).Question:
pyqtSlot()
decorator?Superclass
(that return None
), and what is Subclass
(that returns str|None
) in this case?What I tried to solve:
stub
file) says pyqtSlot()
is:
def pyqtSlot(*types, name: typing.Optional[str] = ..., result: typing.Optional[str] = ...) -> typing.Callable[..., typing.Optional[str]]: ...
, I'm unable to interpret this due to no-experience on decorators.stub
file: QtCore.pyi
says that Class QRunnable
's run
is: def run(self) -> None: ...
.from typing import Callable
from PyQt5.QtCore import QObject, QRunnable, pyqtSignal, pyqtSlot
import traceback
import sys
class WorkerSignal(QObject):
finished = pyqtSignal()
error = pyqtSignal(tuple)
result = pyqtSignal(object)
progress = pyqtSignal(int)
class Worker(QRunnable):
def __init__(self, fn: Callable[[], str]) -> None:
super(Worker, self).__init__()
self.fn:Callable[[], str] = fn
self.signals: WorkerSignal = WorkerSignal()
@pyqtSlot()
def run(self) -> None: # <====================== this line
try:
result: str = self.fn()
except Exception:
traceback.print_exc()
exctype, value = sys.exc_info()[:2]
self.signals.error.emit((exctype, value, traceback.format_exc()))
else:
self.signals.result.emit(result)
finally:
self.signals.finished.emit()
$ mypy /tmp/tmp.py
/tmp/tmp.py:22: error: Signature of "run" incompatible with supertype "QRunnable" [override]
/tmp/tmp.py:22: note: Superclass:
/tmp/tmp.py:22: note: def run(self) -> None
/tmp/tmp.py:22: note: Subclass:
/tmp/tmp.py:22: note: str | None
Found 1 error in 1 file (checked 1 source file)
EDIT:
pyqtSlot()
takes function that returns typing.Optional[str]
(i.e. str | None
). Perhaps its the subclass
?QRunnable
's method run()
only return None
, Perhaps it's the superclass
?decorator's return
and function's return
are different? Would this be the correct question?Other than the typehint you should not use pyqtSlot in a QRunnable method.
The pyqtSlot decorator is only used in methods of classes that inherit from QObject and QRunnable is not. I have reviewed several tutorials that resort to this bad practice since it does not bring any benefit.