0

I was writing a wrapper method when the error occurred. I've managed to simplify the problem to the following piece of code:

def func1(data, **kwargs):
    func2(data, kwargs)

def func2(data, **kwargs):
    print(data)

The TypeError: func2() takes 1 positional argument but 2 were given appears when running func1.

Regardless of what named arguments I put into the function call, the error occurs. However, if I don't pass kwargs to func2, everything runs smoothly.

Most of the information I found about the error message dealt with class methods not calling self, which is not the case.

Doktoro Reichard
  • 577
  • 1
  • 7
  • 22
  • While writing and proof-reading the question, I found [this](http://stackoverflow.com/q/334655/2651145) question, which deals with similar issues. – Doktoro Reichard Jul 26 '15 at 00:43

1 Answers1

2

As it turns out, when calling func2(data, kwargs) from within func1 we are suppling func2 with two arguments, data and kwargs, a dictionary.

Even if the dictionary is empty, it is still a single item, which is recognized as another argument (and not as the sequence of named arguments func1 was called), hence the error message.

Since func2 expects to be called in the following manner:

func2(data, arg1='alpha', arg2='beta', arg3=... )

Unpacking the dictionary is needed. func1 should therefore be:

def func1(data, **kwargs):
    func2(data, **kwargs)

Note the ** on the func2 call, this makes it so that the dictionary is unpacked and resemble what func2 expects to be called.

Doktoro Reichard
  • 577
  • 1
  • 7
  • 22