From a7658c75735a5593a99e4b59895d09ecee30cb8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastien=20G=C3=A9rard?= Date: Tue, 21 Aug 2018 18:01:12 +0200 Subject: [PATCH] fix BaseList.__iter__ operator (#1305) + minor improvements --- AUTHORS | 1 + mongoengine/base/datastructures.py | 8 ++++---- tests/test_datastructures.py | 17 ++++++++++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/AUTHORS b/AUTHORS index 2e7b56fc..b38825dc 100644 --- a/AUTHORS +++ b/AUTHORS @@ -246,3 +246,4 @@ that much better: * Renjianxin (https://github.com/Davidrjx) * Erdenezul Batmunkh (https://github.com/erdenezul) * Andy Yankovsky (https://github.com/werat) + * Bastien Gérard (https://github.com/bagerard) diff --git a/mongoengine/base/datastructures.py b/mongoengine/base/datastructures.py index fddd945a..8948243e 100644 --- a/mongoengine/base/datastructures.py +++ b/mongoengine/base/datastructures.py @@ -128,8 +128,8 @@ class BaseList(list): return value def __iter__(self): - for i in six.moves.range(self.__len__()): - yield self[i] + for v in super(BaseList, self).__iter__(): + yield v def __setitem__(self, key, value, *args, **kwargs): if isinstance(key, slice): @@ -138,7 +138,7 @@ class BaseList(list): self._mark_as_changed(key) return super(BaseList, self).__setitem__(key, value) - def __delitem__(self, key, *args, **kwargs): + def __delitem__(self, key): self._mark_as_changed() return super(BaseList, self).__delitem__(key) @@ -187,7 +187,7 @@ class BaseList(list): self._mark_as_changed() return super(BaseList, self).remove(*args, **kwargs) - def reverse(self, *args, **kwargs): + def reverse(self): self._mark_as_changed() return super(BaseList, self).reverse() diff --git a/tests/test_datastructures.py b/tests/test_datastructures.py index 79381c5a..1ea562a5 100644 --- a/tests/test_datastructures.py +++ b/tests/test_datastructures.py @@ -1,6 +1,21 @@ 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):