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
4 changes: 4 additions & 0 deletions cycler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ def __iadd__(self, other: Cycler[K, V]) -> Cycler[K, V]: # type: ignore[misc]
"""
if not isinstance(other, Cycler):
raise TypeError("Cannot += with a non-Cycler object")
if len(self) != len(other):
raise ValueError(
f"Can only add equal length cycles, not {len(self)} and {len(other)}"
)
# True shallow copy of self is fine since this is in-place
old_self = copy.copy(self)
self._keys = _process_keys(old_self, other)
Expand Down
12 changes: 11 additions & 1 deletion test_cycler.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,22 @@ def test_prod():
_cycler_helper(c3 * c1, 45, ['lw', 'c'], target)


def test_inplace():
def test_inplace_add():
c1 = cycler(c='rgb')
c2 = cycler(lw=range(3))
c2 += c1
_cycler_helper(c2, 3, ['c', 'lw'], [list('rgb'), range(3)])


def test_inplace_add_len_mismatch():
# miss-matched add lengths
c1 = cycler(c='rgb')
c3 = cycler(lw=range(15))
with pytest.raises(ValueError):
c1 += c3


def test_inplace_mul():
c3 = cycler(c='rgb')
c4 = cycler(lw=range(3))
c3 *= c4
Expand Down
Loading