TypeError: __init__() missing 1 required positional argument: 'item'
-
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?
-
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
-
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...
-
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...
I fail to understand what you are doing there at all...
Why does yourmyClass
"inherit" from (or is type)QLineEdit
andHelperClass
which only hold theitem
and you try to pass theitem
(as string = "item") also to yourQLineEdit
?You init
myClass
and pass args for your lineEdit and your helperClass, ok, but what should theQLineEdit
do with your "item"?
If you want your "item" to be the text of theQLineEdit
, why theHelperClass
?@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
-
I fail to understand what you are doing there at all...
Why does yourmyClass
"inherit" from (or is type)QLineEdit
andHelperClass
which only hold theitem
and you try to pass theitem
(as string = "item") also to yourQLineEdit
?You init
myClass
and pass args for your lineEdit and your helperClass, ok, but what should theQLineEdit
do with your "item"?
If you want your "item" to be the text of theQLineEdit
, why theHelperClass
?@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
@Pl45m4 said in TypeError: __init__() missing 1 required positional argument: 'item':
I fail to understand what you are doing there at all...
Why does yourmyClass
"inherit" from (or is type)QLineEdit
andHelperClass
which only hold theitem
and you try to pass theitem
(as string = "item") also to yourQLineEdit
?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 theQLineEdit
do with your "item"?
If you want your "item" to be the text of theQLineEdit
, why theHelperClass
?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
-
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
-
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
@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...
-
@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...
@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
-
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?
@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 saysHelperClass.__init__() missing 1 required positional
So it is complaining about
HelperClass.__init__()
notQLineEdit.__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 fromQObject
publicly) you must place theQObject
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/755209We 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 singlesuper().__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.
- Check that with the re-arranged order everything else, especially the