-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix string numbers in to_rgba() and is_color_like() #13913
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
Conversation
lib/matplotlib/colors.py
Outdated
if not (0 <= c <= 1): | ||
raise ValueError( | ||
"String grayscale values must be within 0-1 range") | ||
return (float(c),) * 3 + (alpha if alpha is not None else 1.,) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to convert to float again, (c, c, c, alpha if ... else 1)
is good enough :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
modulo dropping the extra float call.
lib/matplotlib/colors.py
Outdated
else: | ||
if not (0 <= c <= 1): | ||
raise ValueError( | ||
"String grayscale values must be within 0-1 range") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All other ValueErrors report back the orig_c
. Would it make sense to do so here as well?
Possibly:
"Invalid string grayscale value {!r}. Values must be within 0-1 range.".format(orig_c)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs an API changes note
I'm not sure I agree that this is an API change. I'd say it was a bug that this behavior ever worked. IMO, an API change is a change from one intentional behavior to a different intentional behavior. Behavior that was not intended is not part of the API. That's not to say the fix won't be disruptive, but that's the nature of development. |
It's an API change in the very sense of the definition. You ask the interface a question, like |
3685562
to
de7b0c6
Compare
de7b0c6
to
dbc205d
Compare
Added an API change note. Even though I'm on the edge of argumenting that this adds more noise than helps. Would we have added an API change note if |
@ImportanceOfBeingErnest By that definition, every bug fix is an API change. And I don't think that's hyperbole based on your argument. I'm not trying to die on this hill and if people want to put in an API change note for this, I don't actually care. I'm more confused and surprised that the consensus is in favor of this being an API change. |
There might be people out there who rely on the behaviour |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the "It's a bug, not an API change" argument, but let's stop arguing and get this thing in. The API note can't do much harm. It can even be removed later if a higher power so decides.
PR Summary
matplotlib.colors.to_rgba()
andmatplotlib.colors.is_colorlike()
accepted strings representing numbers outside the 0-1 range (converting'5'
to(5.0, 5.0, 5.0, 1.0)
). This is a bug and should error out.Closes #13912.