From d3f0d31ded4094c9300b385e173cb16f25aff6f5 Mon Sep 17 00:00:00 2001 From: Rahul Ranjan Date: Mon, 9 Nov 2026 09:29:37 +0000 Subject: [PATCH] Make BoxList + and * return a BoxList The out-of-place operators fell through to plain list, dropping the class, the box options and the conversion of appended items: BoxList([{'a': 1}]) + [{'b': 2}] -> plain list, second item a raw dict BoxList([{'a': 1}]) * 2 -> plain list __add__ and __mul__ now return a BoxList with the same box options; appended items are converted the same way append/extend convert them, and a frozen operand produces a frozen result. The in-place variants were already correct. Fixes #358 Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com> --- box/box_list.py | 10 ++++++++++ test/test_box_list.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/box/box_list.py b/box/box_list.py index 8de6b2f0..15ba282d 100644 --- a/box/box_list.py +++ b/box/box_list.py @@ -145,6 +145,16 @@ def converted_items(): return super().__setitem__(key, self._convert(value)) + def __add__(self, other): + if not isinstance(other, list): + raise BoxTypeError("BoxList can only be added to another list.") + return box.BoxList(list(self) + list(other), **self.box_options) + + def __mul__(self, count): + if not isinstance(count, int): + raise BoxTypeError("BoxList can only be multiplied by an integer.") + return box.BoxList(list(self) * count, **self.box_options) + def __iadd__(self, value): # type: ignore[misc] # list accepts any iterable for +=, not only lists if self.box_options.get("frozen_box"): raise BoxError("BoxList is frozen") diff --git a/test/test_box_list.py b/test/test_box_list.py index 469c3e98..15df57b0 100644 --- a/test/test_box_list.py +++ b/test/test_box_list.py @@ -14,6 +14,7 @@ from ruamel.yaml import YAML from box import Box, BoxError, BoxList +from box.exceptions import BoxTypeError from box.converters import toml_read_library, toml_write_library from test.common import test_root, tmp_dir @@ -666,3 +667,41 @@ def test_iadd_frozen_does_not_consume_source(): data += source assert next(source) == {"value": 1} assert data == [0] + + +class TestBoxListOperators: + def test_add_keeps_boxlist_and_converts(self): + bl = BoxList([{"a": 1}], box_dots=True) + result = bl + [{"b": 2}] + assert isinstance(result, BoxList) + assert isinstance(result[0], Box) + assert isinstance(result[1], Box) + assert result.box_options["box_dots"] is True + + def test_add_two_boxlists(self): + result = BoxList([{"a": 1}]) + BoxList([{"b": 2}]) + assert isinstance(result, BoxList) + assert result.to_list() == [{"a": 1}, {"b": 2}] + + def test_add_non_list_raises(self): + with pytest.raises(BoxTypeError): + BoxList() + 5 + + def test_mul_keeps_boxlist(self): + bl = BoxList([{"a": 1}], box_dots=True) + result = bl * 3 + assert isinstance(result, BoxList) + assert len(result) == 3 + assert result.box_options["box_dots"] is True + assert result.to_list() == [{"a": 1}] * 3 + + def test_mul_non_int_raises(self): + with pytest.raises(BoxTypeError): + BoxList() * "x" + + def test_frozen_add_result_is_frozen(self): + frozen = BoxList([{"a": 1}], frozen_box=True) + result = frozen + [{"b": 2}] + assert isinstance(result, BoxList) + with pytest.raises(BoxError): + result.append({"c": 3})