From 0f5f258ee320f8a92093a21fed48fc38dd09a57e Mon Sep 17 00:00:00 2001 From: Anish Pentyala Date: Wed, 11 Nov 2026 16:55:20 -0800 Subject: [PATCH 1/5] Normalize in-place BoxList repetition counts before mutation --- box/box_list.py | 1 + 1 file changed, 1 insertion(+) diff --git a/box/box_list.py b/box/box_list.py index c4ba946..7bf6c70 100644 --- a/box/box_list.py +++ b/box/box_list.py @@ -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: From ad434c68cad139eb2dec62ce17f99cd1bedb9184 Mon Sep 17 00:00:00 2001 From: Anish Pentyala Date: Wed, 11 Nov 2026 16:55:37 -0800 Subject: [PATCH 2/5] Document in-place repetition count fix --- CHANGES.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 29f807b..dc749bb 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 From 20f1d88b2ee6faea5b7eae38eec8712d4011b695 Mon Sep 17 00:00:00 2001 From: Anish Pentyala Date: Wed, 11 Nov 2026 16:55:55 -0800 Subject: [PATCH 3/5] Add contributor credit for repetition fix --- AUTHORS.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index 43c5149..ea7e27c 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -5,6 +5,8 @@ reporting, all input is greatly appreciated! Code contributions: +- Anish Pentyala + - Alexandre Bertho (alexbertho) - parijat1222q From 86f2c5075bd8956908fe3a083c2d37fe31410324 Mon Sep 17 00:00:00 2001 From: Anish Pentyala Date: Wed, 11 Nov 2026 16:56:13 -0800 Subject: [PATCH 4/5] Test index-protocol repetition comment consistency --- test/test/test_imul_index_comments.py | 60 +++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 test/test/test_imul_index_comments.py diff --git a/test/test/test_imul_index_comments.py b/test/test/test_imul_index_comments.py new file mode 100644 index 0000000..fddfbf7 --- /dev/null +++ b/test/test/test_imul_index_comments.py @@ -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"] From 683d9a0efdb90b190675f1fc8396fae742a13f49 Mon Sep 17 00:00:00 2001 From: Anish Pentyala Date: Wed, 11 Nov 2026 16:56:40 -0800 Subject: [PATCH 5/5] Place repetition regression tests in the existing test directory --- test/{test => }/test_imul_index_comments.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/{test => }/test_imul_index_comments.py (100%) diff --git a/test/test/test_imul_index_comments.py b/test/test_imul_index_comments.py similarity index 100% rename from test/test/test_imul_index_comments.py rename to test/test_imul_index_comments.py