fix: don't mutate the source stops in ColorStops.reversed() - #147
Merged
tlambert03 merged 3 commits intoAug 10, 2026
Merged
Conversation
`self._stops[::-1]` is a view, so rewriting the position column wrote through to the object being reversed: its positions became `1 - p` while its colors stayed put, leaving stops in descending order that the constructor itself rejects. Evaluating the original colormap afterwards gave a wrong ramp, and a second `.reversed()` call disagreed with the first. `__init__` stores arrays uncopied, so the returned object aliased the same buffer. Copying the reversed view first fixes both, and matches `shifted()`, which already copies before rewriting positions. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Reviewed-By: Codex (gpt-5.6-sol, reasoning effort xhigh)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #147 +/- ##
=======================================
Coverage 95.70% 95.70%
=======================================
Files 168 168
Lines 2188 2188
=======================================
Hits 2094 2094
Misses 94 94 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
matthiasschabel
marked this pull request as ready for review
August 9, 2026 21:39
tlambert03
enabled auto-merge (squash)
August 10, 2026 07:39
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ColorStops.reversed()corrupts the object it is called on (src/cmap/_colormap.py:1164).self._stops[::-1]is a view, sorev_stops[:, 0] = 1 - rev_stops[:, 0]writes through to the source: every positionpbecomes1 - pwhile the colors stay where they were. A red/green/blue ramp at[0, 0.5, 1]is left holding[1, 0.5, 0]— descending stops the constructor itself rejects with "Color stops must be in ascending position".__init__stores arrays uncopied (:918), so the returned object aliases that buffer too.Two
.reversed()calls on the same object also disagree, since the first corrupts the input to the second.Copying the reversed view before rewriting positions fixes both halves, and matches
shifted()twelve lines below, which already copies before rewriting positions.Nothing exercised this: the existing reversal assertions use the
reversed()builtin (__reversed__), a different method, and cmap's two internal callers discard the corrupted source immediately.🤖 Generated with Claude Code