Skip to content

Commit d79336d

Browse files
committed
Enforce top level keys check in merge method. #367
1 parent cba5aef commit d79336d

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

benedict/dicts/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from benedict.dicts.keyattr import KeyattrDict
2727
from benedict.dicts.keylist import KeylistDict
2828
from benedict.dicts.keypath import KeypathDict
29+
from benedict.dicts.keypath import keypath_util
2930
from benedict.dicts.parse import ParseDict
3031
from benedict.serializers import JSONSerializer, YAMLSerializer
3132

@@ -219,7 +220,10 @@ def merge(self, other, *args, **kwargs):
219220
If overwrite is False, existing values will not be overwritten.
220221
If concat is True, list values will be concatenated together.
221222
"""
222-
_merge(self, other, *args, **kwargs)
223+
others = [other] + list(args)
224+
for other in others:
225+
keypath_util.check_keys(other, self._keypath_separator)
226+
_merge(self, *others, **kwargs)
223227

224228
def move(self, key_src, key_dest):
225229
"""

tests/github/test_issue_0367.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
3+
from benedict import benedict
4+
5+
6+
class github_issue_0367_test_case(unittest.TestCase):
7+
"""
8+
This class describes a github issue 0367 test case.
9+
https://github.com/fabiocaccamo/python-benedict/issues/367
10+
11+
To run this specific test:
12+
- Run python -m unittest tests.github.test_issue_0367
13+
"""
14+
15+
def test_dict_keys_with_separators_with_merge(self):
16+
d = {"foo.bar": 1}
17+
b = benedict()
18+
with self.assertRaises(ValueError):
19+
b.merge(d)
20+
# self.assertEqual(b, {"foo": {"bar": 1}})
21+
22+
def test_dict_keys_with_separators_with_nested_merge(self):
23+
d = {"baz": {"foo.bar": 1}}
24+
b = benedict()
25+
with self.assertRaises(ValueError):
26+
b.merge(d)
27+
# self.assertEqual(b, {"baz": {"foo.bar": 1}})
28+
29+
def test_dict_keys_with_separators_with_constructor(self):
30+
d = {"foo.bar": 1}
31+
with self.assertRaises(ValueError):
32+
benedict(d)
33+
# self.assertEqual(b, {"foo": {"bar": 1}})

0 commit comments

Comments
 (0)