Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. TypeError: __init__() missing 1 required positional argument: 'item'

TypeError: __init__() missing 1 required positional argument: 'item'

Scheduled Pinned Locked Moved Unsolved Qt for Python
9 Posts 3 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrAWD
    wrote on 16 Oct 2024, 23:36 last edited by
    #1

    Interesting problem here and I hope there would be some help with this. I have a simple test script that looks right to me and worked with PySide2. Now that I have switched to PySide6, this is giving me an error instead. Here is the code:

    from PySide6.QtWidgets import QApplication, QLineEdit, QMainWindow
    
    class HelperClass:
        def __init__(self, item):
            self.item = item
    
    class myClass(QLineEdit, HelperClass):
        def __init__(self, item, parent=None):
            HelperClass.__init__(self, item)
            QLineEdit.__init__(self, parent)
    
    class MyWindow(QMainWindow):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("pySide6 example")
            self.label = myClass("item")
            self.setCentralWidget(self.label)
    
    if __name__ == "__main__":
        app = QApplication([])
        win = MyWindow()
        win.show()
        app.exec()
    

    For some reason, I am getting this traceback when I try to run it:

    Traceback (most recent call last):
      File "C:\pyProjects\freedom\pyClarity\MyTesting\Z2.py", line 31, in <module>
        win = MyWindow()
      File "C:\pyProjects\freedom\pyClarity\MyTesting\Z2.py", line 25, in __init__
        self.label = myClass("item")
      File "C:\pyProjects\freedom\pyClarity\MyTesting\Z2.py", line 18, in __init__
        QLineEdit.__init__(self, parent)
    TypeError: __init__() missing 1 required positional argument: 'item'
    

    Any idea why this is happening here? What has changed with the pySide6 to cause this not to work?

    J 1 Reply Last reply 18 Oct 2024, 09:59
    0
    • M Offline
      M Offline
      MrAWD
      wrote on 17 Oct 2024, 13:00 last edited by MrAWD
      #2

      If I replace line:

      QLineEdit.__init__(self, parent)
      

      with this:

      QLineEdit.__init__(self, parent=parent)
      

      I get a different error:

      Traceback (most recent call last):
        File "C:\MyTesting\Z2.py", line 34, in <module>
          win = MyWindow()
        File "C:\MyTesting\Z2.py", line 28, in __init__
          self.label = myClass("item", parent=self)
        File "C:\MyTesting\Z2.py", line 21, in __init__
          super().__init__(parent=parent)
      TypeError: __init__() got an unexpected keyword argument 'parent'
      

      This on its own is strange as well, since this is how init is defined in the code (here is a snippet that defines that part) and both constructors have a parent defined there (code from QtWidgets.pyi):

      class QLineEdit(PySide6.QtWidgets.QWidget):
          @typing.overload
          def __init__(self, arg__1: str, parent: PySide6.QtWidgets.QWidget | None= ...) -> None: ...
          @typing.overload
          def __init__(self, parent: PySide6.QtWidgets.QWidget | None= ...) -> None: ...
      

      Another thing that I have tried was to call myClass with self passed in as a parent argument and that didn't help either. Here is that line:

      self.label = myClass("item", self) # or parent=self
      
      1 Reply Last reply
      0
      • M Offline
        M Offline
        MrAWD
        wrote on 17 Oct 2024, 14:10 last edited by MrAWD
        #3

        Apparently, if I add a second argument to the QLineEdit init invocation method, it works!???

        So, this code is running correctly:

        from PySide6.QtWidgets import QApplication, QLineEdit, QMainWindow
        
        class HelperClass:
            def __init__(self, item):
                self.item = item
        
        class myClass(QLineEdit, HelperClass):
            def __init__(self, item, parent=None):
                HelperClass.__init__(self, item)
                QLineEdit.__init__(self, parent, item=item) # <- change was made here!!
        
        class MyWindow(QMainWindow):
            def __init__(self):
                super().__init__()
                self.setWindowTitle("pySide6 example")
                self.label = myClass("item")
                self.setCentralWidget(self.label)
        
        if __name__ == "__main__":
            app = QApplication([])
            win = MyWindow()
            win.show()
            app.exec()
        
        

        I wish I would understand how this magic works, but...

        P 1 Reply Last reply 17 Oct 2024, 14:40
        0
        • M MrAWD
          17 Oct 2024, 14:10

          Apparently, if I add a second argument to the QLineEdit init invocation method, it works!???

          So, this code is running correctly:

          from PySide6.QtWidgets import QApplication, QLineEdit, QMainWindow
          
          class HelperClass:
              def __init__(self, item):
                  self.item = item
          
          class myClass(QLineEdit, HelperClass):
              def __init__(self, item, parent=None):
                  HelperClass.__init__(self, item)
                  QLineEdit.__init__(self, parent, item=item) # <- change was made here!!
          
          class MyWindow(QMainWindow):
              def __init__(self):
                  super().__init__()
                  self.setWindowTitle("pySide6 example")
                  self.label = myClass("item")
                  self.setCentralWidget(self.label)
          
          if __name__ == "__main__":
              app = QApplication([])
              win = MyWindow()
              win.show()
              app.exec()
          
          

          I wish I would understand how this magic works, but...

          P Online
          P Online
          Pl45m4
          wrote on 17 Oct 2024, 14:40 last edited by Pl45m4
          #4

          @MrAWD

          I fail to understand what you are doing there at all...
          Why does your myClass "inherit" from (or is type) QLineEdit and HelperClass which only hold the item and you try to pass the item (as string = "item") also to your QLineEdit ?

          You init myClass and pass args for your lineEdit and your helperClass, ok, but what should the QLineEdit do with your "item"?
          If you want your "item" to be the text of the QLineEdit, why the HelperClass?

          @MrAWD said in TypeError: __init__() missing 1 required positional argument: 'item':

              QLineEdit.__init__(self, parent, item=item) # <- change was made here!!
          

          This works because it changes the position/order of arguments.

          As you posted above, the correct order is

          def __init__(self, arg__1: str, parent: PySide6.QtWidgets.QWidget | None= ...) -> None:
          

          So (self, str, QWidget), without the keyword, you had in this order

          __init__(self, parent, item)
          

          parent does not match Str arg, and item does not match QWidget type

          What version of PySide6 are you using?

          It could be related to

          • https://forum.qt.io/topic/144262/typeerror-with-pyside-6-5-0-for-multiple-inheritance-of-mainwindow

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          M 1 Reply Last reply 17 Oct 2024, 15:34
          1
          • P Pl45m4
            17 Oct 2024, 14:40

            @MrAWD

            I fail to understand what you are doing there at all...
            Why does your myClass "inherit" from (or is type) QLineEdit and HelperClass which only hold the item and you try to pass the item (as string = "item") also to your QLineEdit ?

            You init myClass and pass args for your lineEdit and your helperClass, ok, but what should the QLineEdit do with your "item"?
            If you want your "item" to be the text of the QLineEdit, why the HelperClass?

            @MrAWD said in TypeError: __init__() missing 1 required positional argument: 'item':

                QLineEdit.__init__(self, parent, item=item) # <- change was made here!!
            

            This works because it changes the position/order of arguments.

            As you posted above, the correct order is

            def __init__(self, arg__1: str, parent: PySide6.QtWidgets.QWidget | None= ...) -> None:
            

            So (self, str, QWidget), without the keyword, you had in this order

            __init__(self, parent, item)
            

            parent does not match Str arg, and item does not match QWidget type

            What version of PySide6 are you using?

            It could be related to

            • https://forum.qt.io/topic/144262/typeerror-with-pyside-6-5-0-for-multiple-inheritance-of-mainwindow
            M Offline
            M Offline
            MrAWD
            wrote on 17 Oct 2024, 15:34 last edited by
            #5

            @Pl45m4 said in TypeError: __init__() missing 1 required positional argument: 'item':

            @MrAWD

            I fail to understand what you are doing there at all...
            Why does your myClass "inherit" from (or is type) QLineEdit and HelperClass which only hold the item and you try to pass the item (as string = "item") also to your QLineEdit ?

            I have created this script to represent an issue that I have in my real code, which is proprietary and thousands of lines long. HelperClass is an interface and also a base class that handles item (which is also of another custom type)

            @Pl45m4 said in TypeError: __init__() missing 1 required positional argument: 'item':

            You init myClass and pass args for your lineEdit and your helperClass, ok, but what should the QLineEdit do with your "item"?
            If you want your "item" to be the text of the QLineEdit, why the HelperClass?

            That is the puzzling piece there! QLineEdit doesn't know nothing about the item and it shouldn't really needed it. But, when code is written without it, I get the original error.
            Only with item=item passed to it it works.

            And, I have tried to use that line:

            QLineEdit.__init__(self, parent)
            

            as this:

            QLineEdit.__init__(self, "initial text for this edit box", parent)
            

            and that still fails. If I add item=item to that entry, I do get it to work and that text is visible in QLineEdit widget. So, this works as well:

            QLineEdit.__init__(self, "initial text for this edit box", parent, item=item)
            

            @Pl45m4 said in TypeError: __init__() missing 1 required positional argument: 'item':

                QLineEdit.__init__(self, parent, item=item) # <- change was made here!!
            

            This works because it changes the position/order of arguments.

            As you posted above, the correct order is

            def __init__(self, arg__1: str, parent: PySide6.QtWidgets.QWidget | None= ...) -> None:
            

            So (self, str, QWidget), without the keyword, you had in this order

            __init__(self, parent, item)
            

            parent does not match Str arg, and item does not match QWidget type

            if you see the above comment (where I added another string before the parent argument), you would see that this is also not working as expected. There are two constructors there and second one should have picked my original example as written. But, it doesn't

            When I change that line to this:

            QLineEdit.__init__(self, "xxx", parent=parent)  # , item=item)
            

            I get this error again:

            Traceback (most recent call last):
              File "C:\MyTesting\Z2.py", line 34, in <module>
                win = MyWindow()
              File "C:\MyTesting\Z2.py", line 28, in __init__
                self.label = myClass("item", parent=self)  # .centralwidget)
              File "C:\MyTesting\Z2.py", line 20, in __init__
                QLineEdit.__init__(self, "xxx", parent=parent)  # , item=item)
            TypeError: __init__() got an unexpected keyword argument 'parent'
            

            @Pl45m4 said in TypeError: __init__() missing 1 required positional argument: 'item':

            What version of PySide6 are you using?

            I am using 6.8.0 right now

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MrAWD
              wrote on 17 Oct 2024, 16:00 last edited by
              #6

              One more interesting thing is that even with the line:

              QLineEdit.__init__(self, "xxx", parent=parent, item=item)
              

              This is still breaking with this error:

              Traceback (most recent call last):
                File "C:\MyTesting\Z2.py", line 34, in <module>
                  win = MyWindow()
                File "C:\MyTesting\Z2.py", line 28, in __init__
                  self.label = myClass("item", parent=self)  # .centralwidget)
                File "C:\MyTesting\Z2.py", line 20, in __init__
                  QLineEdit.__init__(self, "xxx", parent=parent, item=item)
              TypeError: __init__() got an unexpected keyword argument 'parent'
              

              Once that parent=parent is changed to just parent, it works as expected

              J 1 Reply Last reply 17 Oct 2024, 17:45
              0
              • M MrAWD
                17 Oct 2024, 16:00

                One more interesting thing is that even with the line:

                QLineEdit.__init__(self, "xxx", parent=parent, item=item)
                

                This is still breaking with this error:

                Traceback (most recent call last):
                  File "C:\MyTesting\Z2.py", line 34, in <module>
                    win = MyWindow()
                  File "C:\MyTesting\Z2.py", line 28, in __init__
                    self.label = myClass("item", parent=self)  # .centralwidget)
                  File "C:\MyTesting\Z2.py", line 20, in __init__
                    QLineEdit.__init__(self, "xxx", parent=parent, item=item)
                TypeError: __init__() got an unexpected keyword argument 'parent'
                

                Once that parent=parent is changed to just parent, it works as expected

                J Offline
                J Offline
                JonB
                wrote on 17 Oct 2024, 17:45 last edited by
                #7

                @MrAWD
                I agree I do not understand the behaviour, and the fact that something has changed from PySide2 to PySide6. When you tested with these two were you using exact same Python version, or the Python which was current when they were current?

                Are you able to test with PyQt6?

                If I remember I might see how it behaves for me tomorrow...

                M 1 Reply Last reply 17 Oct 2024, 19:07
                0
                • J JonB
                  17 Oct 2024, 17:45

                  @MrAWD
                  I agree I do not understand the behaviour, and the fact that something has changed from PySide2 to PySide6. When you tested with these two were you using exact same Python version, or the Python which was current when they were current?

                  Are you able to test with PyQt6?

                  If I remember I might see how it behaves for me tomorrow...

                  M Offline
                  M Offline
                  MrAWD
                  wrote on 17 Oct 2024, 19:07 last edited by
                  #8

                  @JonB said in TypeError: __init__() missing 1 required positional argument: 'item':

                  @MrAWD
                  I agree I do not understand the behaviour, and the fact that something has changed from PySide2 to PySide6. When you tested with these two were you using exact same Python version, or the Python which was current when they were current?

                  Are you able to test with PyQt6?

                  If I remember I might see how it behaves for me tomorrow...

                  I was using 3.9.9 for both PySide2 and PySide6.

                  I have not tested with the PyQt6

                  1 Reply Last reply
                  0
                  • M MrAWD
                    16 Oct 2024, 23:36

                    Interesting problem here and I hope there would be some help with this. I have a simple test script that looks right to me and worked with PySide2. Now that I have switched to PySide6, this is giving me an error instead. Here is the code:

                    from PySide6.QtWidgets import QApplication, QLineEdit, QMainWindow
                    
                    class HelperClass:
                        def __init__(self, item):
                            self.item = item
                    
                    class myClass(QLineEdit, HelperClass):
                        def __init__(self, item, parent=None):
                            HelperClass.__init__(self, item)
                            QLineEdit.__init__(self, parent)
                    
                    class MyWindow(QMainWindow):
                        def __init__(self):
                            super().__init__()
                            self.setWindowTitle("pySide6 example")
                            self.label = myClass("item")
                            self.setCentralWidget(self.label)
                    
                    if __name__ == "__main__":
                        app = QApplication([])
                        win = MyWindow()
                        win.show()
                        app.exec()
                    

                    For some reason, I am getting this traceback when I try to run it:

                    Traceback (most recent call last):
                      File "C:\pyProjects\freedom\pyClarity\MyTesting\Z2.py", line 31, in <module>
                        win = MyWindow()
                      File "C:\pyProjects\freedom\pyClarity\MyTesting\Z2.py", line 25, in __init__
                        self.label = myClass("item")
                      File "C:\pyProjects\freedom\pyClarity\MyTesting\Z2.py", line 18, in __init__
                        QLineEdit.__init__(self, parent)
                    TypeError: __init__() missing 1 required positional argument: 'item'
                    

                    Any idea why this is happening here? What has changed with the pySide6 to cause this not to work?

                    J Offline
                    J Offline
                    JonB
                    wrote on 18 Oct 2024, 09:59 last edited by JonB
                    #9

                    @MrAWD said in TypeError: __init__() missing 1 required positional argument: 'item':

                    TypeError: __init__() missing 1 required positional argument: 'item'

                    Well, I tried and PyQt6 reports same error. So this is not a PySide-only issue. But the PyQt6 gives a surprising clue:

                    jon@ubuntu-22:~/QtTests/PyQt$ python3 multiinherit.py 
                    Traceback (most recent call last):
                      File "/home/jon/QtTests/PyQt/multiinherit.py", line 21, in <module>
                        win = MyWindow()
                              ^^^^^^^^^^
                      File "/home/jon/QtTests/PyQt/multiinherit.py", line 16, in __init__
                        self.label = myClass("item")
                                     ^^^^^^^^^^^^^^^
                      File "/home/jon/QtTests/PyQt/multiinherit.py", line 10, in __init__
                        QLineEdit.__init__(self, parent)
                    TypeError: HelperClass.__init__() missing 1 required positional argument: 'item'
                    

                    So although the error is shown beneath QLineEdit.__init__(self, parent) and PySide6 just says __init__() missing 1 required positional, PyQt6 says

                    HelperClass.__init__() missing 1 required positional

                    So it is complaining about HelperClass.__init__() not QLineEdit.__init__()....

                    I am not a regular Python dev so I will leave it at that.

                    UPDATE
                    On a whim/hunch, I tried changing the order of your inheritance to:

                    class myClass(HelperClass, QLineEdit)
                    

                    Lo and behold, this works fine under both PySide & PyQt!

                    Now, it may be you were getting away with something which ought never to have worked on old PySide2. In C++ if you want to multi-inherit from a QObject type and a non-QObject type (not allowed to multi-inherit from QObject publicly) you must place the QObject one first in the list, to keep moc happy (https://doc.qt.io/qt-6/moc.html#multiple-inheritance-requires-qobject-to-be-first). To be fair, that is exactly what you non-working original did. I am finding the opposite is required to keep PyQt6/PySide6 happy here!

                    • Check that with the re-arranged order everything else, especially the QLineEdit functionality/signals, work.
                    • If so, my finding is that the reverse of what C++/moc wants is required for Py...! Go figure :)

                    P.S.
                    The error might have some relationship to https://forum.qt.io/post/755209

                    We have hit this issue too. PySide 6.5.0 appears to use the signature of the last super-class that is initialised for both of them.

                    I know you are not using super(), but when it goes wrong in your original order maybe this is related to the "using the last signature encountered". Or, you might want to look at Christian Tismer's answer in https://bugreports.qt.io/browse/PYSIDE-2294 where he suggests combining the two separate __init__()s into a single

                    super().__init__(a_value=a_value, b_value=b_value)
                    

                    You might also report your original code as a "bug" to the PySide folks and see what they have to say. Though the fact that it behaves the same as PyQt may indicate it is not their problem.

                    1 Reply Last reply
                    3

                    8/9

                    17 Oct 2024, 19:07

                    • Login

                    • Login or register to search.
                    8 out of 9
                    • First post
                      8/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved