Skip to content
Merged
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
2 changes: 2 additions & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ reporting, all input is greatly appreciated!

Code contributions:

- Anish Pentyala

- Alexandre Bertho (alexbertho)
- parijat1222q

Expand Down
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Changelog
Version 8.0.1
-------------

* Preserve BoxList item comments when in-place repetition uses an object with __index__, without raising after changing the list

* Clear removed BoxList item comments after in-place repetition by zero or a negative count, so new items do not inherit stale YAML comments (thanks to Mrunmayee Daware)

* Fix ConfigBox.merge_update consuming one-shot key/value pairs before merging them
Expand Down
1 change: 1 addition & 0 deletions box/box_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def __iadd__(self, value): # type: ignore[misc] # list accepts any iterable fo
def __imul__(self, value): # type: ignore[misc]
if self.box_options.get("frozen_box"):
raise BoxError("BoxList is frozen")
value = operator.index(value)
length = len(self)
result = super().__imul__(value)
if len(self) == 0:
Expand Down
60 changes: 60 additions & 0 deletions test/test_imul_index_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""In-place repetition must normalize index-protocol counts before mutation."""

import operator

import pytest

from box import BoxError, BoxList


class IndexOnce:
def __init__(self, value):
self.value = value
self.calls = 0

def __index__(self):
self.calls += 1
if self.calls > 1:
raise AssertionError("count evaluated twice")
return self.value


@pytest.mark.parametrize("count", [2, 1, 0, -2])
@pytest.mark.parametrize("comments", [True, False])
def test_imul_index_count_normalized_once(count, comments):
values = BoxList(["a", "b"])
if comments:
values.set_comment(0, "first")
values.set_comment(1, "second")
key = IndexOnce(count)
result = operator.imul(values, key)
assert result is values
assert list(values) == ["a", "b"] * count
assert key.calls == 1
if comments:
for index in range(max(count, 0) * 2):
assert values.get_comment(index) == {"eol": "first" if index % 2 == 0 else "second"}
if count <= 0:
assert not values.__dict__.get("_box_comments")


def test_imul_invalid_index_preserves_data_and_comments():
class InvalidIndex:
def __index__(self):
raise ValueError("bad count")

values = BoxList(["a", "b"])
values.set_comment(1, "second")
with pytest.raises(ValueError, match="bad count"):
operator.imul(values, InvalidIndex())
assert list(values) == ["a", "b"]
assert values.get_comment(1) == {"eol": "second"}


def test_imul_frozen_refuses_before_index_conversion():
values = BoxList(["a"], frozen_box=True)
key = IndexOnce(2)
with pytest.raises(BoxError):
operator.imul(values, key)
assert key.calls == 0
assert list(values) == ["a"]