Skip to content

Replace is_numlike by isinstance(..., numbers.Number). #10468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/api/next_api_changes/2018-02-15-AL-deprecations.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Deprecations
````````````
``cbook.is_numlike`` is deprecated. Use ``isinstance(..., numbers.Number)``
instead.
3 changes: 2 additions & 1 deletion lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import itertools
import logging
import math
from numbers import Number
import warnings

import numpy as np
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import six
from six.moves import zip

from numbers import Number
try:
from math import gcd
except ImportError:
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 4 additions & 3 deletions lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

import six

from numbers import Number
import warnings

import numpy as np

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
Expand Down Expand Up @@ -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([])
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions lib/matplotlib/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from six.moves import map, zip

import math
from numbers import Number
import warnings

import numpy as np
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import six

from numbers import Number
import sys
import time
import warnings
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions lib/matplotlib/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 4 additions & 3 deletions lib/mpl_toolkits/axes_grid1/axes_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
16 changes: 12 additions & 4 deletions lib/mpl_toolkits/axes_grid1/axes_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -89,6 +93,7 @@ def _get_axes_aspect(ax):

return aspect


class AxesX(_Base):
"""
Scaled size whose relative part corresponds to the data width
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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] == "%":
Expand All @@ -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
Expand Down