X Tutup
Skip to content

widgets: use a shared _Buttons class for {Radio,Check}Buttons#30997

Merged
timhoffm merged 5 commits intomatplotlib:mainfrom
doronbehar:_Buttons
Jan 23, 2026
Merged

widgets: use a shared _Buttons class for {Radio,Check}Buttons#30997
timhoffm merged 5 commits intomatplotlib:mainfrom
doronbehar:_Buttons

Conversation

@doronbehar
Copy link
Copy Markdown
Contributor

PR summary

A preparation refactoring commit for #30803 . Changes include:

  • widgets.pyi: Add missing RadioButtons.index_selected type hint
  • CheckButtons: rename _checks to _buttons
  • *Buttons: use a shared _Buttons class
  • *Buttons: use a semi-public self._buttons_ys variable
  • *Buttons: share more init functionality

PR checklist

Copy link
Copy Markdown
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

Please add an entry to widget_api.rst



class CheckButtons(AxesWidget):
class _Buttons(AxesWidget):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add a docstring.

The class should be treated analogous to _SelectorWidget, including

The class itself is private and may be changed or removed without prior warning.
However, the public API it provides to subclasses is stable and considered
public on the subclasses.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Please add a docstring.

Done.

@github-actions github-actions bot added the Documentation: API files in lib/ and doc/api label Jan 19, 2026
Copy link
Copy Markdown
Member

@story645 story645 left a comment

Choose a reason for hiding this comment

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

I assume that this is just the move things phase and code changes will happen in the follow up? Also thanks for doing this!


self._useblit = useblit

self._buttons_ys = np.linspace(1, 0, len(labels)+2)[1:-1]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

(Answering in a code review in order to explain better)

I assume that this is just the move things phase and code changes will happen in the follow up? Also thanks for doing this!

Correct. In the next PR the _Buttons class will also gain a self._buttons_xs attribute, which will be used when creating the _buttons in _init_props. As for self.labels, they will be created with the algorithm written in the previous PR, but with mtext.Text instanced once etc.

@doronbehar doronbehar requested a review from timhoffm January 23, 2026 01:48
@timhoffm timhoffm merged commit c77a979 into matplotlib:main Jan 23, 2026
37 of 40 checks passed
@QuLogic QuLogic added this to the v3.11.0 milestone Jan 23, 2026
@QuLogic
Copy link
Copy Markdown
Member

QuLogic commented Jan 24, 2026

This broke some cross-references, but it was hidden by the docs build being broken; I'll fix it in #31020.

doronbehar added a commit to doronbehar/matplotlib that referenced this pull request Jan 24, 2026
In matplotlib#30997 the classes `RadioButtons` & `CheckButtons` started sharing
more code, as they are fundamentally similar. When copy-pasting the
methods that were seemingly identical, the `_clicked` method was copied
from the original `CheckButtons` class, and there the `self._frames`
object was used instead of `self._buttons`. This caused an error when
actually using and clicking on buttons created with `RadioButtons`, as
the `RadioButtons._frames` doesn't exist - something that unfortunately
the tests did not catch.

Both `CheckButtons._frames` and `CheckButtons._buttons` are very similar
so even before matplotlib#30997 the `CheckButtons._clicked` method could have used
`self._checks` and not `self._frames`. Hence this change should be
harmless.
@doronbehar
Copy link
Copy Markdown
Contributor Author

Another fixup change that I noticed is needed when rewriting #30803 :

doronbehar added a commit to doronbehar/matplotlib that referenced this pull request Jan 28, 2026
In matplotlib#30997 the classes `RadioButtons` & `CheckButtons` started sharing
more code, as they are fundamentally similar. When copy-pasting the
methods that were seemingly identical, the `_clicked` method was copied
from the original `CheckButtons` class, and there the `self._frames`
object was used instead of `self._buttons`. This caused an error when
actually using and clicking on buttons created with `RadioButtons`, as
the `RadioButtons._frames` doesn't exist - something that unfortunately
the tests did not catch.

Both `CheckButtons._frames` and `CheckButtons._buttons` are very similar
so even before matplotlib#30997 the `CheckButtons._clicked` method could have used
`self._checks` and not `self._frames`. Hence this change should be
harmless.
doronbehar added a commit to doronbehar/matplotlib that referenced this pull request Jan 29, 2026
In matplotlib#30997 the classes `RadioButtons` & `CheckButtons` started sharing
more code, as they are fundamentally similar. When copy-pasting the
methods that were seemingly identical, the `_clicked` method was copied
from the original `CheckButtons` class, and there the `self._frames`
object was used instead of `self._buttons`. This caused an error when
actually using and clicking on buttons created with `RadioButtons`, as
the `RadioButtons._frames` doesn't exist - something that unfortunately
the tests did not catch.

Both `CheckButtons._frames` and `CheckButtons._buttons` are very similar
so even before matplotlib#30997 the `CheckButtons._clicked` method could have used
`self._checks` and not `self._frames`. Hence this change should be
harmless.
doronbehar added a commit to doronbehar/matplotlib that referenced this pull request Jan 29, 2026
In matplotlib#30997 the classes `RadioButtons` & `CheckButtons` started sharing
more code, as they are fundamentally similar. When copy-pasting the
methods that were seemingly identical, the `_clicked` method was copied
from the original `CheckButtons` class, and there the `self._frames`
object was used instead of `self._buttons`. This caused an error when
actually using and clicking on buttons created with `RadioButtons`, as
the `RadioButtons._frames` doesn't exist - something that unfortunately
the tests did not catch.

Both `CheckButtons._frames` and `CheckButtons._buttons` are very similar
so even before matplotlib#30997 the `CheckButtons._clicked` method could have used
`self._checks` and not `self._frames`. Hence this change should be
harmless.
doronbehar added a commit to doronbehar/matplotlib that referenced this pull request Jan 29, 2026
In matplotlib#30997 the classes `RadioButtons` & `CheckButtons` started sharing
more code, as they are fundamentally similar. When copy-pasting the
methods that were seemingly identical, the `_clicked` method was copied
from the original `CheckButtons` class, and there the `self._frames`
object was used instead of `self._buttons`. This caused an error when
actually using and clicking on buttons created with `RadioButtons`, as
the `RadioButtons._frames` doesn't exist - something that unfortunately
the tests did not catch.

Both `CheckButtons._frames` and `CheckButtons._buttons` are very similar
so even before matplotlib#30997 the `CheckButtons._clicked` method could have used
`self._checks` and not `self._frames`. Hence this change should be
harmless.
QuLogic added a commit that referenced this pull request Jan 30, 2026
RadioButtons: fix self._clicked method (followup to #30997)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

X Tutup