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

Skip to content

solution: All subclasses of LocationEvent could be used in cbook.callbacks before being fully initialized - issue 15139 #16948

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 5 commits into from
Jun 9, 2020

Conversation

Stefan-Mitic
Copy link
Contributor

PR Summary

Modified the call to LocationEvent.init() to be after MouseButton initialization in MouseEvent.init(). The reason for this is the error occurs because callback.process is invoked before the initialization.

MouseEvent is a subclass of LocationEvent, so it is safe to say that motion_notify_event() can be called by a child class of LocationEvent.

Issue Ref: #15139

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@tacaswell tacaswell added this to the v3.2.2 milestone Mar 30, 2020
@tacaswell
Copy link
Member

@anntzer I don't think this collides with #16931 (which will fix the same bug by not doing the processing in __init__ anymore). I think it is worth merging and backporting this fix.

Thanks for this @Stefan-Mitic ! It looks like there are a couple of un-related commits on this branch. Could you please remove them? See https://matplotlib.org/devel/gitwash/development_workflow.html?highlight=rebase#rewriting-commit-history for instructions. Remember that when you push the re-written branch to github to not do what it suggests about merging and instead use push --force-with-lease.

@anntzer
Copy link
Contributor

anntzer commented Mar 30, 2020

I can handle the rebase, sure.

@Stefan-Mitic
Copy link
Contributor Author

@anntzer I don't think this collides with #16931 (which will fix the same bug by not doing the processing in __init__ anymore). I think it is worth merging and backporting this fix.

Thanks for this @Stefan-Mitic ! It looks like there are a couple of un-related commits on this branch. Could you please remove them? See https://matplotlib.org/devel/gitwash/development_workflow.html?highlight=rebase#rewriting-commit-history for instructions. Remember that when you push the re-written branch to github to not do what it suggests about merging and instead use push --force-with-lease.

Thanks for the help @tacaswell

@Stefan-Mitic Stefan-Mitic requested a review from tacaswell April 2, 2020 17:58
@QuLogic
Copy link
Member

QuLogic commented Apr 3, 2020

So if I read #15139 correctly, the same thing should be done for KeyEvent?

@Stefan-Mitic
Copy link
Contributor Author

Yes @QuLogic, I added it to KeyEvent. Both KeyEvent and MouseEvent are LocationEvents, I think it is fine to pass those through the callback function.

@QuLogic
Copy link
Member

QuLogic commented Apr 3, 2020

Can you add tests? I think it should work in lib/matplotlib/tests/test_backend_bases.py.

Copy link
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.

In both cases, there should be a comment why the super-init call is deferred to the end. Usually it's in the first line and there's a danger that people will move it back later, or at least are confused why it's at the end.

@Stefan-Mitic
Copy link
Contributor Author

Stefan-Mitic commented Apr 4, 2020

@QuLogic, I'm not sure how to trigger a keypress event to invoke my callback function.
This is the code I'm trying to work with:

def test_key_event_init():
    fig, ax = plt.subplots()
    canvas = FigureCanvasBase(fig)

    def on_key_press(event):
        assert event.key == "test_key"

    canvas.mpl_connect('key_press_event', on_key_press)
    # TODO: trigger key_press_event here

@Stefan-Mitic Stefan-Mitic requested a review from timhoffm April 4, 2020 23:26
Co-authored-by: Tim Hoffmann <[email protected]>
@tacaswell
Copy link
Member

Given this explanation, I'm not 100% sure that this change is the correct solution. init calling other methods that rely on a fully set-up object can be problematic (as this issue illustrates).

I don't disagree, but we have the event system we have. With super() you tend to end up is cases where either you have to do your work in the subclass first and then push it up the chain or push it up the chain and then extend it on the way out. Conceptually either works ("sub classes can affect the parent code by setting state" vs "subclasses can strictly extend the base functionality") but woe unto those who end up in code bases that do some of one and some of the other....

@QuLogic QuLogic merged commit 43a4c2b into matplotlib:master Jun 9, 2020
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Jun 9, 2020
…nt could be used in cbook.callbacks before being fully initialized - issue 15139
tacaswell added a commit that referenced this pull request Jun 10, 2020
…948-on-v3.2.x

Backport PR #16948 on branch v3.2.x (solution: All subclasses of LocationEvent could be used in cbook.callbacks before being fully initialized - issue 15139)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants