From 1f923d40d67d95a0d0aa3cdaea329ce9c4296d90 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 15 Feb 2018 03:01:25 +0100 Subject: [PATCH] Replace is_numlike by isinstance(..., numbers.Number). Since we bumped the min numpy version to 1.10, numpy scalars are now also instances of numbers.Number. --- .../2018-02-15-AL-deprecations.rst | 4 ++++ lib/matplotlib/axes/_axes.py | 3 ++- lib/matplotlib/cbook/__init__.py | 1 + lib/matplotlib/collections.py | 4 +++- lib/matplotlib/lines.py | 7 ++++--- lib/matplotlib/markers.py | 9 +++++---- lib/matplotlib/patches.py | 3 ++- lib/matplotlib/pyplot.py | 8 ++++---- lib/matplotlib/units.py | 10 +++++++--- lib/mpl_toolkits/axes_grid1/axes_grid.py | 7 ++++--- lib/mpl_toolkits/axes_grid1/axes_size.py | 16 ++++++++++++---- 11 files changed, 48 insertions(+), 24 deletions(-) create mode 100644 doc/api/next_api_changes/2018-02-15-AL-deprecations.rst diff --git a/doc/api/next_api_changes/2018-02-15-AL-deprecations.rst b/doc/api/next_api_changes/2018-02-15-AL-deprecations.rst new file mode 100644 index 000000000000..781a5fe85eab --- /dev/null +++ b/doc/api/next_api_changes/2018-02-15-AL-deprecations.rst @@ -0,0 +1,4 @@ +Deprecations +```````````` +``cbook.is_numlike`` is deprecated. Use ``isinstance(..., numbers.Number)`` +instead. diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index d6ad5453e645..dc3a722484e5 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -8,6 +8,7 @@ import itertools import logging import math +from numbers import Number import warnings import numpy as np @@ -6526,7 +6527,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None, m[:] = (m / db) / tops[-1].sum() if cumulative: slc = slice(None) - if cbook.is_numlike(cumulative) and cumulative < 0: + if isinstance(cumulative, Number) and cumulative < 0: slc = slice(None, None, -1) if density: diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index c90e42e9f641..02d71238094e 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -581,6 +581,7 @@ def is_scalar(obj): return not isinstance(obj, six.string_types) and not iterable(obj) +@deprecated('3.0', 'isinstance(..., numbers.Number)') def is_numlike(obj): """return true if *obj* looks like a number""" return isinstance(obj, (numbers.Number, np.number)) diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index 9e124cdf479d..21ec61905ce0 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -15,6 +15,8 @@ import six from six.moves import zip + +from numbers import Number try: from math import gcd except ImportError: @@ -370,7 +372,7 @@ def contains(self, mouseevent): pickradius = ( float(self._picker) - if cbook.is_numlike(self._picker) and + if isinstance(self._picker, Number) and self._picker is not True # the bool, not just nonzero or 1 else self._pickradius) diff --git a/lib/matplotlib/lines.py b/lib/matplotlib/lines.py index 984f5b025f4b..429fcac2cc24 100644 --- a/lib/matplotlib/lines.py +++ b/lib/matplotlib/lines.py @@ -9,6 +9,7 @@ import six +from numbers import Number import warnings import numpy as np @@ -16,7 +17,7 @@ from . import artist, cbook, colors as mcolors, docstring, rcParams from .artist import Artist, allow_rasterization from .cbook import ( - _to_unmasked_float_array, iterable, is_numlike, ls_mapper, ls_mapper_r, + _to_unmasked_float_array, iterable, ls_mapper, ls_mapper_r, STEP_LOOKUP_MAP) from .markers import MarkerStyle from .path import Path @@ -421,7 +422,7 @@ def __init__(self, xdata, ydata, self.update(kwargs) self.pickradius = pickradius self.ind_offset = 0 - if is_numlike(self._picker): + if isinstance(self._picker, Number): self.pickradius = self._picker self._xorig = np.asarray([]) @@ -456,7 +457,7 @@ def contains(self, mouseevent): if callable(self._contains): return self._contains(self, mouseevent) - if not is_numlike(self.pickradius): + if not isinstance(self.pickradius, Number): raise ValueError("pick radius should be a distance") # Make sure we have data to plot diff --git a/lib/matplotlib/markers.py b/lib/matplotlib/markers.py index ff27c4b253bf..d27cb1456b0c 100644 --- a/lib/matplotlib/markers.py +++ b/lib/matplotlib/markers.py @@ -90,11 +90,11 @@ from six.moves import xrange from collections import Sized +from numbers import Number import numpy as np -from . import rcParams -from .cbook import is_math_text, is_numlike +from . import cbook, rcParams from .path import Path from .transforms import IdentityTransform, Affine2D @@ -259,7 +259,8 @@ def set_marker(self, marker): marker in self.markers): self._marker_function = getattr( self, '_set_' + self.markers[marker]) - elif isinstance(marker, six.string_types) and is_math_text(marker): + elif (isinstance(marker, six.string_types) + and cbook.is_math_text(marker)): self._marker_function = self._set_mathtext_path elif isinstance(marker, Path): self._marker_function = self._set_path_marker @@ -309,7 +310,7 @@ def _set_vertices(self): def _set_tuple_marker(self): marker = self._marker - if is_numlike(marker[0]): + if isinstance(marker[0], Number): if len(marker) == 2: numsides, rotation = marker[0], 0.0 elif len(marker) == 3: diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index 1d66125561b1..a46d3a4d48e9 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -7,6 +7,7 @@ from six.moves import map, zip import math +from numbers import Number import warnings import numpy as np @@ -120,7 +121,7 @@ def get_verts(self): def _process_radius(self, radius): if radius is not None: return radius - if cbook.is_numlike(self._picker): + if isinstance(self._picker, Number): _radius = self._picker else: if self.get_edgecolor()[3] == 0: diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 296a05bede20..1a86ea4c6c01 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -22,6 +22,7 @@ import six +from numbers import Number import sys import time import warnings @@ -31,9 +32,8 @@ import matplotlib.colorbar from matplotlib import style from matplotlib import _pylab_helpers, interactive -from matplotlib.cbook import dedent, silent_list, is_numlike -from matplotlib.cbook import _string_to_bool -from matplotlib.cbook import deprecated, warn_deprecated +from matplotlib.cbook import ( + dedent, deprecated, silent_list, warn_deprecated, _string_to_bool) from matplotlib import docstring from matplotlib.backend_bases import FigureCanvasBase from matplotlib.figure import Figure, figaspect @@ -2421,7 +2421,7 @@ def getname_val(identifier): 'return the name and column data for identifier' if isinstance(identifier, six.string_types): return identifier, r[identifier] - elif is_numlike(identifier): + elif isinstance(identifier, Number): name = r.dtype.names[int(identifier)] return name, r[name] else: diff --git a/lib/matplotlib/units.py b/lib/matplotlib/units.py index b1140ded00b0..0df465430b46 100644 --- a/lib/matplotlib/units.py +++ b/lib/matplotlib/units.py @@ -46,9 +46,13 @@ def default_units(x, axis): import six -from matplotlib.cbook import iterable, is_numlike, safe_first_element + +from numbers import Number + import numpy as np +from matplotlib.cbook import iterable, safe_first_element + class AxisInfo(object): """ @@ -125,9 +129,9 @@ def is_numlike(x): """ if iterable(x): for thisx in x: - return is_numlike(thisx) + return isinstance(thisx, Number) else: - return is_numlike(x) + return isinstance(x, Number) class Registry(dict): diff --git a/lib/mpl_toolkits/axes_grid1/axes_grid.py b/lib/mpl_toolkits/axes_grid1/axes_grid.py index dde0e8dd7ccb..b6031e742d5d 100644 --- a/lib/mpl_toolkits/axes_grid1/axes_grid.py +++ b/lib/mpl_toolkits/axes_grid1/axes_grid.py @@ -3,8 +3,9 @@ import six +from numbers import Number + import matplotlib.axes as maxes -import matplotlib.cbook as cbook import matplotlib.ticker as ticker from matplotlib.gridspec import SubplotSpec @@ -208,7 +209,7 @@ def __init__(self, fig, h = [] v = [] - if isinstance(rect, six.string_types) or cbook.is_numlike(rect): + if isinstance(rect, (str, Number)): self._divider = SubplotDivider(fig, rect, horizontal=h, vertical=v, aspect=False) elif isinstance(rect, SubplotSpec): @@ -529,7 +530,7 @@ def __init__(self, fig, h = [] v = [] - if isinstance(rect, six.string_types) or cbook.is_numlike(rect): + if isinstance(rect, (str, Number)): self._divider = SubplotDivider(fig, rect, horizontal=h, vertical=v, aspect=aspect) elif isinstance(rect, SubplotSpec): diff --git a/lib/mpl_toolkits/axes_grid1/axes_size.py b/lib/mpl_toolkits/axes_grid1/axes_size.py index 163a6245fef0..0c91a2a9753b 100644 --- a/lib/mpl_toolkits/axes_grid1/axes_size.py +++ b/lib/mpl_toolkits/axes_grid1/axes_size.py @@ -15,9 +15,11 @@ class (or others) to determine the size of each axes. The unit import six -import matplotlib.cbook as cbook +from numbers import Number + from matplotlib.axes import Axes + class _Base(object): "Base class" @@ -44,6 +46,7 @@ def get_size(self, renderer): b_rel_size, b_abs_size = self._b.get_size(renderer) return a_rel_size + b_rel_size, a_abs_size + b_abs_size + class AddList(_Base): def __init__(self, add_list): self._list = add_list @@ -75,7 +78,8 @@ def get_size(self, renderer): abs_size = 0. return rel_size, abs_size -Scalable=Scaled +Scalable = Scaled + def _get_axes_aspect(ax): aspect = ax.get_aspect() @@ -89,6 +93,7 @@ def _get_axes_aspect(ax): return aspect + class AxesX(_Base): """ Scaled size whose relative part corresponds to the data width @@ -113,6 +118,7 @@ def get_size(self, renderer): abs_size = 0. return rel_size, abs_size + class AxesY(_Base): """ Scaled size whose relative part corresponds to the data height @@ -194,7 +200,6 @@ def get_size(self, renderer): return rel_size, abs_size - class MaxHeight(_Base): """ Size whose absolute part is the largest height of @@ -239,6 +244,7 @@ def get_size(self, renderer): abs_size = a*self._fraction return rel_size, abs_size + class Padded(_Base): """ Return a instance where the absolute part of *size* is @@ -254,6 +260,7 @@ def get_size(self, renderer): abs_size = a + self._pad return rel_size, abs_size + def from_any(size, fraction_ref=None): """ Creates Fixed unit when the first argument is a float, or a @@ -264,7 +271,7 @@ def from_any(size, fraction_ref=None): >>> Size.from_any("50%", a) # => Size.Fraction(0.5, a) """ - if cbook.is_numlike(size): + if isinstance(size, Number): return Fixed(size) elif isinstance(size, six.string_types): if size[-1] == "%": @@ -286,6 +293,7 @@ def get_size(self, renderer): return rel_size, abs_size + class GetExtentHelper(object): def _get_left(tight_bbox, axes_bbox): return axes_bbox.xmin - tight_bbox.xmin