Maximilian Wolf
Maximilian Wolf

Reputation: 83

__init__ method missing positional argument even though it is clearly passed

I have a custom widget PlaylistView in kivy which should take another custom widget as argument for its init method:

class PlaylistView(Widget):
    def __init__(self, node, **kwargs):  # (originally:) node: GridNode,...  (custom node, removed for min. reproducable exmple but still yields the same error)
        super().__init__(**kwargs)
        self.node = node
        ...

But whenever I try to create an instance of it, for example: PlaylistView(1), PlaylistView(GridNode('test')) or PlaylistView("test") (no matter what I pass as argument), I get the following error:

 Traceback (most recent call last):
   File "C:/Users/Maximilian.Wolf/PycharmProjects/MiaudioPyler/main.py", line 215, in <module>
     PlaylistView("test")
   File "C:/Users/Maximilian.Wolf/PycharmProjects/MiaudioPyler/main.py", line 157, in __init__
     super().__init__(**kwargs)
   File "C:\Users\Maximilian.Wolf\Miniconda3\envs\MiaudioPyler\lib\site-packages\kivy\uix\widget.py", line 359, in __init__
     self.apply_class_lang_rules(
   File "C:\Users\Maximilian.Wolf\Miniconda3\envs\MiaudioPyler\lib\site-packages\kivy\uix\widget.py", line 463, in apply_class_lang_rules
     Builder.apply(
   File "C:\Users\Maximilian.Wolf\Miniconda3\envs\MiaudioPyler\lib\site-packages\kivy\lang\builder.py", line 541, in apply
     self._apply_rule(
   File "C:\Users\Maximilian.Wolf\Miniconda3\envs\MiaudioPyler\lib\site-packages\kivy\lang\builder.py", line 663, in _apply_rule
     self._apply_rule(
   File "C:\Users\Maximilian.Wolf\Miniconda3\envs\MiaudioPyler\lib\site-packages\kivy\lang\builder.py", line 659, in _apply_rule
     child = cls(__no_builder=True)
 TypeError: __init__() missing 1 required positional argument: 'node'

I also tried using node=None in init, same results.

Could this be something to do with kivy or how I call super().__init__?
EDIT
I tried this seperately, which works, still no clue what's causing the error.

from kivy.uix.widget import Widget

class PlaylistView(Widget):
    def __init__(self, node, **kwargs):
        super().__init__(**kwargs)
        self.node = node

PlaylistView(1)

EDIT 2 More info: What I did and wasn't showing in the example is loading a bunch of kv-files with Builder.load_file(... Commenting this out also makes the error go away.

Upvotes: 1

Views: 584

Answers (3)

user23124729
user23124729

Reputation: 1

If you are used docsig as dev package or flake8 with dependency with docsig and raised error like like below during pre-commit run or commit changes SIG203 parameters missing (params-missing) 'ClassSample.__init__' Then put all __init__ method's input parameter docstring into class their class.

Ex: WRONG:

Class Sample1:
  def __init__(a, b):
    """
    This is the constructor of the class `Sample1`.
    :param a: int
    :param b: int
    """

RIGHT:

Class Sample1:
  """
    This is the `Sample1` class.
    :param a: int
    :param b: int
  """
  def __init__(a, b):
    """
    This is the constructor of the class `Sample1`.
    """

Upvotes: 0

azelcer
azelcer

Reputation: 1533

The traceback must be read from top to bottom. The error is is file "C:\Users\Maximilian.Wolf\Miniconda3\envs\MiaudioPyler\lib\site-packages\kivy\lang\builder.py", line 659. The line

child = cls(__no_builder=True)

is triggering the error. Since kivy uses factories, it is not clear what is causing the error, but the parameter is missing in that call, and not in your call to super().__init__.

You should also include *args in the signature and call to super: factories may use the function signatures.

Upvotes: 1

necoder
necoder

Reputation: 9

Try doing:

def __init__(self, node): 
        super(Widget, self).__init__()

Upvotes: 0

Related Questions