diff --git a/cycler/__init__.py b/cycler/__init__.py index 1f05670..db476b8 100644 --- a/cycler/__init__.py +++ b/cycler/__init__.py @@ -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) diff --git a/test_cycler.py b/test_cycler.py index 637521b..1eccac3 100644 --- a/test_cycler.py +++ b/test_cycler.py @@ -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