Repository navigation
Use CallbackRegistry in Widgets and some related cleanup - #18226
Conversation
|
It also needs to be mentioned in the API change note as if you register a method into it currently we will keep a hard-ref that keeps that object alive, the CallbackRegistry will only keep a weakref and if you otherwise let the object get gc'd your callback will go away. I don't think we need a deprecation path on that. On the other hand, to channel @anntzer he will say we should always keep a hard-ref and keep objects alive (I am skeptical of this). |
|
I added an API change note. |
|
(I'm not going to fight over this, at least being consistent within the library is good too.) |
| The `.widgets.Widget` classes that allow event observers now use a | ||
| `.CallbackRegistry` to save callbacks. This provides consistency with canvas | ||
| event callbacks, and fixes some bugs that are already handled there. It also | ||
| means that callbacks are now stored as weak references, which means you must |
There was a problem hiding this comment.
This only applies to methods, we keep hard-refs to everything else.
| Widget class internals | ||
| ~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| Several `.widgets.Widget` class internals have been privatized and deprecated: |
There was a problem hiding this comment.
Do we think that there was anyone directly adding things to the observers dictionary?
There was a problem hiding this comment.
Then they should have updated cnt so that followup additions would have the right number. I would expect them to get an error from that, and hopefully figure it out.
| @cbook.deprecated("3.4") | ||
| @property | ||
| def cnt(self): | ||
| # Not real, but close enough. |
There was a problem hiding this comment.
Widget.cnt is roughly the next cid to be returned, but this is just the count of the callbacks added. I can't get the next cid without reaching into CallbackRegistry internals (and maybe not even then since it uses itertools.count().)
This simplifies processing, fixes memory leaks with weak references, and fixes bugs like being unable to disconnect a callback in its own code.
|
Actually I missed two points here:
May be nice to decide on this before 3.4 gets released @QuLogic @tacaswell @timhoffm |
PR Summary
This came about from this Discourse post: https://discourse.matplotlib.org/t/have-a-button-disconnect-its-own-callback/21465
Using a
CallbackRegistrysimplifies processing, fixes memory leaks with weak references, and fixes bugs like being unable to disconnect a callback in its own code, as in the above post.I also cleaned up some conditions and deprecated the old callback-related attributes.
PR Checklist