From 06081dcb07a4c92e2a7e3e51d1ab29e759e221e9 Mon Sep 17 00:00:00 2001 From: Thomas Hisch Date: Thu, 27 Dec 2018 18:33:24 +0100 Subject: [PATCH] Simplify callable(self._contains) checks Artist._contains can either be None (not-set) or a callable (see docstring of Artist.set_contains). Therefore, the callable(self._contains) checks can be simplified to self._contains is not None. --- lib/matplotlib/artist.py | 4 +++- lib/matplotlib/axes/_base.py | 2 +- lib/matplotlib/axis.py | 6 +++--- lib/matplotlib/collections.py | 2 +- lib/matplotlib/figure.py | 2 +- lib/matplotlib/image.py | 4 ++-- lib/matplotlib/patches.py | 2 +- lib/matplotlib/table.py | 2 +- lib/matplotlib/text.py | 2 +- 9 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/artist.py b/lib/matplotlib/artist.py index c1e7c7a67c76..93a91a67538b 100644 --- a/lib/matplotlib/artist.py +++ b/lib/matplotlib/artist.py @@ -383,7 +383,7 @@ def contains(self, mouseevent): -------- set_contains, get_contains """ - if callable(self._contains): + if self._contains is not None: return self._contains(self, mouseevent) _log.warning("%r needs 'contains' method", self.__class__.__name__) return False, {} @@ -411,6 +411,8 @@ def contains(artist: Artist, event: MouseEvent) -> bool, dict implementation of the respective artist, but may provide additional information. """ + if not callable(picker): + raise TypeError("picker is not a callable") self._contains = picker def get_contains(self): diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 8287983a3039..5786cc6c1d46 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -4229,7 +4229,7 @@ def get_children(self): def contains(self, mouseevent): # docstring inherited. - if callable(self._contains): + if self._contains is not None: return self._contains(self, mouseevent) return self.patch.contains(mouseevent) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index e33e55a2ee5a..93472c729223 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -246,7 +246,7 @@ def contains(self, mouseevent): This function always returns false. It is more useful to test if the axis as a whole contains the mouse rather than the set of tick marks. """ - if callable(self._contains): + if self._contains is not None: return self._contains(self, mouseevent) return False, {} @@ -1858,7 +1858,7 @@ class XAxis(Axis): def contains(self, mouseevent): """Test whether the mouse event occurred in the x axis. """ - if callable(self._contains): + if self._contains is not None: return self._contains(self, mouseevent) x, y = mouseevent.x, mouseevent.y @@ -2202,7 +2202,7 @@ def contains(self, mouseevent): Returns *True* | *False* """ - if callable(self._contains): + if self._contains is not None: return self._contains(self, mouseevent) x, y = mouseevent.x, mouseevent.y diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index a58d60e19d65..9b9bb068821b 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -352,7 +352,7 @@ def contains(self, mouseevent): Returns ``bool, dict(ind=itemlist)``, where every item in itemlist contains the event. """ - if callable(self._contains): + if self._contains is not None: return self._contains(self, mouseevent) if not self.get_visible(): diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index f86c0c78936c..ddd4d7eec4f1 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -656,7 +656,7 @@ def contains(self, mouseevent): ------- bool, {} """ - if callable(self._contains): + if self._contains is not None: return self._contains(self, mouseevent) inside = self.bbox.contains(mouseevent.x, mouseevent.y) return inside, {} diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index f1970a356536..2ec7eee423ec 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -613,7 +613,7 @@ def contains(self, mouseevent): """ Test whether the mouse event occurred within the image. """ - if callable(self._contains): + if self._contains is not None: return self._contains(self, mouseevent) # TODO: make sure this is consistent with patch and patch # collection on nonlinear transformed coordinates. @@ -1310,7 +1310,7 @@ def get_window_extent(self, renderer=None): def contains(self, mouseevent): """Test whether the mouse event occurred within the image.""" - if callable(self._contains): + if self._contains is not None: return self._contains(self, mouseevent) if not self.get_visible(): # or self.get_figure()._renderer is None: diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index 0a025ed2f96a..7d7945efe118 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -127,7 +127,7 @@ def contains(self, mouseevent, radius=None): Returns T/F, {} """ - if callable(self._contains): + if self._contains is not None: return self._contains(self, mouseevent) radius = self._process_radius(radius) inside = self.get_path().contains_point( diff --git a/lib/matplotlib/table.py b/lib/matplotlib/table.py index 33827fc726fe..55eadc51b0c1 100644 --- a/lib/matplotlib/table.py +++ b/lib/matplotlib/table.py @@ -438,7 +438,7 @@ def _get_grid_bbox(self, renderer): def contains(self, mouseevent): # docstring inherited - if callable(self._contains): + if self._contains is not None: return self._contains(self, mouseevent) # TODO: Return index of the cell containing the cursor so that the user diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 6f036c5ae6e8..5fbe603d84c8 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -193,7 +193,7 @@ def contains(self, mouseevent): ------- bool : bool """ - if callable(self._contains): + if self._contains is not None: return self._contains(self, mouseevent) if not self.get_visible() or self._renderer is None: