Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MNT: Fix handling of ints in hsv_to_rgb() - #32058

Merged
QuLogic merged 1 commit into
matplotlib:mainfrom
Sreekant13:fix/hsv-to-rgb-int-input
Jul 21, 2026
Merged

MNT: Fix handling of ints in hsv_to_rgb()#32058
QuLogic merged 1 commit into
matplotlib:mainfrom
Sreekant13:fix/hsv-to-rgb-int-input

Conversation

@Sreekant13

Copy link
Copy Markdown
Contributor

PR summary

On NumPy 2.0, hsv_to_rgb() raises ValueError: Unable to avoid copy while creating an array as requested for integer input, for example:

import numpy as np
import matplotlib.colors as mcolors
mcolors.hsv_to_rgb(np.array([0, 1, 1]))  # ValueError on NumPy 2.0

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 and copy=False forbids 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 to hsv_to_rgb(). This mirrors it: use np.asarray() for the dtype promotion and expand a 1D input to 2D explicitly. Behavior for float and 2D input is unchanged. Adds test_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

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().
@QuLogic QuLogic added this to the v3.11.2 milestone Jul 21, 2026
@QuLogic
QuLogic merged commit a4e6ea1 into matplotlib:main Jul 21, 2026
39 of 41 checks passed
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())
@Sreekant13

Copy link
Copy Markdown
Contributor Author

Thanks for reviewing and merging this. Appreciate the maintainers keeping matplotlib in such good shape.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants