Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Closed
Milestone
Description
Bug summary
I've got a classCharts
that usesself.fig.canvas.mpl_connect('key_press_event', self.sendEvent)
to handle keypresses, which works fine. But recently I tried changing my class to subclassdict
, and – oddly – this seems to confuse Matplotlib. I get the error:
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/helipad/visualize.py", line 72, in launch self.fig.canvas.mpl_connect('key_press_event', self.sendEvent) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/backend_bases.py", line 2476, in mpl_connect return self.callbacks.connect(s, func) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/cbook/__init__.py", line 228, in connect if proxy in self._func_cid_map[signal]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^TypeError: unhashable type: 'Charts'
Which is weird because I'm not passingCharts
at all.
Looking at MPL's code, it looks like the_weak_or_strong_ref
function is getting a reference toCharts
back out of the method I passed as a callback, putting it in theproxy
variable, and hashing it, which subclassingdict
prevents it from doing.
Code for reproduction
importmatplotlib.pyplotaspltclassThisWorks:def__init__(self):self.fig,plots=plt.subplots(1)self.fig.canvas.mpl_connect('key_press_event',self.sendEvent)self.keyListeners= {}defsendEvent(self,event):ifevent.name=='key_press_event'andevent.keyinself.keyListeners:forfinself.keyListeners[event.key]:f(self.model,event)classThisDoesntWork(ThisWorks,dict):passx=ThisDoesntWork()
Actual outcome
Traceback (most recent call last): File "/Users/charwick/Desktop/example.py", line 15, in <module> x = ThisDoesntWork() ^^^^^^^^^^^^^^^^ File "example.py", line 6, in __init__ self.fig.canvas.mpl_connect('key_press_event', self.sendEvent) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/backend_bases.py", line 2476, in mpl_connect return self.callbacks.connect(s, func) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/cbook/__init__.py", line 227, in connect if proxy in self._func_cid_map[signal]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^TypeError: unhashable type: 'ThisDoesntWork'
Expected outcome
Code above doesn't actually do anything, but it shouldn't generate an error.
Additional information
No response
Operating system
MacOS 13.3.1
Matplotlib Version
3.7.1
Matplotlib Backend
No response
Python version
3.11.3
Jupyter version
No response
Installation
pip
Metadata
Metadata
Assignees
Labels
No labels