Skip to content
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

Add @QtCore.Slot() decorations to NavigationToolbar2QT #27215

Merged
merged 3 commits into from
Nov 18, 2023
Merged

Add @QtCore.Slot() decorations to NavigationToolbar2QT #27215

merged 3 commits into from
Nov 18, 2023

Conversation

bersbersbers
Copy link
Contributor

PR summary

Add @QtCore.Slot() decorations to NavigationToolbar2QT to suppress "Registering dynamic slot" warnings. Closes #27214.

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.

@bersbersbers
Copy link
Contributor Author

Unfortunately, I haven't found a workaround for https://bugreports.qt.io/browse/PYSIDE-2512 that does not use a lambda. Should I add # noqa: E731 for this one?

@anntzer
Copy link
Contributor

anntzer commented Oct 27, 2023

A similar workaround would be to wrap with functools.partial, i.e. QtCore.Slot()(functools.partial(getattr(self, callback))) (which all fits in a single line).

@bersbersbers
Copy link
Contributor Author

@anntzer thanks, that's a neat idea that works locally for me. Updated the PR.

@bersbersbers
Copy link
Contributor Author

Woohoo, all green. That wasn't too bad, just had to add some functools.wraps as well for PyQt.

Will you require a test for the emitted warning as well, or is it enough for all existing functionality to remain working?

slot = getattr(self, callback)
# https://bugreports.qt.io/browse/PYSIDE-2512
slot = functools.wraps(slot)(functools.partial(slot))
slot = QtCore.Slot()(slot)
Copy link
Member

Choose a reason for hiding this comment

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

If this indeed portable this way, can we just decorate the methods directly? Even if we have to add a bunch of methods like

@QtCore.Slot
def foo(self):
    return super().foo()

that seems better than doing the multi-layered meta-programming here!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, can do. A considerable amount of meta-programming is still done in the loop just around the snippet, so maybe a test is needed to make sure that all functions in self.toolitems have been decorated...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wow, that's interesting. I tried adding

    @QtCore.Slot()
    def home(self, *args):
        return super().home(*args)

    @QtCore.Slot()
    def back(self, *args):
        return super().back(*args)

    @QtCore.Slot()
    def forward(self, *args):
        return super().forward(*args)

    @QtCore.Slot()
    def pan(self, *args):
        return super().pan(*args)

    @QtCore.Slot()
    def zoom(self, *args):
        return super().zoom(*args)

    @QtCore.Slot()
    def configure_subplots(self, *args):
        return super().configure_subplots(*args)

    @QtCore.Slot()
    def edit_parameters(self, *args):
        return super().edit_parameters(*args)

    @QtCore.Slot()
    def save_figure(self, *args):
        return super().save_figure(*args)

and this works for home, back and forward - but not for the others. Irrespective of the position inside NavigationToolbar2QT, and irrespective of the order of the functions. This is with PySide6-Essentials v6.6.0.

I put this code into https://github.com/bersbersbers/matplotlib-27214/commit/a3d97f8eb2b40a46fe2d4fc4cf60f9d631fd3cf2 if anyone wants to try it themselves.

Copy link
Member

Choose a reason for hiding this comment

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

My guess is multiple inheritance is related, but do not have a good theory as to why (other than general suspicion that MI always causes problems....).

Copy link
Contributor

Choose a reason for hiding this comment

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

If the metaprogramming approach works then let's just stick to it.

@QuLogic QuLogic merged commit f533687 into matplotlib:main Nov 18, 2023
42 checks passed
@QuLogic
Copy link
Member

QuLogic commented Nov 18, 2023

Thanks @bersbersbers! Congratulations on your first PR to Matplotlib 🎉 We hope to hear from you again.

@QuLogic QuLogic added this to the v3.9.0 milestone Nov 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

Successfully merging this pull request may close these issues.

[Bug]: NavigationToolbar2QT should use @Slot annotation
5 participants