Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError when calling parent class despite passing parameter

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 "...\HotTeacher\package\tblock.py", line 102, in insertTextElem
    TE = TextElement(self.transmitter, self)
  File "...\HotTeacher\elements\textelement.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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading