diff --git a/box/box.py b/box/box.py index e1ce2047..020ba875 100644 --- a/box/box.py +++ b/box/box.py @@ -1058,12 +1058,21 @@ def update(self, *args, **kwargs): if single_arg: if hasattr(single_arg, "keys"): for k in single_arg: - self.__convert_and_store(k, single_arg[k]) + self._update_store(k, single_arg[k]) else: for k, v in single_arg: - self.__convert_and_store(k, v) + self._update_store(k, v) for k in kwargs: - self.__convert_and_store(k, kwargs[k]) + self._update_store(k, kwargs[k]) + + def _update_store(self, key, value): + # In box_dots mode a dotted key is a path, the same as in + # __setitem__ and merge_update; storing it literally would create + # a parallel key that dots lookups cannot reach consistently. + if self.__process_dotted_key(key): + self.__setitem__(key, value) + else: + self.__convert_and_store(key, value) def merge_update(self, *args, **kwargs): """ diff --git a/test/test_box_keys_8.py b/test/test_box_keys_8.py index 690dc424..0b39778f 100644 --- a/test/test_box_keys_8.py +++ b/test/test_box_keys_8.py @@ -117,3 +117,38 @@ def test_config_box(self): cb = ConfigBox({"maxConnections": "5"}, camel_killer_box=True, camel_killer_box_keep_keys=True) assert cb.int("max_connections") == 5 assert cb.to_dict() == {"maxConnections": "5"} + + +class TestUpdateDottedPaths: + """ update() honors box_dots path semantics, like __setitem__ and merge_update. """ + + def test_update_dotted_key_nests(self): + b = Box(default_box=True, box_dots=True) + b.update({"x.y": 5}) + assert b["x"]["y"] == 5 + + def test_update_dotted_key_extends_existing_path(self): + b = Box({"x": {"y": 1}}, box_dots=True) + b.update({"x.z": 5}) + assert b.to_dict() == {"x": {"y": 1, "z": 5}} + + def test_update_dotted_key_with_dots_exclude_stays_literal(self): + b = Box(box_dots=True, box_dots_exclude=r"^[a-z]+\.[a-z]+$") + b.update({"keep.this": 1}) + assert b.to_dict() == {"keep.this": 1} + + def test_update_kwargs_dotted_key_nests(self): + b = Box(default_box=True, box_dots=True) + b.update(**{"k.k": 9}) + assert b["k"]["k"] == 9 + + def test_update_non_dotted_unchanged(self): + b = Box({"a": 1}) + b.update({"b": 2}, c=3) + assert b.to_dict() == {"a": 1, "b": 2, "c": 3} + + def test_box_addition_merges_dotted_paths(self): + b = Box({"x": {"y": 1}}, default_box=True, box_dots=True) + Box( + {"x.z": 2}, default_box=True, box_dots=True + ) + assert b.to_dict() == {"x": {"y": 1, "z": 2}}