From ae46b1195f742e5069f272b929c5dadc7009f290 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Mon, 28 Dec 2020 00:40:27 +0100 Subject: [PATCH] Fix Artist.remove_callback() Regression was introduced in #18912. --- lib/matplotlib/artist.py | 2 +- lib/matplotlib/tests/test_artist.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/artist.py b/lib/matplotlib/artist.py index 0a238267b20d..4f2ff33a18bd 100644 --- a/lib/matplotlib/artist.py +++ b/lib/matplotlib/artist.py @@ -351,7 +351,7 @@ def remove_callback(self, oid): -------- add_callback """ - self._callbacks.disconnect("pchanged", oid) + self._callbacks.disconnect(oid) def pchanged(self): """ diff --git a/lib/matplotlib/tests/test_artist.py b/lib/matplotlib/tests/test_artist.py index b75997daf97b..ed368288622d 100644 --- a/lib/matplotlib/tests/test_artist.py +++ b/lib/matplotlib/tests/test_artist.py @@ -303,3 +303,21 @@ def test_set_alpha_for_array(): art._set_alpha_for_array([0.5, 1.1]) with pytest.raises(ValueError, match="alpha must be between 0 and 1"): art._set_alpha_for_array([0.5, np.nan]) + + +def test_callbacks(): + def func(artist): + func.counter += 1 + + func.counter = 0 + + art = martist.Artist() + oid = art.add_callback(func) + assert func.counter == 0 + art.pchanged() # must call the callback + assert func.counter == 1 + art.set_zorder(10) # setting a property must also call the callback + assert func.counter == 2 + art.remove_callback(oid) + art.pchanged() # must not call the callback anymore + assert func.counter == 2