matplotlib icon indicating copy to clipboard operation
matplotlib copied to clipboard

Shorten various offsetbox implementations

Open anntzer opened this issue 4 years ago • 7 comments

PR Summary

1st commit: Inherit OffsetBox.{get_offset,get_window_extent}.

By making the parameters of OffsetBox.get_offset optional (which matches the fact that there are currently subclasses for which get_offset does not take any parameters), and adjusting OffsetBox.get_window_extent to use get_extent (which is typically redefined in subclasses) instead of get_extent_offsets (which is not), we can inherit the implementation of OffsetBox.get_window_extent across most subclasses instead of having to copy-paste it again and again.

2nd commit: Inline AnchoredOffsetBox._update_offset_func.

It is only called in get_window_extent -- and in draw, but that call is immediately followed by a call to get_window_extent, so it is redundant.

PR Checklist

Tests and Styling

  • [ ] Has pytest style unit tests (and pytest passes).
  • [ ] Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • [ ] New features are documented, with examples if plot related.
  • [ ] New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • [ ] API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • [ ] Documentation is sphinx and numpydoc compliant (the docs should build without error).

anntzer avatar Nov 21 '21 13:11 anntzer

I agree the API is pretty confusing, but that's what it was before. Still, it can be improved (last commit now) by making the callables passed to set_offset take no arguments whatsoever (they can call get_extent themselves), and therefore make get_offset also take no arguments. For backcompat I maintained the ability to pass *args, **kwargs if the callable passed to set_offset likewise takes such parameters (and likewise maintained in the AnchoredOffsetbox offset computer), but this can be deprecated in a later step.

anntzer avatar Feb 13 '22 15:02 anntzer

Making args to get_offset explicit makes everything much more complicated, because now I need to check the signature of self._offset (if it is a callable) to decide whether to pass args or not (even if you make the signature width=None, ... you cannot know whether the called did get_offset() or get_offset(None, ...) (which could be technically valid with whatever offset func they set previously). Likewise, making the docstring of set_offset more restrictive actually seems more confusing, as you'd now need to explicitly state the "old" signature which is something we want to get rid of. I have no qualms with deprecating the old set_offset form but again don't think it has to be done as part of this PR (which breaks no backcompat whatsoever, AFAICT).

anntzer avatar Feb 13 '22 22:02 anntzer

Done (with minor rewordings on top of the proposed docs).

anntzer avatar Apr 11 '22 22:04 anntzer

@tacaswell has told me that the message makes sense from the contents, but that last commit message is a bit inscrutable at first glance.

QuLogic avatar Apr 14 '22 05:04 QuLogic

Agreed, is the new title better? :)

anntzer avatar Apr 14 '22 10:04 anntzer

This behavior / signature goes back to 3ae92215dae8f55903f6bc6c8c063e5cb7498bac which is related to mouse-draggable artists.

I do not feel I understand why it was there to begin with to be confident ripping it out.

tacaswell avatar Apr 19 '22 13:04 tacaswell

Moved to draft. Seems to at least need an Api note, and response to comments above

jklymak avatar Jun 02 '22 12:06 jklymak

Superseded by #23896 and #23907.

anntzer avatar Nov 01 '22 12:11 anntzer