Skip to content

Commit

Permalink
Merge pull request #103 from tacaswell/fix/inplace_no_len_check
Browse files Browse the repository at this point in the history
FIX: check that lengths match with in-place addition
  • Loading branch information
QuLogic authored Feb 27, 2025
2 parents b952686 + 4638537 commit d1737eb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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

0 comments on commit d1737eb

Please sign in to comment.