From 1bd4eddc72350d9cf2cd3ca93984534b7b130ec5 Mon Sep 17 00:00:00 2001 From: Waseem Mushtaq Date: Sat, 14 Nov 2026 11:54:39 +0800 Subject: [PATCH 1/5] Add BoxList.copy() returning a BoxList; __copy__ delegates to it --- box/box_list.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/box/box_list.py b/box/box_list.py index 7bf6c70..b227d49 100644 --- a/box/box_list.py +++ b/box/box_list.py @@ -403,11 +403,14 @@ def __repr__(self): def __str__(self): return str(self.to_list()) - def __copy__(self): + def copy(self): out = self.__class__((x for x in self), **self.box_options) self._comment_copy_to(out) return out + def __copy__(self): + return self.copy() + def __deepcopy__(self, memo=None): out = self.__class__(**{**self.box_options, "frozen_box": False}) memo = memo or {} From 3dcbea7567329c5cc59e0abd7955287fdc8c26e8 Mon Sep 17 00:00:00 2001 From: Waseem Mushtaq Date: Sat, 14 Nov 2026 11:55:22 +0800 Subject: [PATCH 2/5] Add BoxList.copy() stub to box_list.pyi --- box/box_list.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/box/box_list.pyi b/box/box_list.pyi index ff97496..738c77a 100644 --- a/box/box_list.pyi +++ b/box/box_list.pyi @@ -30,6 +30,7 @@ class BoxList(list): def append(self, p_object: Any) -> None: ... def extend(self, iterable: Any) -> None: ... def insert(self, index: Any, p_object: Any) -> None: ... + def copy(self) -> BoxList: ... def __copy__(self) -> BoxList: ... def __deepcopy__(self, memo: Any | None = ...) -> BoxList: ... def __hash__(self) -> int: ... # type: ignore[override] From 2cdb6d9cbdc41b9e4864a71832a9fc07f28d29be Mon Sep 17 00:00:00 2001 From: Waseem Mushtaq Date: Sat, 14 Nov 2026 11:56:26 +0800 Subject: [PATCH 3/5] Add tests for BoxList.copy() --- test/test_box_list.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/test_box_list.py b/test/test_box_list.py index d13a837..360c54a 100644 --- a/test/test_box_list.py +++ b/test/test_box_list.py @@ -197,6 +197,33 @@ def test_deepcopy_preserves_box_options(self): clone.append(1) assert hash(clone) == hash(frozen) + def test_copy_returns_a_box_list(self): + import copy + + # .copy() must return a BoxList like copy.copy does, not a plain list + bl = BoxList([{"a": 1}, 2], box_dots=True) + copied = bl.copy() + assert type(copied) is BoxList + assert copied == bl and copied is not bl + assert copied.box_options.get("box_dots") is True + assert copied["[0].a"] == 1 + assert type(copy.copy(bl)) is BoxList + + def test_copy_keeps_item_comments(self): + bl = BoxList([1, 2]) + bl.set_comment(0, "first") + copied = bl.copy() + assert copied.get_comment(0) == {"eol": "first"} + + def test_copy_of_frozen_box_list_stays_frozen(self): + frozen = BoxList([1, 2], frozen_box=True) + copied = frozen.copy() + assert type(copied) is BoxList + assert copied.box_options.get("frozen_box") is True + with pytest.raises(BoxError): + copied.append(3) + assert hash(copied) == hash(frozen) + @pytest.mark.parametrize("index", [0, -1, "[0]", "[1]"]) @pytest.mark.parametrize("container", ["mapping", "sequence"]) def test_assigned_containers_are_converted(self, index, container): From 2e2ca3b772afc28203f50863af9e79adaa09e231 Mon Sep 17 00:00:00 2001 From: Waseem Mushtaq Date: Sat, 14 Nov 2026 11:57:10 +0800 Subject: [PATCH 4/5] Add Unreleased changelog entry for BoxList.copy() fix Fix BoxList.copy() to return a BoxList instead of a plain list, preserving box_options and item comments. --- CHANGES.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 826d748..4276e46 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,5 +1,10 @@ Changelog +Unreleased +---------- + +* Fixing BoxList.copy() returning a plain list instead of a BoxList, losing its box_options (box_dots, frozen_box) and item comments. A frozen BoxList's copy was even mutable (thanks to Waseem Mushtaq) + Version 8.0.1 ------------- From 022a74aab24c5a28f3043eef79a4dbf7f279c352 Mon Sep 17 00:00:00 2001 From: Waseem Mushtaq Date: Sat, 14 Nov 2026 11:57:49 +0800 Subject: [PATCH 5/5] Add Waseem Mushtaq to AUTHORS.rst Added Waseem Mushtaq to the list of authors. --- AUTHORS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index b2f08ec..ac23084 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -61,6 +61,7 @@ Code contributions: - gyanu2507 (gyanu2507) - Rahul (rahul05ranjan) - Anish Pentyala (anishpentyala) +- Waseem Mushtaq (waseem1302-x) Suggestions and bug reporting: