首页 > 解决方案 > VSCode + Pylance:已识别类属性但没有可用的自动完成功能

问题描述

我最近在 VSCode 上安装了 Pylance 作为 Python 语言服务器。我正在用 PyQt5 构建一个应用程序。在有问题的脚本中,我继承了由 pyqt5 uic 自动生成的 UI python 脚本。listProfilesQListViewUi_ProfileMainWindow

class ProfileMainWindow(QMainWindow, Ui_ProfileMainWindow):
    def __init__(self, model = None):
        super().__init__()
        self.setupUi(self)
        if model:
            self.listProfiles.setModel(model)

Pylance 能够看到listProfiles属性并将其识别为QListView对象:

在此处输入图像描述

但是没有为它提供自动完成功能:

在此处输入图像描述

此外,当鼠标悬停时,会为其指出self.listProfiles一个泛型类型:Any

在此处输入图像描述

我真的不知道发生了什么事。我想假设在考虑错误之前我已经搞砸了。

自从我与 Pylance 合作了一天,我会说这不是在我安装语言服务器后立即发生的,但我不确定这条信息,因为我正在研究另一部分剧本,也许,我只是没有意识到

代码中没有错误,因为应用程序运行正常。

编辑 1

交换继承类的顺序即可解决问题:

class ProfileMainWindow(Ui_ProfileMainWindow,QMainWindow):
    def __init__(self, model = None):
        super().__init__()
        self.setupUi(self)
        if model:
            self.listProfiles.setModel(model)

仍然无法弄清楚为什么以及发生了什么。

标签: pythonvisual-studio-codepyqtpylance

解决方案


推荐阅读