5

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
Sewbacca
  • 576
  • 6
  • 17
  • Since you're new to pyside and Qt6 is still pretty new (some classes and features have even been temporarily removed, and there are some bugs here and there), I'd think about using PySide2 right now if I were you. – musicamante May 13 '21 at 13:27
  • 1
    @musicamante I tested this exact setup except using pyside2 and the problem remains. Ill update the question. Thanks! – Sewbacca May 13 '21 at 13:38
  • unfortunately I don't use vscode, so I cannot help you on this. But consider that signals are not actual functions, they are objects that are bounded to the instances, which might be the reason for which they're not correctly recognized for the completion. – musicamante May 13 '21 at 14:11
  • @musicamante I basicly followed [this](https://www.youtube.com/watch?v=JL0H2xLdu2k) tutorial and he seemed to have completion for `clicked`, thats why i was wondering why i dont got it. I have also no idea if this is the "right" way to do it, since i dont have experience. The only thing i can say is, that `clicked` is in fact a object and `clicked.connect()` a member functions (which also is not being completed). – Sewbacca May 13 '21 at 15:21
  • @musicamante Im also not sure if this is a problem with vscode. I looked through the definitions and it seems like there arent any for `clicked`. I tried reinstalling pyside2/6 and it never popped up. In fact no field popped up. – Sewbacca May 13 '21 at 17:42

3 Answers3

5

You can generate the stubs manually by running

pyside6-genpyi all

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

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())
KerberosMorphy
  • 330
  • 1
  • 4
  • 14
1

Pylance tries to use stub files(which you can traverse with Go to definition) of PySide6 library to offer intellisense features. According to Qt docs clicked signal should be defined in QAbstractButton class but in type definitions (stubs) we can't see this signal definitions. If you look closely PyCharm gathers it from QAbstractButton class in your linked video but Pylance in Vs Code searches it in QPushButton.

This was a known issue in Pylance since PySide2 but according to the Pylance repo this may originate from Qt for Python's incorrect type definitions:

Libraries that have typing that's either incorrect or doesn't work well with PyLance. Best and maybe most known example I have is PySide2 (but see issues with other libraries in earlier posts): tons(!) of errors for correct, working code, which makes spotting real errors difficult. The same code in PyQt5 (which is basically a twin sibling to PySide2) doesn't raise any complaints from PyLance, so I'm assuming the problem is with PySide2's typing. While I'm aware that neither PySide2's typing nor PyLance's analysis results are necessarily incorrect, fact is that most of the reported errors should not be present, so for sake of simplicity I'll just call it incorrect.

The reason it can access at runtime is; it's created instance over shiboken QObject type I guess. I don't have any workarounds but it doesn't bother me because I prefer following official Qt documentation when looking for which signals available in a class definition.

EDIT: There was a resolved bug report on this which is the reason PyCharm able to handle auto-completion. Opening another on VS Code should help.

lymphatic
  • 91
  • 1
  • 6
  • Thanks for answering. It explained why switching to PyQt5 "solved" my Problem. Any idea which language server works with pyside2? – Sewbacca May 17 '21 at 15:35
  • I've tried couple of them before - with no luck. But re-opening [this](https://bugreports.qt.io/browse/PYSIDE-1079) bug report with vscode info should help. Because that is also shows how PyCharm able to do this :) – lymphatic May 18 '21 at 09:08
0

Since I had this exact same problem, I found this very informative comment on a PySide bug report: https://bugreports.qt.io/browse/PYSIDE-1603

Our stubs are auto-generated by introspection, signals are not included. At the moment, we need to discuss how we should support signals.

So, not a bug, just a missing feature.

maflAT
  • 41
  • 4