Search code examples
pythonvisual-studio-codepysidepyside2

How to properly setup vscode with pyside? Missing suggestions


I'm very new to pyside, qt and python. I managed to setup a project with a basic window and a push button which closes the app. My problem is, that somehow vscode won't show all properties available, even though the code runs with them fine. vscode-pyside2-problem-1.png Note how a bunch of other properties are suggested, except for the signal clicked. If I hover over clicked, it tells me clicked: Any Only during debugging, vscode tells me what clicked is: vscode-pyside2-problem-2.png Current setup:

  • OS: Linux
  • Virtualenv with pyside2 installed
  • Using ms-python.python and ms-python.vscode-pylance
  • ui/MainWindow.ui file and corresponding generated ui/MainWindow_ui.ui file with pyside2-uic

Solution

  • You can generate the stubs manually by running

    pyside6-genpyi all
    

    PySide2 have the same tools, it's not just for PySide6.

    EDIT: PySide2 didn't seems to have to same tooling, there's a new pyside2-stubs since May 21, 2022, https://pypi.org/project/PySide2-stubs/

    I use PDM to handle my project, with the last one I execute:

    pdm run pyside6-genpyi all
    

    And PyLance detect all my stubs:

    enter image description here

    If you got error with the Signal, it's because Qt/PySide do some magic tricks and convert Signal to SignalInstance. My solution is to cast SignalInstance to get to right hint.

    from typing import cast
    from PySide6.QtCore import Signal, SignalInstance
    
    class ConnectionPanel(QtWidgets.QWidget):
        connected = cast(SignalInstance, Signal())
        disconnected = cast(SignalInstance, Signal())