mirror of https://github.com/scrapy/scrapy.git
made ListField init with an instance (not a class) of Field
This commit is contained in:
parent
d8b85ae7ad
commit
cf638e682c
|
|
@ -19,13 +19,13 @@ class BaseField(object):
|
|||
|
||||
|
||||
class ListField(BaseField):
|
||||
def __init__(self, field_type, default=None):
|
||||
self._field = field_type()
|
||||
def __init__(self, field, default=None):
|
||||
self.field = field
|
||||
super(ListField, self).__init__(default)
|
||||
|
||||
def to_python(self, value):
|
||||
if hasattr(value, '__iter__'):
|
||||
return [self._field.to_python(v) for v in value]
|
||||
return [self.field.to_python(v) for v in value]
|
||||
else:
|
||||
raise TypeError("Cannot instatiante %s with %s" \
|
||||
% (self.__class__.__name__, type(value).__name__))
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ class InheritDefaultAdaptor(DefaultedAdaptor):
|
|||
|
||||
|
||||
class ListFieldTestItem(Item):
|
||||
names = fields.ListField(fields.TextField)
|
||||
names = fields.ListField(fields.TextField())
|
||||
|
||||
|
||||
class ListFieldItemAdaptor(ItemAdaptor):
|
||||
|
|
|
|||
|
|
@ -37,10 +37,10 @@ class NewItemTest(unittest.TestCase):
|
|||
|
||||
self.assertRaises(TypeError, TestItem, name=set())
|
||||
|
||||
def test_multi(self):
|
||||
def test_list(self):
|
||||
class TestListItem(Item):
|
||||
name = fields.TextField()
|
||||
names = fields.ListField(fields.TextField)
|
||||
names = fields.ListField(fields.TextField())
|
||||
|
||||
i = TestListItem()
|
||||
i['name'] = u'name'
|
||||
|
|
@ -337,7 +337,7 @@ class NewItemFieldsTest(unittest.TestCase):
|
|||
self.assertEqual(field.from_unicode_list([]), u'')
|
||||
self.assertEqual(field.from_unicode_list([u'hello', u'world']), u'hello world')
|
||||
|
||||
field = fields.ListField(fields.TextField)
|
||||
field = fields.ListField(fields.TextField())
|
||||
self.assertEqual(field.from_unicode_list([]), [])
|
||||
self.assertEqual(field.from_unicode_list([u'hello', u'world']), [u'hello', u'world'])
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue