Repository: opendilab/treevalue (language: python)

Issue:
Inherit MutableMapping in TreeValue Class
The `TreeValue` class need to inherit `MutableMapping` class for supporting all the features that `dict` object have.

See: https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes


Discussion / hints:

Fix patch:
diff --git a/docs/source/api_doc/tree/tree.rst b/docs/source/api_doc/tree/tree.rst
index 2b387292fa..deeb2084ff 100644
--- a/docs/source/api_doc/tree/tree.rst
+++ b/docs/source/api_doc/tree/tree.rst
@@ -9,7 +9,7 @@ TreeValue
 ---------------
 
 .. autoclass:: TreeValue
-    :members: __init__, __getattribute__, __setattr__, __delattr__, __contains__, __repr__, __iter__, __hash__, __eq__, _attr_extern, __len__, __bool__, __str__, __getstate__, __setstate__, get, pop, keys, values, items, __getitem__, __setitem__, __delitem__, _getitem_extern, _setitem_extern, _delitem_extern, popitem
+    :members: __init__, __getattribute__, __setattr__, __delattr__, __contains__, __repr__, __iter__, __hash__, __eq__, _attr_extern, __len__, __bool__, __str__, __getstate__, __setstate__, get, pop, keys, values, items, __getitem__, __setitem__, __delitem__, _getitem_extern, _setitem_extern, _delitem_extern, popitem, clear
 
 
 .. _apidoc_tree_tree_delayed:
diff --git a/treevalue/tree/common/storage.pxd b/treevalue/tree/common/storage.pxd
index 7631cc2753..b1a2b5834e 100644
--- a/treevalue/tree/common/storage.pxd
+++ b/treevalue/tree/common/storage.pxd
@@ -16,6 +16,7 @@ cdef class TreeStorage:
     cpdef public object pop_or_default(self, str key, object default)
     cpdef public tuple popitem(self)
     cpdef public void del_(self, str key) except *
+    cpdef public void clear(self)
     cpdef public boolean contains(self, str key)
     cpdef public uint size(self)
     cpdef public boolean empty(self)
diff --git a/treevalue/tree/common/storage.pyx b/treevalue/tree/common/storage.pyx
index c353ed2cba..a0c512de25 100644
--- a/treevalue/tree/common/storage.pyx
+++ b/treevalue/tree/common/storage.pyx
@@ -67,6 +67,9 @@ cdef class TreeStorage:
         except KeyError:
             raise KeyError(f"Key {repr(key)} not found in this tree.")
 
+    cpdef public void clear(self):
+        self.map.clear()
+
     cpdef public boolean contains(self, str key):
         return key in self.map
 
diff --git a/treevalue/tree/tree/tree.pxd b/treevalue/tree/tree/tree.pxd
index 92c20f73da..e00a3113a9 100644
--- a/treevalue/tree/tree/tree.pxd
+++ b/treevalue/tree/tree/tree.pxd
@@ -23,6 +23,7 @@ cdef class TreeValue:
     cpdef get(self, str key, object default= *)
     cpdef pop(self, str key, object default= *)
     cpdef popitem(self)
+    cpdef void clear(self)
 
     cpdef treevalue_keys keys(self)
     cpdef treevalue_values values(self)
diff --git a/treevalue/tree/tree/tree.pyx b/treevalue/tree/tree/tree.pyx
index 9ab963b158..610a38fa94 100644
--- a/treevalue/tree/tree/tree.pyx
+++ b/treevalue/tree/tree/tree.pyx
@@ -161,6 +161,14 @@ cdef class TreeValue:
         except KeyError:
             raise KeyError(f'popitem(): {self._type.__name__} is empty.')
 
+    @cython.binding(True)
+    cpdef void clear(self):
+        """
+        Overview:
+            Clear all the items in this treevalue object.
+        """
+        self._st.clear()
+
     @cython.binding(True)
     cpdef _attr_extern(self, str key):
         r"""


Test patch:
diff --git a/test/tree/common/test_storage.py b/test/tree/common/test_storage.py
index be39ee6291..621f37947d 100644
--- a/test/tree/common/test_storage.py
+++ b/test/tree/common/test_storage.py
@@ -273,6 +273,19 @@ def test_del_(self):
         with pytest.raises(KeyError):
             t.del_('fff')
 
+    def test_clear(self):
+        t = create_storage({'a': 1, 'b': 2, 'c': raw({'x': 3, 'y': 4}), 'd': {'x': 3, 'y': 4}})
+        t.clear()
+        assert t == create_storage({})
+        assert t.size() == 0
+
+        d1 = delayed_partial(lambda: 1)
+        d2 = delayed_partial(lambda x: x + 1, d1)
+        t1 = create_storage({'a': d1, 'b': d2, 'c': d1, 'd': 100})
+        t1.clear()
+        assert t1 == create_storage({})
+        assert t1.size() == 0
+
     def test_contains(self):
         t = create_storage({'a': 1, 'b': 2, 'c': raw({'x': 3, 'y': 4}), 'd': {'x': 3, 'y': 4}})
         assert t.contains('a')
diff --git a/test/tree/tree/test_tree.py b/test/tree/tree/test_tree.py
index 8930acc34a..9ad3f7976b 100644
--- a/test/tree/tree/test_tree.py
+++ b/test/tree/tree/test_tree.py
@@ -355,6 +355,11 @@ def test_popitem(self):
         with pytest.raises(KeyError):
             tv2.popitem()
 
+    def test_clear(self):
+        tv1 = TreeValue({'a': 1, 'b': 2, 'c': {'x': 2, 'y': 3}, 'd': raw({'x': 2, 'y': 3})})
+        assert tv1.clear() is None
+        assert not tv1
+
     def test_keys(self):
         tv1 = TreeValue({'a': 1, 'b': 2, 'c': {'x': 2, 'y': 3}, 'd': raw({'x': 2, 'y': 3})})
         assert len(tv1.keys()) == 4