From 1758d23b46167128e374712f3982f4138618e7c4 Mon Sep 17 00:00:00 2001 From: Juan Gutierrez Date: Sun, 29 Sep 2024 21:06:14 -0400 Subject: [PATCH] Call mark_as_changed_wrapper for `list.clear` on ListField `list.clear` was added in Python 3.3 --- mongoengine/base/datastructures.py | 1 + tests/test_datastructures.py | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/mongoengine/base/datastructures.py b/mongoengine/base/datastructures.py index a175cd119..dcb8438c7 100644 --- a/mongoengine/base/datastructures.py +++ b/mongoengine/base/datastructures.py @@ -177,6 +177,7 @@ def __setitem__(self, key, value): remove = mark_as_changed_wrapper(list.remove) reverse = mark_as_changed_wrapper(list.reverse) sort = mark_as_changed_wrapper(list.sort) + clear = mark_as_changed_wrapper(list.clear) __delitem__ = mark_as_changed_wrapper(list.__delitem__) __iadd__ = mark_as_changed_wrapper(list.__iadd__) __imul__ = mark_as_changed_wrapper(list.__imul__) diff --git a/tests/test_datastructures.py b/tests/test_datastructures.py index 42ce42a1d..eb1417d11 100644 --- a/tests/test_datastructures.py +++ b/tests/test_datastructures.py @@ -185,7 +185,7 @@ def test___iter__(self): base_list = BaseList(values, instance=None, name="my_name") assert values == list(base_list) - def test___iter___allow_modification_while_iterating_withou_error(self): + def test___iter___allow_modification_while_iterating_without_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): @@ -365,6 +365,11 @@ def test_sort_calls_with_key(self): base_list.sort(key=lambda i: str(i)) assert base_list == [1, 11, 2] + def test_clear_calls_mark_as_changed(self): + base_list = self._get_baselist([True, False]) + base_list.clear() + assert base_list._instance._changed_fields == ["my_name"] + class TestStrictDict(unittest.TestCase): def setUp(self):