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

Skip to content

[MNT]: Cleanup ticklabel_format (style=) #26801

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 8 commits into from
Sep 20, 2023

Conversation

pedrompecanha
Copy link
Contributor

@pedrompecanha pedrompecanha commented Sep 17, 2023

PR summary

Fixes #25974.
-->

PR checklist

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

Copy link
Member

@rcomer rcomer left a comment

Choose a reason for hiding this comment

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

Thank you for your work on this @pedrompecanha! Just a couple of comments on the implementation...

This line will break if style is None, so perhaps should be moved into the else of your new if loop below.

style = style.lower()

is_sci_style = _api.check_getitem(STYLES, style=style)
STYLES = {'sci': True, 'scientific': True, 'plain': False}
if style is None:
is_sci_style = False
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
is_sci_style = False
is_sci_style = None

None here should mean that the style is not changed from what it previously was. Setting is_sci_style also to None achieves that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm changing it so that everything is in the dictionary. I believe it solves it more effectively. About setting it to None or False, I thought this variable should be either True or False but, after reading the logic after it, I've seen it can be None, indeed.

@rcomer
Copy link
Member

rcomer commented Sep 18, 2023

The typing stub file will also need updating to match the new signature

def ticklabel_format(
self,
*,
axis: Literal["both", "x", "y"] = ...,
style: Literal["", "sci", "scientific", "plain"] = ...,
scilimits: tuple[int, int] | None = ...,
useOffset: bool | float | None = ...,
useLocale: bool | None = ...,
useMathText: bool | None = ...
) -> None: ...

@@ -3283,8 +3284,11 @@ def ticklabel_format(self, *, axis='both', style='', scilimits=None,
except (ValueError, TypeError) as err:
raise ValueError("scilimits must be a sequence of 2 integers"
) from err
STYLES = {'sci': True, 'scientific': True, 'plain': False, '': None}
is_sci_style = _api.check_getitem(STYLES, style=style)
STYLES = {'sci': True, 'scientific': True, 'plain': False}
Copy link
Member

@oscargus oscargus Sep 18, 2023

Choose a reason for hiding this comment

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

An option here (and also in case someone has set style='' explicitly), is to do:

Suggested change
STYLES = {'sci': True, 'scientific': True, 'plain': False}
STYLES = {'sci': True, 'scientific': True, 'plain': False, '': None, None: None}
is_sci_style = _api.check_getitem(STYLES, style=style)

Copy link
Member

Choose a reason for hiding this comment

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

in case someone has set style='' explicitly

That's a good point. I suggest to also add a comment that '' is included for backwards-compatibility, as I do not think it would be obvious to future readers of the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the comment and added the empty string into the dictionary. Actually, I was playing tennis and thought about how my changes weren't backwards compatible. Nice to see we are in the same page :)

…ionary and added stub parameter alternative.
Whether to use scientific notation.
The formatter default is to use scientific notation.
Sci is equivalent to scientific.
The '' option is included solely for backwards-compatibility.
Copy link
Member

Choose a reason for hiding this comment

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

Apologies, I think I was unclear. I meant to add a code comment where the dictionary is defined, so that future developers know that it’s there for a reason. I don’t think it’s needed in the docstring.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in the latest commit!

Copy link
Member

@rcomer rcomer left a comment

Choose a reason for hiding this comment

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

The last test failure comes with instructions (which I admit are new to me!)

@@ -3283,7 +3284,9 @@ def ticklabel_format(self, *, axis='both', style='', scilimits=None,
except (ValueError, TypeError) as err:
raise ValueError("scilimits must be a sequence of 2 integers"
) from err
STYLES = {'sci': True, 'scientific': True, 'plain': False, '': None}
STYLES = {'sci': True, 'scientific': True, 'plain': False, '': None, None: None}
# 'sci' is equivalent to 'scientific'.
Copy link
Member

Choose a reason for hiding this comment

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

I think the fact that 'sci' and ‘scientific' are equivalent should be in the docstring, as this is useful information for the user.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just put them back there! As for the pyplot issue, I attempted following the instructions and it told me, in my system, that the file got reformatted. Let's see how it goes!

Copy link
Member

Choose a reason for hiding this comment

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

It seems the pyplot change has not appeared here on GitHub...

Copy link
Contributor Author

@pedrompecanha pedrompecanha Sep 19, 2023

Choose a reason for hiding this comment

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

Yeah! I tried running the command again, but the file remained unchanged. I tried to change the file by hand, although this is not encouraged, and the commit went through on my fork, but it won't open here on Github. Just goes straight to a 404 page. Weird.

Here is the final change I made: pedrompecanha@3cf0234

Copy link
Contributor Author

@pedrompecanha pedrompecanha Sep 19, 2023

Choose a reason for hiding this comment

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

I believe it has gone through now. Must have been a GitHub instability. The tests seem to be running well, although I'm kind of scared of having to change this file by hand. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

Your manual change matches what I get by running the script, so I think it's OK.

Copy link
Member

@rcomer rcomer left a comment

Choose a reason for hiding this comment

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

Thanks @pedrompecanha, I think this is in good shape now. I just have a tiny suggestion on the docstring.

@pedrompecanha
Copy link
Contributor Author

@rcomer, @timhoffm and @oscargus, thank you for the support and patience during this process! I'd like to ask that, if you find any similar cleanup or refactoring tasks, if you could please tag me, as I'll be really active in the next 1-2 months in here. Of course, if that is allowed by the rules of the project. I'll try my best to solve the issues. Again, thank you!

@rcomer rcomer added this to the v3.9.0 milestone Sep 20, 2023
@timhoffm timhoffm merged commit 2cbdff8 into matplotlib:main Sep 20, 2023
@timhoffm
Copy link
Member

Thanks @pedrompecanha and congratulations on your first contribution to Matplotlib! We hope to see you back.

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

Successfully merging this pull request may close these issues.

[MNT]: Cleanup ticklabel_format(..., style=)
4 participants