Wrong type annotation of pyqtSlot in PyQt5

Kyle Altendorf sda at fstab.net
Thu Sep 21 12:42:11 BST 2023


On Thu, Sep 21, 2023, at 06:01, Alexander Hedges wrote:
> Hi,
> 
> Currently in PyQt5 (as of the latest development build) the pyqtSlot decorator
> is typed as:
> 
> ```python
> def pyqtSlot(*args: typing.Any, name: typing.Optional[str] = ...,
> result: typing.Optional[str] = ...) -> typing.Callable[...,
> typing.Optional[str]]: ...
> ```
> 
> Which is an incorrect annotation given that this function is a decorator (it
> should return a function that takes a function and returns a function).
> 
> In PyQt6 this is currently correctly annotated as returning a function that
> takes a function and returns a function with the same signature:
> 
> ```python
> FuncT = typing.TypeVar('FuncT', bound=typing.Callable)
> def pyqtSlot(*types, name: typing.Optional[str] = ..., result:
> typing.Optional[str] = ...) -> typing.Callable[[FuncT], FuncT]: ...
> ```
> 
> It would be great if you could fix the stub file in PyQt5.

For what it's worth, here is where we ended up in PyQt5-stubs.  I know there were some aspects of signals and slots that I didn't quite get how I wanted, but I think we got the first layers anyways.

https://github.com/python-qt-tools/PyQt5-stubs/blob/50475496654268dfe9f2b1755b6270a619e4f339/PyQt5-stubs/QtCore.pyi#L13119

@typing.overload
def pyqtSlot(*types: typing.Any) -> typing.Callable[[FuncT[T]], FuncT[T]]: ...
@typing.overload
def pyqtSlot(*types: typing.Any, name: str) -> typing.Callable[[FuncT[T]], FuncT[T]]: ...
@typing.overload
def pyqtSlot(*types: typing.Any, result: typing.Type[T]) -> typing.Callable[[FuncT[T]], FuncT[T]]: ...

<etc>

We aren't particularly active these days, but there were a lot of improvements made in PyQt5-stubs for the hints.

Cheers,
-kyle


More information about the PyQt mailing list