Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: check that lengths match with in-place addition #103

Merged
merged 1 commit into from
Feb 27, 2025
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