Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pilo/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ class MyForm(pilo.Form)

"""

def __init__(self, src=NONE, **options):
def __init__(self, src=NONE, name=None, **options):
super(Field, self).__init__()

# hooks
Expand All @@ -316,7 +316,7 @@ def __init__(self, src=NONE, **options):

# site
self.parent = None
self.name = None
self.name = name
self.src = src

# options
Expand Down Expand Up @@ -372,7 +372,9 @@ def __str__(self):
return '{0}({1})'.format(type(self).__name__, attrs)

def attach(self, parent, name=None):
self.parent, self.name = parent, name
self.parent = parent
if self.name is None:
self.name = name
if self.src is NONE:
self.src = self.name
if inspect.isclass(parent) and issubclass(parent, Form):
Expand Down
28 changes: 28 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,31 @@ def send_to_zoo(self):
]:
obj = Animal.type.cast(desc)(desc)
self.assertIsInstance(obj, cls)

def test_explicit_field_name_existing_method(self):
"""
Test that you can use existing dictionary methods for field names.
"""

class Form(pilo.Form):

items_key = pilo.fields.List(pilo.fields.String(), name='items')

items = ['a', 'b', 'c']
form = Form(items=items)
self.assertEqual(form['items'], items)
self.assertEqual(dict(form.items()), dict(items=items))

def test_explicit_field_name_reserved_word(self):
"""
Test that you can use existing dictionary methods for field names.
"""

class Form(pilo.Form):

defintion = pilo.fields.Tuple((pilo.fields.String(), pilo.fields.String(), ), name='def')

definition = ('term', 'definition')
form = Form(**{'def': definition})
self.assertEqual(form['def'], definition)
self.assertEqual(dict(form), {'def': definition})