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/treevalue/tree/tree/tree.pyx b/treevalue/tree/tree/tree.pyx
index 066c00ed61..c19464c70b 100644
--- a/treevalue/tree/tree/tree.pyx
+++ b/treevalue/tree/tree/tree.pyx
@@ -102,26 +102,20 @@ cdef class TreeValue:
             return obj
 
     @cython.binding(True)
-    cpdef get(self, str key, object default=_GET_NO_DEFAULT):
+    cpdef get(self, str key, object default=None):
         r"""
         Overview:
             Get item from the tree node.
 
-        Arguments:
-            - key (:obj:`str`): Item's name.
-            - default (:obj:`default`): Default value when this item is not found, default is \
-                `_GET_NO_DEFAULT` which means just raise `KeyError` when not found.
+        :param key: Item's name.
+        :param default: Default value when this item is not found, default is ``None``.
+        :return: Item's value.
 
-        Returns:
-            - value: Item's value.
+        .. note::
+            The method :meth:`get` will never raise ``KeyError``, like the behaviour in \
+            `dict.get <https://docs.python.org/3/library/stdtypes.html#dict.get>`_.
         """
-        cdef object value
-        if default is _GET_NO_DEFAULT:
-            value = self._st.get(key)
-        else:
-            value = self._st.get_or_default(key, default)
-
-        return self._unraw(value)
+        return self._unraw(self._st.get_or_default(key, default))
 
     @cython.binding(True)
     cpdef pop(self, str key, object default=_GET_NO_DEFAULT):
@@ -129,13 +123,14 @@ cdef class TreeValue:
         Overview:
             Pop item from the tree node.
 
-        Arguments:
-            - key (:obj:`str`): Item's name.
-            - default (:obj:`default`): Default value when this item is not found, default is \
-                `_GET_NO_DEFAULT` which means just raise `KeyError` when not found.
+        :param key: Item's name.
+        :param default: Default value when this item is not found, default is ``_GET_NO_DEFAULT`` which means \
+            just raise ``KeyError`` when not found.
+        :return: Item's value.
 
-        Returns:
-            - value: Item's value.
+        .. note::
+            The method :meth:`pop` will raise ``KeyError`` when ``key`` is not found, like the behaviour in \
+            `dict.pop <https://docs.python.org/3/library/stdtypes.html#dict.pop>`_.
         """
         cdef object value
         if default is _GET_NO_DEFAULT:


Test patch:
diff --git a/test/tree/tree/test_tree.py b/test/tree/tree/test_tree.py
index 3b736f61d8..4b521ae004 100644
--- a/test/tree/tree/test_tree.py
+++ b/test/tree/tree/test_tree.py
@@ -287,8 +287,7 @@ def test_get(self):
         assert tv1.get('b') == 2
         assert tv1.get('c') == TreeValue({'x': 2, 'y': 3})
         assert tv1.get('d') == {'x': 2, 'y': 3}
-        with pytest.raises(KeyError):
-            _ = tv1.get('e')
+        assert tv1.get('e') is None
         assert tv1.get('e', 233) == 233
 
         tv1 = TreeValue({'a': 1, 'b': 2, 'c': {'x': 2, 'y': 3}, 'd': raw({'x': 2, 'y': 3})})