PySide6:typing文件中没有包含信号?(例如QtCore.pyi等)

7
考虑以下最小工作示例:
#!/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()

它显示一个标签(一个非常小的窗口),其中包含一个递增的计数器。这很好地运作。然而,我在使用PyLance扩展和Visual Studio Code进行开发,IDE不知道QTimertimeout信号。

time.timeout tooltip

好的,按下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: ...

这似乎不仅限于 QTimer,也适用于 QAbstractButton,其中不包括 clicked, pressed, releasedtoggled 等信号。简而言之,所有信号都似乎从对象中消失了。

这是怎么回事?除了 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

2
最终找到了另一个提到这个问题的地方。显然,PySide开发人员已经知道了这个问题,并正在解决它。请参见https://bugreports.qt.io/browse/PYSIDE-1675以及此问题所依赖的其他问题。 - Philipp Burch
2个回答

1
这个问题已经在PYSIDE-1603中得到解决,并且在Jira中的6.4.3版本中修复。
QTimer stubs现在包括:
class QTimer(PySide6.QtCore.QObject):
     timeout                  : ClassVar[Signal] = ... # timeout()

仍然不太理想,因为 Signal 最好应该是一个通用的类型,所以 emit() 函数和 connect() 函数的参数仍然没有类型。但是完整的 Signal 签名已经作为注释包含在内:

class QObject(Shiboken.Object):
    destroyed                : ClassVar[Signal] = ... # destroyed()
    objectNameChanged        : ClassVar[Signal] = ... # objectNameChanged(QString)

0
这个问题已经存在很长时间了,即使在PyQt中也是如此。我认为像clicked、pressed这样的“slot”将成为实例的属性,并且在Qt程序运行时将被动态链接库使用。在Python代码中所做的就是分配稍后将使用的属性,因此它不会出现在类型文件中,这使得Pylance无法识别它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接