Skip to content
Closed
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
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Code contributions:
- Jesper Schlegel (jesperschlegel)
- J vanBemmel (jbemmel)
- m-janicki
- Eric3-jp (with OpenAI Codex assistance)


Suggestions and bug reporting:
Expand Down
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

Unreleased
----------

* Convert dictionaries and lists assigned to BoxList items and slices, matching append and insert.

Version 7.4.1
-------------

Expand Down
6 changes: 5 additions & 1 deletion box/box_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,18 @@ def __setitem__(self, key, value):
if pos >= len(self) and self.box_options.get("default_box"):
self.extend([None] * (pos - len(self) + 1))
if len(list_pos.group()) == len(key):
return super().__setitem__(pos, value)
return super().__setitem__(pos, self._convert(value))
children = key[len(list_pos.group()) :].lstrip(".")
if self.box_options.get("default_box"):
if children[0] == "[":
super().__setitem__(pos, box.BoxList(**self.box_options))
else:
super().__setitem__(pos, self.box_options.get("box_class")(**self.box_options))
return super().__getitem__(pos).__setitem__(children, value)
if isinstance(key, slice):
value = [self._convert(item) for item in value]
else:
value = self._convert(value)
super().__setitem__(key, value)

def _is_intact_type(self, obj):
Expand Down
53 changes: 53 additions & 0 deletions test/test_box_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,59 @@ def test_box_list(self):
assert isinstance(new_list[1], BoxList)
assert not isinstance(new_list.to_list(), BoxList)

@pytest.mark.parametrize("key", [0, -1, "[0]", "[2]"])
@pytest.mark.parametrize("nested_list", [False, True])
def test_setitem_converts_containers(self, key, nested_list):
bl = BoxList([None], box_dots=True, default_box=True, camel_killer_box=True)
value = {"CamelKey": 1}
bl[key] = [value] if nested_list else value

item = bl[-1]
if nested_list:
assert isinstance(item, BoxList)
item = item[0]
assert isinstance(item, Box)
assert item.camel_key == 1

@pytest.mark.parametrize("key", [slice(None), slice(None, None, 2)])
def test_setitem_slice_converts_containers(self, key):
bl = BoxList([0, 1, 2], camel_killer_box=True)
bl[key] = iter([{"CamelKey": 1}, [{"CamelKey": 2}]])

assert isinstance(bl[0], Box)
assert bl[0].camel_key == 1
assert isinstance(bl[-1], BoxList)
assert bl[-1][0].camel_key == 2
if key.step == 2:
assert bl[1] == 1

def test_setitem_preserves_intact_types(self):
class IntactDict(dict):
pass

class IntactList(list):
pass

value = IntactDict(a=1)
values = IntactList([value])
bl = BoxList([None, None], box_intact_types=(IntactDict, IntactList))
bl[0] = value
bl[1:] = [values]
assert bl[0] is value
assert bl[1] is values

def test_setitem_invalid_extended_slice(self):
bl = BoxList([0, 1, 2])
with pytest.raises(ValueError):
bl[::2] = iter([{"a": 1}])
assert bl == [0, 1, 2]

def test_frozen_list_slice_assignment(self):
bl = BoxList([0, 1, 2], frozen_box=True)
with pytest.raises(BoxError):
bl[:] = [{"a": 1}]
assert bl == [0, 1, 2]

def test_frozen_list(self):
bl = BoxList([5, 4, 3], frozen_box=True)
with pytest.raises(BoxError):
Expand Down