1

Here's the traceback:

Traceback (most recent call last):
  File "/home/neon/pyqt6/myqt/src/app.py", line 13, in <module>
    window = AppWindow()
             ^^^^^^^^^^^
  File "/home/neon/pyqt6/myqt/src/app.py", line 7, in __init__
    super(AppWindow, self).__init__()
  File "/home/neon/pyqt6/myqt/src/ui_mainwindow.py", line 5, in __init__
    print(self)
RuntimeError: '__init__' method of object's base class (AppWindow) not called.

I'm trying to develop a personal QT6 based app in Python3.11. At some point I got stuck somewhere and decided to print(self) in one of the parents of a subclass, and that's what caused the above runtime error.

I've seen the duplicate. I have made attempts below to show why this runtime error should not happen in my opinion.

Here's how to reproduce the error:

  1. Install PySide6 module in a virtual environment
  2. Make file ui_mainwindow.py with following content:
from PySide6.QtWidgets import QLineEdit

class Ui_MainWindow(object):
    def __init__(self):
        # This call causes error
        print(self)
        
    # A sample widget. Irrelevant for error
    def makeui(self):
        self.line_edit = QLineEdit()
  1. Make file app.py with following content:
from PySide6.QtWidgets import QMainWindow, QApplication
from ui_mainwindow import Ui_MainWindow
import sys

class AppWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(AppWindow, self).__init__()
        self.makeui()
        self.setCentralWidget(self.line_edit)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = AppWindow()
    window.show()
    app.exec()
  1. Run app.py as python app.py and see the error reproduced.

Here's something unexpected. If I replace the call print(self) with anything else, let's say print(self.__class__.mro()) or print("Hello World") then no error occurs and print call works!

I'm unable to make sense of why print(self) is causing runtime error with that message.


I also tried to replicate the problem without qt6 and with only python based objects. Here's what I also tried and no error occurred even when print(self) was called in one parent's constructor.

  1. Contents of main.py
from class_d import D

class A(object): 
    def __init__(self):
        super().__init__()

class B(A):
    def __init__(self):
        super().__init__()

class C(B):
    def __init__(self):
        super().__init__()

class E(C, D):
    def __init__(self):
        super(E, self).__init__()

e = E()
  1. Contents of class_d.py
class D(object):
    def __init__(self):
        print(self)  # this call works as expected

Doing python main.py happily prints the repr of E object.

So why is that when qt objects are involved I am getting runtime error?

user47
  • 1,080
  • 11
  • 28
  • When you're using multiple inheritance, *every* class's `__init__()` has to call `super().__init__()` - even the ones that derive from a basic class like `object` that wouldn't appear to need it (because that's not necessarily the class that will be found by `super()`). As for why `print(self)` triggers an error message, I would guess that something in a `__str__()` method actually notices that the class isn't properly initialized. – jasonharper May 14 '23 at 15:21
  • I have something bizarre to report. In the MRO I can clearly see that `Ui_MainWindow` is far away in the resolution order, and that the immediate parent in MRO is `QMainWindow`. Therefore, its own init should be invoked by Python first. However, when I debugged using pdb, I noticed that the next instruction executed after `super(args)` call in `AppWindow` directly jumped to `Ui_MainWindow` init. When I ran `whatis self` it printed corrected result. Executing `p self` caused runtime error. – user47 May 14 '23 at 15:26
  • @user47 I cannot reproduce with PyQt6, but, internally, PySide and PyQt are not the same, and both do some "magic" when initializing instances, so you cannot rely on basic examples based on pure Python `objects`. In any case, did you actually try to put the missing `super().__init__()` in the ui class? Also note that if that is a class generated by the uic tool, you're not expected to modify it anyway to begin with. – musicamante May 14 '23 at 18:22
  • @musicamante Yes, I did try with `super().__init__()` in ui class. Didn't help. – user47 May 14 '23 at 20:00
  • 2
    This is a bug in PySide6. It's actually `repr` that is the source of the problem, rather than `print`. The bug does not occur in PySide2 or PyQt5/6. – ekhumoro May 14 '23 at 22:31

0 Answers0