Skip to content
This repository has been archived by the owner on Jun 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #207 from delira-dev/correct_overwrite_error
Browse files Browse the repository at this point in the history
Correct error when updating config
  • Loading branch information
mibaumgartner committed Sep 12, 2019
2 parents bc054e7 + f66764d commit 024a402
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
17 changes: 2 additions & 15 deletions delira/utils/config.py
Expand Up @@ -240,18 +240,6 @@ def __contains__(self, key):
contain = False
return contain

def __str__(self):
"""
String representation of config
Returns
-------
str
representation of config
"""
encoded_self = Encoder().encode(self)
return encoded_self.__str__()

def update(self, update_dict, deepcopy=False, overwrite=False):
"""
Update internal dicts with dict like object
Expand Down Expand Up @@ -290,15 +278,14 @@ def _update(self, key, item, deepcopy=False, overwrite=False):
overwrite : bool, optional
overwrite existing values inside config, by default False
"""
# check for overwrite
self._raise_overwrite(key, overwrite=overwrite)

if isinstance(item, dict):
# update nested dicts
if key not in self:
self[key] = self._create_internal_dict({})
self[key].update(item, deepcopy=deepcopy, overwrite=overwrite)
else:
# check for overwrite
self._raise_overwrite(key, overwrite=overwrite)
# set item
self._set_internal_item(key, item, deepcopy=deepcopy)

Expand Down
15 changes: 13 additions & 2 deletions tests/utils/test_config.py
Expand Up @@ -15,12 +15,14 @@ class ConfigTest(unittest.TestCase):
def setUp(self):
self.config_cls = Config
self.example_dict = {
"shallowStr": "a", "shallowNum": 1,
"shallowStr": "a",
"shallowNum": 1,
"deep": {"deepStr": "b", "deepNum": 2},
"nestedListOrig": [{"dictList": [1, 2, 3]}],
}
self.update_dict = {
"deep": {"deepStr": "c"}, "shallowNew": 3,
"deep": {"deepStr": "c"},
"shallowNew": 3,
"deepNew": {"newNum": 4},
"nestedList": [{"dictList": [1, 2, 3]}],
"nestedList2": [{"dictList": [1, 2, 3]}],
Expand Down Expand Up @@ -122,6 +124,15 @@ def test_update(self):
self.assertNotEqual(self.update_dict["nestedList2"][0]["dictList"][0],
cf["nestedList2"][0]["dictList"][0])

# check for no error when only updating nested keys
cf = self.config_cls.create_from_dict(self.example_dict)
update_dict = copy.deepcopy(self.update_dict)
update_dict["deep"].pop("deepStr")
update_dict["deep"]["deepStr2"] = "deepStr2"
cf.update(update_dict)
self.assertEqual(cf["deep.deepStr2"],
update_dict["deep"]["deepStr2"])

@unittest.skipUnless(
check_for_no_backend(),
"Test should only be executed if no backend is specified")
Expand Down

0 comments on commit 024a402

Please sign in to comment.