fix BaseList.__iter__ operator (#1305) + minor improvements
This commit is contained in:
parent
36c5f02bfb
commit
a7658c7573
1
AUTHORS
1
AUTHORS
@ -246,3 +246,4 @@ that much better:
|
|||||||
* Renjianxin (https://github.com/Davidrjx)
|
* Renjianxin (https://github.com/Davidrjx)
|
||||||
* Erdenezul Batmunkh (https://github.com/erdenezul)
|
* Erdenezul Batmunkh (https://github.com/erdenezul)
|
||||||
* Andy Yankovsky (https://github.com/werat)
|
* Andy Yankovsky (https://github.com/werat)
|
||||||
|
* Bastien Gérard (https://github.com/bagerard)
|
||||||
|
@ -128,8 +128,8 @@ class BaseList(list):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for i in six.moves.range(self.__len__()):
|
for v in super(BaseList, self).__iter__():
|
||||||
yield self[i]
|
yield v
|
||||||
|
|
||||||
def __setitem__(self, key, value, *args, **kwargs):
|
def __setitem__(self, key, value, *args, **kwargs):
|
||||||
if isinstance(key, slice):
|
if isinstance(key, slice):
|
||||||
@ -138,7 +138,7 @@ class BaseList(list):
|
|||||||
self._mark_as_changed(key)
|
self._mark_as_changed(key)
|
||||||
return super(BaseList, self).__setitem__(key, value)
|
return super(BaseList, self).__setitem__(key, value)
|
||||||
|
|
||||||
def __delitem__(self, key, *args, **kwargs):
|
def __delitem__(self, key):
|
||||||
self._mark_as_changed()
|
self._mark_as_changed()
|
||||||
return super(BaseList, self).__delitem__(key)
|
return super(BaseList, self).__delitem__(key)
|
||||||
|
|
||||||
@ -187,7 +187,7 @@ class BaseList(list):
|
|||||||
self._mark_as_changed()
|
self._mark_as_changed()
|
||||||
return super(BaseList, self).remove(*args, **kwargs)
|
return super(BaseList, self).remove(*args, **kwargs)
|
||||||
|
|
||||||
def reverse(self, *args, **kwargs):
|
def reverse(self):
|
||||||
self._mark_as_changed()
|
self._mark_as_changed()
|
||||||
return super(BaseList, self).reverse()
|
return super(BaseList, self).reverse()
|
||||||
|
|
||||||
|
@ -1,6 +1,21 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from mongoengine.base.datastructures import StrictDict
|
from mongoengine.base.datastructures import StrictDict, BaseList
|
||||||
|
|
||||||
|
|
||||||
|
class TestBaseList(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_iter_simple(self):
|
||||||
|
values = [True, False, True, False]
|
||||||
|
base_list = BaseList(values, instance=None, name='my_name')
|
||||||
|
self.assertEqual(values, list(base_list))
|
||||||
|
|
||||||
|
def test_iter_allow_modification_while_iterating_withou_error(self):
|
||||||
|
# regular list allows for this, thus this subclass must comply to that
|
||||||
|
base_list = BaseList([True, False, True, False], instance=None, name='my_name')
|
||||||
|
for idx, val in enumerate(base_list):
|
||||||
|
if val:
|
||||||
|
base_list.pop(idx)
|
||||||
|
|
||||||
|
|
||||||
class TestStrictDict(unittest.TestCase):
|
class TestStrictDict(unittest.TestCase):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user