Skip to content

Fix loc legend validation #24649

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

Closed
wants to merge 1 commit into from

Conversation

jpjepko
Copy link
Contributor

@jpjepko jpjepko commented Dec 6, 2022

PR Summary

This pull request adds a check to the loc argument of legend. It checks that loc is a 2-tuple of floats or ints, and raises a ValueError otherwise. This addresses the issue #24605. The docstrings already mentioned the fact that loc is a pair of floats so we did not add any changes to any docstrings. We also added test cases for valid and invalid tuples.

PR Checklist

Documentation and Tests

  • Has pytest style unit tests (and pytest passes)
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • New plotting related features are documented with examples.

Release Notes

  • New features are marked with a .. versionadded:: directive in the docstring and documented in doc/users/next_whats_new/
  • API changes are marked with a .. versionchanged:: directive in the docstring and documented in doc/api/next_api_changes/
  • Release notes conform with instructions in next_whats_new/README.rst or next_api_changes/README.rst

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 while, please feel free to ping @matplotlib/developers or anyone who has commented on the PR. 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.

@tacaswell tacaswell added this to the v3.7.0 milestone Dec 7, 2022
@tacaswell
Copy link
Member

tacaswell commented Dec 8, 2022

It looks like what happened is when you tried to push it failed, the message suggested you merge, you did so, and then the push worked?

What is going on is that by default git's push tries to keep you from accidentally make commits inaccessible so if a push would orphan commits git fails and github helpfully suggest you merge the remote work with your and then push (both of which are good ideas). However, in this case it is our goal to re-write history and we want to make some commits disappear and replace them with new and improved commits so we need to tell git that we really mean it. The incantation you want here is

git push --force-with-lease YOUR_REPO fix-loc-legend-validation

which will tell git to do the push anyway, so long as you have at least had a chance to see the commits that you are going to be making unavailable (there is also --force that does in no questions asked but the extra checking in --force-with-lease saves me about once a month).

@ksunden
Copy link
Member

ksunden commented Dec 8, 2022

(just edited because there was a 1 character typo in there leave -> lease)

@jpjepko
Copy link
Contributor Author

jpjepko commented Dec 9, 2022

I believe I had forgotten to point the upstream to the matplotlib repo

git remote add upstream https://github.com/matplotlib/matplotlib.git

Could that have been the culprit? In any case, would you prefer for me to open a new PR to keep the commit history clean?

@QuLogic
Copy link
Member

QuLogic commented Dec 10, 2022

There's no need for that; rebase and drop the extra commits, then follow @tacaswell's instructions for force pushing the new ones.

@jpjepko jpjepko force-pushed the fix-loc-legend-validation branch 3 times, most recently from 03813be to a7e867c Compare December 11, 2022 22:03
@jpjepko
Copy link
Contributor Author

jpjepko commented Dec 11, 2022

Thank you for your guidance. I cleaned up the commit history, rebased, and force pushed as per the updated Development workflow in the dev docs. Are there any other changes you would like for me to incorporate?

@jpjepko jpjepko force-pushed the fix-loc-legend-validation branch from a7e867c to aacac4c Compare December 13, 2022 02:10
@jpjepko
Copy link
Contributor Author

jpjepko commented Dec 13, 2022

Hey @QuLogic, is there anything else that might need any changes? Thank you for your continued guidance.

Comment on lines 476 to 482
if len(loc) != 2:
raise ValueError(
"loc must be string or pair of floats")
for loc_elt in loc:
# int can be converted to float, so ints are fine
if not isinstance(loc_elt, (float, int)):
raise ValueError(f"{loc_elt} is not a valid value for loc")
Copy link
Member

@timhoffm timhoffm Dec 13, 2022

Choose a reason for hiding this comment

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

Suggested change
if len(loc) != 2:
raise ValueError(
"loc must be string or pair of floats")
for loc_elt in loc:
# int can be converted to float, so ints are fine
if not isinstance(loc_elt, (float, int)):
raise ValueError(f"{loc_elt} is not a valid value for loc")
if len(loc) != 2 or not all(isinstance(e, Real) for e in loc):
raise ValueError(
"loc must be string or pair of numbers, not {loc!r}")
else:
raise ValueError(
"loc must be string or pair of numbers, not {loc!r}")
  • simplify to check for Number Real
  • merge the len and the number checks
  • error out for other input types

edit: modified to include suggestions by @QuLogic and @tacaswell

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 we want numbers.Real, as in:

elif isinstance(location, numbers.Real):
self._pos = location

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 incorporated these changes in the original commit. Thanks!

@tacaswell
Copy link
Member

Sorry to be yet another person chiming in from the 🥜 gallery, but can you also add the repr of what the user did pass to the error message?

@tacaswell
Copy link
Member

I'm going to milestone this as 3.8, if it is ready and merged it can definitely go in for 3.7, but I am trying to whittle down the milestone to things we would hold 3.8 over in the next 2 weeks!

@tacaswell tacaswell modified the milestones: v3.7.0, v3.8.0 Dec 15, 2022
@jpjepko jpjepko force-pushed the fix-loc-legend-validation branch from aacac4c to 5b5d17d Compare December 22, 2022 22:34
@jpjepko jpjepko force-pushed the fix-loc-legend-validation branch from 5b5d17d to 0b7fcba Compare December 22, 2022 22:43
@rcomer rcomer linked an issue Dec 23, 2022 that may be closed by this pull request
QuLogic
QuLogic previously approved these changes Jan 5, 2023
@QuLogic QuLogic modified the milestones: v3.8.0, v3.7.0 Jan 5, 2023
@QuLogic QuLogic dismissed their stale review January 5, 2023 06:49

Oops, just realized that tests not passing are the result of this change.

Copy link
Member

@QuLogic QuLogic left a comment

Choose a reason for hiding this comment

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

It's not documented in the type, but it is documented in the description, that loc also accepts the numbers 0 through 10. You can add this (without magic numbers) by checking whether loc in self.codes.items().

This should fix the failing tests.

@tacaswell tacaswell modified the milestones: v3.7.0, v3.8.0 Jan 5, 2023
@tacaswell
Copy link
Member

Pushed back to 3.8 as there are failing tests still.

@jklymak
Copy link
Member

jklymak commented Jan 30, 2023

@jpjepko are you hoping to come back to this? Let us know if you need anything, but it looks like you are missing some valid loc values

@jklymak jklymak marked this pull request as draft February 2, 2023 17:33
@rcomer
Copy link
Member

rcomer commented Mar 7, 2023

Closing in favour of #25281.

@rcomer rcomer closed this Mar 7, 2023
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.

[Bug]: Validation not performed for loc argument to legend
7 participants