Skip to content

Conversation

dstansby
Copy link
Member

@dstansby dstansby commented Jan 3, 2021

xref #19223. I think checking for numbers.Real is the right thing to do here?

if isinstance(s, str):
_api.check_in_list(['vertical', 'horizontal'], rotation=s)
elif s is not None:
cbook._check_isinstance(numbers.Real, s=s)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

either both should be s=s, or both should be rotation=s, depending on whether you want to strictly match the kwarg name, or use a semantically more meaningful name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both should be rotation=s. It's understandable in the context of set_rotation() as well as in Text(..., rotation=).
For the latter s=s would result in "'invalid string' is not a valid value for s" which is quite unhelpful.

Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modulo my comment, which is not strictly necessary, though.

@@ -232,7 +232,7 @@ def test_add_subplot_invalid():
def test_suptitle():
fig, _ = plt.subplots()
fig.suptitle('hello', color='r')
fig.suptitle('title', color='g', rotation='30')
fig.suptitle('title', color='g', rotation=30)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess strictly speaking this is backcompat breaking? I doubt it's worth a deprecation period, but it should be noted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not even sure we need to mention it. '30' is not a supported format according to docs. It just happens to work. If we drop that without deprecation period, a note does not really help anybody. Nobody goes through the note and says "hey rotation does not take string numbers anymore. That's my thing, I'm doing that all the time". I'll bet anything if anybody passes '30', they will only notice by the error message they will get.

@timhoffm
Copy link
Member

timhoffm commented Apr 5, 2021

@dstansby please change to cbook._check_isinstance(numbers.Real, rotation=s). See the open comment above.

@jklymak jklymak marked this pull request as draft April 23, 2021 17:01
@dstansby dstansby force-pushed the validate-rotation branch 2 times, most recently from d021b94 to d845a57 Compare December 28, 2021 10:18
@dstansby dstansby added this to the v3.6.0 milestone Dec 28, 2021
@dstansby
Copy link
Member Author

I finally got round to rebasing this, so should be good for review again.

@timhoffm
Copy link
Member

@@ -1177,6 +1178,10 @@ def set_rotation(self, s):
The rotation angle in degrees in mathematically positive direction
(counterclockwise). 'horizontal' equals 0, 'vertical' equals 90.
"""
if isinstance(s, str):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two separate checks lead to slightly confusing messages as they don't mention the respective other valid parameters, e.g.

ValueError: '30' is not a valid value for rotation; supported values are 'vertical', 'horizontal'

does not mention numbers.

I'd go for a manual check and message here:

if s is not None and not isinstance(s, numbers.Real) and s not in ['vertical', 'horizontal']:
    Raise ValueError("rotation must be 'vertical', 'horizontal' or a number, not {s!r}"]

@dstansby dstansby marked this pull request as ready for review December 28, 2021 15:09
@QuLogic QuLogic merged commit 2465eff into matplotlib:main Jan 5, 2022
@dstansby dstansby deleted the validate-rotation branch January 5, 2022 10:31
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.

4 participants