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: 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 ------------- 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 {} 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] 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):