mirror of https://github.com/scrapy/scrapy.git
newitem: reverting to use 'default' Field key instead of 'default_factory'
This commit is contained in:
parent
78b69ec97e
commit
3658acd9da
|
|
@ -30,7 +30,7 @@ class Item(DictMixin, BaseItem):
|
|||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self._values = {}
|
||||
if args or kwargs: # avoid instantiating dict for most common case
|
||||
if args or kwargs: # avoid creating dict for most common case
|
||||
for k, v in dict(*args, **kwargs).iteritems():
|
||||
self[k] = v
|
||||
|
||||
|
|
@ -39,11 +39,9 @@ class Item(DictMixin, BaseItem):
|
|||
return self._values[key]
|
||||
except KeyError:
|
||||
field = self.fields[key]
|
||||
default_factory = field.get('default_factory')
|
||||
if default_factory:
|
||||
return default_factory()
|
||||
else:
|
||||
raise KeyError(key)
|
||||
if 'default' in field:
|
||||
return field['default']
|
||||
raise
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
if key in self.fields:
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ class NewItemTest(unittest.TestCase):
|
|||
|
||||
def test_default_value(self):
|
||||
class TestItem(Item):
|
||||
name = Field(default_factory=lambda: u'John')
|
||||
name = Field(default=u'John')
|
||||
|
||||
i = TestItem()
|
||||
self.assertEqual(i['name'], u'John')
|
||||
|
|
|
|||
Loading…
Reference in New Issue