Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions box/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
35 changes: 35 additions & 0 deletions test/test_box_keys_8.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}}