Chapter 11: TypeError: __init__() got an unexpected keyword argument 'for_list'

5,791 views
Skip to first unread message

Jonathan Sundqvist

unread,
Jan 17, 2014, 6:42:40 AM1/17/14
to obey-the-test...@googlegroups.com
Hi everyone

I get this error when running the tests in Chapter 11: TypeError: __init__() got an unexpected keyword argument 'for_list' in ExistingListItemForm
There is also an error: TypeError: __init__() got an unexpected keyword argument 'data'

My form class looks like this and uses super, so as I understand it I shouldn't get the error:

class ExistingListItemForm(forms.models.ModelForm):

    def __init__(self, for_list, *args, **kwargs):
        super(ExistingListItemForm, self).__init__(*args, **kwargs)

Any pointers to where I go wrong would be very helpful.

Thank you!
Jonathan

Steve Young

unread,
Jan 17, 2014, 10:52:07 AM1/17/14
to obey-the-test...@googlegroups.com
Where are you in the chapter?  I think the super line should be:

        super().__init__(*args, **kwargs)

according to the book. And eventually the class:

class ExistingListItemForm(ItemForm):

once you have class ItemForm set up.

Jonathan Sundqvist

unread,
Jan 17, 2014, 11:18:51 AM1/17/14
to Steve Young, obey-the-testing-goat-book
Steve, I'm right below the headline: "A more complex form to handle uniqueness validation"

Yup, that's how the super should look like in python3, I've been doing python 2.7 so far. In python 2.7 you have to use those arguments. Using those arguments in python3 should work just fine as well, according to my understanding.

I also tried using ItemForm in the class directly like that, made no difference, and the error specifically refers to the __init__ function. So I'm still stuck.


Jonathan Sundqvist


--
You received this message because you are subscribed to a topic in the Google Groups "Obey the testing goat! Test-Driven Web Development with Python book" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/obey-the-testing-goat-book/XzRd5CrkoBk/unsubscribe.
To unsubscribe from this group and all of its topics, send an email to obey-the-testing-go...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Harry Percival

unread,
Jan 17, 2014, 12:37:43 PM1/17/14
to Jonathan Sundqvist, Steve Young, obey-the-testing-goat-book
I hate the old super(), and tend to use the unbound methods on the parent class, cf my later note.  but, anyway, that's probable irrelephant.

re: the error you're seeing, what test is it coming from?  Can you post the full stack trace?


--
You received this message because you are subscribed to the Google Groups "Obey the testing goat! Test-Driven Web Development with Python book" group.
To unsubscribe from this group and stop receiving emails from it, send an email to obey-the-testing-go...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
------------------------------
Harry J.W. Percival
------------------------------
Twitter: @hjwp
Mobile:  +44 (0) 78877 02511
Skype:         harry.percival

Jonathan Sundqvist

unread,
Jan 17, 2014, 1:26:47 PM1/17/14
to hjwp2, Steve Young, obey-the-testing-goat-book
This is the full stack trace for all the 4 errors that I get when run the test. And sorry for the old super. :)

======================================================================
ERROR: test_form_renders_text_input (lists.tests.test_forms.ExistingListItemForm)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/jonathan/Documents/Dropbox/Education/tutorials/obeygoat/lists/tests/test_forms.py", line 14, in test_form_renders_text_input
    form = ExistingListItemForm(for_list=list_)

TypeError: __init__() got an unexpected keyword argument 'for_list'

======================================================================
ERROR: test_form_save_handles_saving_to_a_list (lists.tests.test_forms.ExistingListItemForm)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/jonathan/Documents/Dropbox/Education/tutorials/obeygoat/lists/tests/test_forms.py", line 35, in test_form_save_handles_saving_to_a_list
    form = ExistingListItemForm(data={'text': 'do me'})

TypeError: __init__() got an unexpected keyword argument 'data'

======================================================================
ERROR: test_form_validation_for_blank_items (lists.tests.test_forms.ExistingListItemForm)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/jonathan/Documents/Dropbox/Education/tutorials/obeygoat/lists/tests/test_forms.py", line 19, in test_form_validation_for_blank_items
    form = ExistingListItemForm(for_list=list_, data={'text': ''})

TypeError: __init__() got an unexpected keyword argument 'for_list'

======================================================================
ERROR: test_form_validation_for_duplicate_items (lists.tests.test_forms.ExistingListItemForm)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/jonathan/Documents/Dropbox/Education/tutorials/obeygoat/lists/tests/test_forms.py", line 29, in test_form_validation_for_duplicate_items
    form = ExistingListItemForm(for_list=list_, data={'text': 'no twins!'})

TypeError: __init__() got an unexpected keyword argument 'for_list'

----------------------------------------------------------------------


Jonathan Sundqvist


Harry Percival

unread,
Jan 17, 2014, 1:33:21 PM1/17/14
to Jonathan Sundqvist, Steve Young, obey-the-testing-goat-book
So it looks like the dunderinit of ExistingListItemForm isn't accepting the for_list keyword.   Assuming you've tried to edit it as required, maybe something went wrong? Could your edits to that file somehow have got lost?  Are you sure your editor saved the file?  Could you have been editing a file in the wrong folder?  Could there be a .pyc hanging around somewhere causing weird effects?  Could there be two copies of ExistingListItemForm?

Jonathan Sundqvist

unread,
Jan 17, 2014, 1:43:42 PM1/17/14
to hjwp2, Steve Young, obey-the-testing-goat-book
Thank you Harry, it was as I suspected, I just needed to be asked those questions. When I checked everything for the nth time I found out that I had redefined ExistingListItemForm in test_forms.py.  So when I renamed it to ExistingListItemFormTest it worked great.

"Could there be two copies of ExistingListItemForm?"
So yes, there were in fact two copies.

Problem solved, once again thank you!


Jonathan Sundqvist


Reply all
Reply to author
Forward
0 new messages