MNT: Fix handling of ints in hsv_to_rgb() - #32058
Merged
Merged
Conversation
hsv_to_rgb() built its working array with np.array(hsv, copy=False, dtype=...), which raises "Unable to avoid copy while creating an array as requested" on NumPy 2.0 whenever the input needs a dtype cast, so integer input such as hsv_to_rgb((0, 1, 1)) crashed even though the "Don't work on ints" comment shows ints are meant to be promoted to float. This is the same NumPy 2.0 regression that matplotlib#30815 fixed in the twin function rgb_to_hsv(); the same fix was never applied to hsv_to_rgb(). Mirror it here: use np.asarray for the dtype promotion and expand a 1D input to 2D explicitly. Adds test_hsv_to_rgb_int().
timhoffm
approved these changes
Jul 19, 2026
QuLogic
approved these changes
Jul 21, 2026
QuLogic
added a commit
that referenced
this pull request
Jul 21, 2026
QuLogic
added a commit
that referenced
this pull request
Jul 21, 2026
…058-on-v3.11.x Backport PR #32058 on branch v3.11.x (MNT: Fix handling of ints in hsv_to_rgb())
Contributor
Author
|
Thanks for reviewing and merging this. Appreciate the maintainers keeping matplotlib in such good shape. |
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.
PR summary
On NumPy 2.0,
hsv_to_rgb()raisesValueError: Unable to avoid copy while creating an array as requestedfor integer input, for example:The function built its working array with
np.array(hsv, copy=False, dtype=np.promote_types(...)). When the input dtype differs from the target (as with integer input), NumPy 2.0 needs a copy to perform the cast andcopy=Falseforbids it, so the call raises. The "Don't work on ints" comment shows integer input is meant to be promoted to float, so this is a regression rather than intended behavior.This is the same NumPy 2.0 regression that #30815 fixed in the twin function
rgb_to_hsv(), but the same fix was never applied tohsv_to_rgb(). This mirrors it: usenp.asarray()for the dtype promotion and expand a 1D input to 2D explicitly. Behavior for float and 2D input is unchanged. Addstest_hsv_to_rgb_int().AI Disclosure
AI (Claude Code) was used to locate the missing twin fix. I drafted the change, wrote the regression test, reviewed and verified the change, including reproducing the crash and confirming the fix returns the correct colors.
PR checklist