Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class method takes 1 positional argument but 2 were given

I have read several topics with similar problem, but I don't understand the error is thrown in my case.

I have a class method:

def submit_new_account_form(self, **credentials):
...

When I call it on an instance of my object like this:

create_new_account = loginpage.submit_new_account_form(
            {'first_name': 'Test', 'last_name': 'Test', 'phone_or_email':
              temp_email, 'newpass': '1q2w3e4r5t',
             'sex': 'male'})

I receive this error:

line 22, in test_new_account_succes
    'sex': 'male'})
TypeError: submit_new_account_form() takes 1 positional argument but 2 were       
given
like image 925
Akop Akopov Avatar asked Oct 12 '17 15:10

Akop Akopov


1 Answers

Well that is logical: **credentials means that you will provide it named arguments. But you do not provide a name for the dictionary.

There are two possibilities here:

  1. you use credentials as a single argument, and pass it the dictionary, like:

    def submit_new_account_form(self, credentials):
        # ...
        pass
    
    loginpage.submit_new_account_form({'first_name': 'Test', 'last_name': 'Test', 'phone_or_email': temp_email, 'newpass': '1q2w3e4r5t', 'sex': 'male'})
    
  2. you pass the dictionary as named arguments, by putting two asterisks in front:

    def submit_new_account_form(self, **credentials):
        # ...
        pass
    
    loginpage.submit_new_account_form(**{'first_name': 'Test', 'last_name': 'Test', 'phone_or_email': temp_email, 'newpass': '1q2w3e4r5t', 'sex': 'male'})
    

The second approach is equal to passing named arguments like:

loginpage.submit_new_account_form(first_name='Test', last_name='Test', phone_or_email=temp_email, newpass='1q2w3e4r5t', sex='male')

I think the last way to call this is cleaner syntax. Furthermore it allows you to easily modify the signature of the submit_new_account_form function signature to catch certain parameters immediately, instead of wrapping them into a dictionary.

like image 118
Willem Van Onsem Avatar answered Sep 23 '22 20:09

Willem Van Onsem