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

Skip to content

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

Merged
merged 1 commit into from
Apr 15, 2019

Conversation

timhoffm
Copy link
Member

@timhoffm timhoffm commented Apr 9, 2019

PR Summary

matplotlib.colors.to_rgba() and matplotlib.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.

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.,)
Copy link
Contributor

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 :)

@tacaswell tacaswell added this to the v3.2.0 milestone Apr 11, 2019
Copy link
Member

@tacaswell tacaswell left a 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.

else:
if not (0 <= c <= 1):
raise ValueError(
"String grayscale values must be within 0-1 range")

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)

Copy link
Member

@dstansby dstansby left a 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

@dopplershift
Copy link
Contributor

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.

@ImportanceOfBeingErnest
Copy link
Member

It's an API change in the very sense of the definition. You ask the interface a question, like is_colorlike("5") and it gives an answer, True. Now this is changed to give the answer False. Clearly this change is useful and desired, but it's always good to document those changes in the list that exists for that very purpose.

@timhoffm
Copy link
Member Author

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 is_colorlike('5') had raised an error instead? Also I doubt, that this could have been used in working code, because using the returned (5., 5., 5., 1.) tuple anywhere in matplotlib would have raised an error there. In that sense, I don't think this a breaking API change.

@dopplershift
Copy link
Contributor

@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.

@dstansby
Copy link
Member

There might be people out there who rely on the behaviour is_colorlike("5"), and I think it's better to at least leave a note somewhere to tell them why it's not working any more.

@dstansby dstansby dismissed their stale review April 14, 2019 15:18

API note added

Copy link
Member

@efiring efiring left a 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.

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.

is_color_like returning erroneous value on strings of integers
7 participants