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
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Code contributions:
- gyanu2507 (gyanu2507)
- Rahul (rahul05ranjan)
- Anish Pentyala (anishpentyala)
- Waseem Mushtaq (waseem1302-x)

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,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
-------------

Expand Down
5 changes: 4 additions & 1 deletion box/box_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
1 change: 1 addition & 0 deletions box/box_list.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
27 changes: 27 additions & 0 deletions test/test_box_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down