5

I've recently installed Pylance as python language server on VSCode. I'm building an app with PyQt5. In the script under question, I'm inheriting a UI python script automatically generated by pyqt5 uic. listProfiles is a QListView object generated in Ui_ProfileMainWindow

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

Pylance is capable of seeing the listProfiles attribute and recognizes it as a QListView object:

enter image description here

But no autocomplete is provided for it:

enter image description here

Furthermore, a when the mouse is over self.listProfiles, a generic Any type is pointed out for it:

enter image description here

I really can't figure out what's going on. I'd like to assume I've messed something up before thinking of a bug.

Since it's been a day I'm working with Pylance, I'd say that this was not happened as soon as I installed the language server, but I'm not sure about this piece of information, since I was working on another part of the script and, maybe, I just didn't realize that

No errors are in the code, since the app run and works properly.

EDIT 1

The problem is solved swapping the order of the inherited class:

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

Still can't figure out why and what's going on.

Buzz
  • 1,102
  • 1
  • 9
  • 24
  • `super` can lead to strange behavior with Multiple Inheritance (blabla Resolution), better to never use `super` and always call the method from the class you want, like `QMainWindow.__init__(self)` – rioV8 Apr 24 '21 at 11:29
  • does it work in a test script with self defined classes in the same file, does PyLance pick up the fields, In Qt all fields of all classes are hidden in a shadow object that is not visible to the outside world. Examine the py files where the Qt object are defined – rioV8 Apr 24 '21 at 11:30
  • @rioV8, tried substituting `QMainWindow.__init__(self)` and/or `Ui_ProfileMainWindow.__init__(self)` and does not work – Buzz Apr 24 '21 at 11:42
  • 1
    @Buzz Pylance probably implements autocompletion with some feature that is not supported by compiled libraries (written in C++) like pyqt. – eyllanesc Apr 24 '21 at 12:11
  • is `Ui_ProfileMainWindow` a `QMainWindow` or do they have a shared base class? my `super` comment is not a solution for the question but a general not-use advise – rioV8 Apr 24 '21 at 12:34
  • @rioV8, first, thank you for the advice! Just a clarification, hoping not being off topic. Should I instanziate both `QMainWindow.__init__(self)` and `Ui_ProfileMainWindow.__init__(self)` in this case? By the way, `Ui_ProfileMainWindow` is generated as superclass of `object` – Buzz Apr 24 '21 at 13:21
  • @eyllanesc, actually autocompletion works fine with pyqt objects. It was that specific case that was not working, and I'm still wondering where the actual issue is – Buzz Apr 24 '21 at 13:22
  • Any update on why changing the inheritance order makes pylance work? – Ronald Petit Feb 07 '22 at 17:51

0 Answers0