typing: Slots can return non-None

Phil Thompson phil at riverbankcomputing.com
Wed Jul 5 13:44:49 BST 2023


On 29/06/2023 22:53, Florian Bruhin wrote:
> Hey,
> 
> In QtCore.pyi, PyQt6 claims that:
> 
>     PYQT_SLOT = typing.Union[typing.Callable[..., None], 
> pyqtBoundSignal]
> 
> which is the defined as the argument to pyqtBoundSignal.connect().
> 
> However, the None there is incorrect and should be Any: Slot functions
> can return something, the return value just gets ignored when it's used
> as a slot.
> 
> See e.g.:
> 
> 	from PyQt6.QtCore import QTimer, QCoreApplication
> 
> 	def fun() -> int:
> 		print("timeout")
> 		return 42
> 
> 	app = QCoreApplication([])
> 	t = QTimer()
> 	t.setInterval(1000)
> 	t.timeout.connect(fun)
> 	t.start()
> 	app.exec()
> 
> which works fine but fails mypy on "t.timeout.connect(fun)" with:
> 
>     error: Argument 1 to "connect" of "pyqtBoundSignal" has 
> incompatible
>     type "Callable[[], int]"; expected "Union[Callable[..., None],
>     pyqtBoundSignal]"  [arg-type]
> 
> Florian

Fixed.

Thanks,
Phil


More information about the PyQt mailing list