pythonpyqt5decoratortype-hinting

How to correctly type-hint a function with `pyqtSlot()` decorator inside `QRunnable` class?


Status:

  1. I'm learning how to use concurrency/threads within PyQt library from here www.pythonguis.com. Below is the simplified/part of code from this tutorial.
  2. I want strict type hinting in my code. And I'm getting error messages on this code from mypy. (Shown on the 2-nd source-code block).

Question:

What I tried to solve:

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:


Solution

  • 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.