Skip to content

Commit d1737eb

Browse files
authored
Merge pull request #103 from tacaswell/fix/inplace_no_len_check
FIX: check that lengths match with in-place addition
2 parents b952686 + 4638537 commit d1737eb

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

cycler/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ def __iadd__(self, other: Cycler[K, V]) -> Cycler[K, V]: # type: ignore[misc]
355355
"""
356356
if not isinstance(other, Cycler):
357357
raise TypeError("Cannot += with a non-Cycler object")
358+
if len(self) != len(other):
359+
raise ValueError(
360+
f"Can only add equal length cycles, not {len(self)} and {len(other)}"
361+
)
358362
# True shallow copy of self is fine since this is in-place
359363
old_self = copy.copy(self)
360364
self._keys = _process_keys(old_self, other)

test_cycler.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,22 @@ def test_prod():
6565
_cycler_helper(c3 * c1, 45, ['lw', 'c'], target)
6666

6767

68-
def test_inplace():
68+
def test_inplace_add():
6969
c1 = cycler(c='rgb')
7070
c2 = cycler(lw=range(3))
7171
c2 += c1
7272
_cycler_helper(c2, 3, ['c', 'lw'], [list('rgb'), range(3)])
7373

74+
75+
def test_inplace_add_len_mismatch():
76+
# miss-matched add lengths
77+
c1 = cycler(c='rgb')
78+
c3 = cycler(lw=range(15))
79+
with pytest.raises(ValueError):
80+
c1 += c3
81+
82+
83+
def test_inplace_mul():
7484
c3 = cycler(c='rgb')
7585
c4 = cycler(lw=range(3))
7686
c3 *= c4

0 commit comments

Comments
 (0)