TypeError when calling parent class despite passing parameter

Question:

I have a class TextElement which inherits from PyQt-class QTextEdit and a custom class BaseElement.
BaseElement needs the parameter "transmitter" which is passed by the parameter "transmitter" of TextElement itself.

Despite doing everthing accordingly I get the TypeError:

Traceback (most recent call last):
  File "...HotTeacherpackagetblock.py", line 102, in insertTextElem
    TE = TextElement(self.transmitter, self)
  File "...HotTeacherelementstextelement.py", line 13, in __init__
    QTextEdit.__init__(self, parent=parent)
TypeError: BaseElement.__init__() missing 1 required positional argument: 'transmitter'

I checked if I forgot to pass the parameter "transmitter" when an instance of TextElement is created but everthing seems fine.

baseelement.py

class BaseElement:
    def __init__(self, transmitter) -> None:
        self.transmitter = transmitter
        self.isfocussed = False

textelement.py

class TextElement(QTextEdit, BaseElement):
    def __init__(self, transmitter, parent=None) -> None:
        print(transmitter) # Just for checking if something is passes
        QTextEdit.__init__(self, parent)
        BaseElement.__init__(self, transmitter)
        ...

This is where an instance of TextElement is created:

class BlockRow:
    def insertTextElem(self) -> None:
        TE = TextElement(self.transmitter, self)
        ...

I didn’t use super() to call the parent classes because both need different parameters.

So why am I getting this TypeError despite every parameter is passed seemingly fine?

I’ve also tried to pass the parameters like this:

class TextElement(QTextEdit, BaseElement):
    def __init__(self, transmitter, parent=None) -> None:
        print(transmitter) # Just for checking if something is passes
        QTextEdit.__init__(self, parent=parent)
        BaseElement.__init__(self, transmitter=transmitter)

But this didn’t work either.

Asked By: Deator

||

Answers:

I suspect that QTextEdit looks a little like this:

class QTextEdit:
    def __init__(self, parent) -> None:
        ...
        super().__init__()
        ...

The super().__init__() there, accidentally calls your BaseElement.__init__().

Swapping the classes over: class TextElement(BaseElement, QTextEdit): means that the super call no longer looks into BaseElement.

You can get a sense of what happens if you try: print(TextElement.mro()) for both versions of the base classes.

Answered By: quamrana
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.