Skip to content
Open
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
6 changes: 5 additions & 1 deletion benedict/dicts/keylist/keylist_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ def _get_item_key_and_value(item: Any, key: Any) -> tuple[Any, Any]:
def _get_or_new_item_value(item: Any, key: Any, subkey: Any) -> Any:
try:
_, value = _get_item_key_and_value(item, key)
if not type_util.is_dict_or_list_or_tuple(value):
# a list can only hold an integer-index subkey; anything else needs a dict
keeps_subkey = type_util.is_dict(value) or (
type_util.is_list_or_tuple(value) and _get_index(subkey) is not None
)
if not keeps_subkey:
raise TypeError
except (IndexError, KeyError, TypeError):
value = _new_item_value(subkey)
Expand Down
17 changes: 17 additions & 0 deletions tests/dicts/keypath/test_keypath_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,23 @@ def test_setitem_override_existing_item(self) -> None:
r = {"a": {"b": {"c": {"d": 3}}}}
self.assertEqual(b, r)

def test_setitem_string_subkey_overrides_list_value(self) -> None:
# a string subkey cannot index a list, so it overrides the list like a scalar
b = KeypathDict({"a": [1, 2]})
b["a.x"] = 1
self.assertEqual(b, {"a": {"x": 1}})
b = KeypathDict({"a": [1, 2]})
b["a.x.y"] = 1
self.assertEqual(b, {"a": {"x": {"y": 1}}})
# a list-of-scalar element is likewise overridden by a string subkey
b = KeypathDict({"a": [5]})
b["a[0].x"] = 3
self.assertEqual(b, {"a": [{"x": 3}]})
# a valid integer index keeps the list
b = KeypathDict({"a": [1, 2]})
b["a[0]"] = 9
self.assertEqual(b, {"a": [9, 2]})

def test_setitem_with_keys_list(self) -> None:
d: dict[str, Any] = {
"a": {
Expand Down