The class method takes 1 positional argument, but 2

I read several topics with a similar problem, but I do not understand that in my case an error occurs.

I have a class method:

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

When I call it on an instance of my object as follows:

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

I get this error:

line 22, in test_new_account_succes
    'sex': 'male'})
TypeError: submit_new_account_form() takes 1 positional argument but 2 were       
given
+4
source share
1 answer

Well, that’s logical: it **credentialsmeans that you provide it with arguments. But you did not specify the name of the dictionary.

There are two possibilities here:

  • you use it credentialsas one argument and pass it a dictionary, for example:

    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'})
    
  • you pass the dictionary as named arguments by setting two stars in front of you:

    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 of the type:

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

, . , submit_new_account_form, , , .

+5

Source: https://habr.com/ru/post/1687383/


All Articles