PySide6:信号未包含在键入文件中?(QtCore.pyi 等)

Phi*_*rch 7 qt typing pyside qt6 pyside6

考虑以下最小工作示例:

#!/usr/bin/env python3

from PySide6.QtCore import QTimer
from PySide6.QtWidgets import QLabel, QApplication

app = QApplication()
label = QLabel('Label')

x = 0

def timeout():
  global x
  x += 1
  label.setText(str(x))

timer = QTimer()
timer.timeout.connect(timeout)
timer.start(200)

label.show()
app.exec()
Run Code Online (Sandbox Code Playgroud)

它显示一个标签(一个非常小的窗口),其中包含一个递增计数器。这一切正常。但是,我使用 PyLance 扩展在 Visual Studio Code 中进行开发,并且 IDE 不知道以下timeout信号QTimer

time.timeout 工具提示

公平地说,按下F12打开QtCore.pyi的定义QTimer,这实际上不包含成员timeout

class QTimer(PySide6.QtCore.QObject):

    def __init__(self, parent:Optional[PySide6.QtCore.QObject]=...) -> None: ...

    def interval(self) -> int: ...
    def isActive(self) -> bool: ...
    def isSingleShot(self) -> bool: ...
    def killTimer(self, arg__1:int) -> None: ...
    def remainingTime(self) -> int: ...
    def setInterval(self, msec:int) -> None: ...
    def setSingleShot(self, singleShot:bool) -> None: ...
    def setTimerType(self, atype:PySide6.QtCore.Qt.TimerType) -> None: ...
    @overload
    @staticmethod
    def singleShot(arg__1:int, arg__2:Callable) -> None: ...
    @overload
    @staticmethod
    def singleShot(msec:int, receiver:PySide6.QtCore.QObject, member:bytes) -> None: ...
    @overload
    @staticmethod
    def singleShot(msec:int, timerType:PySide6.QtCore.Qt.TimerType, receiver:PySide6.QtCore.QObject, member:bytes) -> None: ...
    @overload
    def start(self) -> None: ...
    @overload
    def start(self, msec:int) -> None: ...
    def stop(self) -> None: ...
    def timerEvent(self, arg__1:PySide6.QtCore.QTimerEvent) -> None: ...
    def timerId(self) -> int: ...
    def timerType(self) -> PySide6.QtCore.Qt.TimerType: ...
Run Code Online (Sandbox Code Playgroud)

这显然不限于,QTimer但也适用,例如 for QAbstractButton,其中不包括clickedpressedreleasedtoggled简而言之,看起来好像物体上的所有信号都丢失了。

这里发生了什么?pip install pyside6除了获取打字包含中定义的所有信号之外,我还需要安装其他东西吗?当然,这不是一个令人震惊的事情,但如果 IDE 了解对象的所有成员,就会更方便。

如果重要的话,我使用 Ubuntu 20.04 的库存 python3:

$ python3
Python 3.8.10 (default, Nov 26 2021, 20:14:08) 
[GCC 9.3.0] on linux
Run Code Online (Sandbox Code Playgroud)

小智 1

根据 Jira 的说法,这个问题已在PYSIDE-1603中得到解决,并在 6.4.3 中修复。

QTimer 存根现在包括:

class QTimer(PySide6.QtCore.QObject):
     timeout                  : ClassVar[Signal] = ... # timeout()
Run Code Online (Sandbox Code Playgroud)

仍然不太好,因为理想情况下 Signal 应该是通用的,因此仍然没有键入 的参数emit()和函数。connect()然而,完整的Signal签名作为评论包含在内:

class QObject(Shiboken.Object):
    destroyed                : ClassVar[Signal] = ... # destroyed()
    objectNameChanged        : ClassVar[Signal] = ... # objectNameChanged(QString)
Run Code Online (Sandbox Code Playgroud)