Enforce top level keys check in `merge` method. #367

This commit is contained in:
Fabio Caccamo 2024-02-24 16:25:37 +01:00
parent cba5aefc0c
commit d79336dd73
2 changed files with 38 additions and 1 deletions

View File

@ -26,6 +26,7 @@ from benedict.dicts.io import IODict
from benedict.dicts.keyattr import KeyattrDict
from benedict.dicts.keylist import KeylistDict
from benedict.dicts.keypath import KeypathDict
from benedict.dicts.keypath import keypath_util
from benedict.dicts.parse import ParseDict
from benedict.serializers import JSONSerializer, YAMLSerializer
@ -219,7 +220,10 @@ class benedict(KeyattrDict, KeypathDict, IODict, ParseDict):
If overwrite is False, existing values will not be overwritten.
If concat is True, list values will be concatenated together.
"""
_merge(self, other, *args, **kwargs)
others = [other] + list(args)
for other in others:
keypath_util.check_keys(other, self._keypath_separator)
_merge(self, *others, **kwargs)
def move(self, key_src, key_dest):
"""

View File

@ -0,0 +1,33 @@
import unittest
from benedict import benedict
class github_issue_0367_test_case(unittest.TestCase):
"""
This class describes a github issue 0367 test case.
https://github.com/fabiocaccamo/python-benedict/issues/367
To run this specific test:
- Run python -m unittest tests.github.test_issue_0367
"""
def test_dict_keys_with_separators_with_merge(self):
d = {"foo.bar": 1}
b = benedict()
with self.assertRaises(ValueError):
b.merge(d)
# self.assertEqual(b, {"foo": {"bar": 1}})
def test_dict_keys_with_separators_with_nested_merge(self):
d = {"baz": {"foo.bar": 1}}
b = benedict()
with self.assertRaises(ValueError):
b.merge(d)
# self.assertEqual(b, {"baz": {"foo.bar": 1}})
def test_dict_keys_with_separators_with_constructor(self):
d = {"foo.bar": 1}
with self.assertRaises(ValueError):
benedict(d)
# self.assertEqual(b, {"foo": {"bar": 1}})