Skip to content

Commit 9800f21

Browse files
committed
Prevent clearing dict instance when assigning value to itself. #294
1 parent 18629ba commit 9800f21

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

benedict/dicts/base/base_dict.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ def __setitem__(self, key, value):
7979
is_dict_item = key in self._dict and isinstance(self._dict[key], dict)
8080
is_dict_value = isinstance(value, dict)
8181
if is_dict_item and is_dict_value:
82+
if self._dict[key] is value:
83+
# prevent clearing dict instance when assigning value to itself. fix #294
84+
return
8285
self._dict[key].clear()
8386
self._dict[key].update(value)
8487
return

tests/github/test_issue_0294.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
3+
from benedict import benedict
4+
5+
6+
class github_issue_0294_test_case(unittest.TestCase):
7+
"""
8+
This class describes a github issue 0294 test case.
9+
https://github.com/fabiocaccamo/python-benedict/issues/294
10+
11+
To run this specific test:
12+
- Run python -m unittest tests.github.test_issue_0294
13+
"""
14+
15+
def test_assigning_benedict_element_to_itself_clears_the_element(self):
16+
d = benedict({"a": {"b": 1}})
17+
d["a"] = d["a"]
18+
self.assertEqual(d, {"a": {"b": 1}})
19+
d.a = d.a
20+
self.assertEqual(d, {"a": {"b": 1}})

0 commit comments

Comments
 (0)