Skip to content

Commit cdb830d

Browse files
committed
Don't go via six for callable(); related cleanups.
1 parent de5a4ee commit cdb830d

20 files changed

+55
-73
lines changed

lib/matplotlib/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def wrap(self, fmt, func, level='helpful', always=True):
333333
if always is True, the report will occur on every function
334334
call; otherwise only on the first time the function is called
335335
"""
336-
assert six.callable(func)
336+
assert callable(func)
337337

338338
def wrapper(*args, **kwargs):
339339
ret = func(*args, **kwargs)

lib/matplotlib/animation.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1290,14 +1290,14 @@ def __init__(self, fig, func, frames=None, init_func=None, fargs=None,
12901290
# will be treated as a number of frames.
12911291
if frames is None:
12921292
self._iter_gen = itertools.count
1293-
elif six.callable(frames):
1293+
elif callable(frames):
12941294
self._iter_gen = frames
12951295
elif iterable(frames):
12961296
self._iter_gen = lambda: iter(frames)
12971297
if hasattr(frames, '__len__'):
12981298
self.save_count = len(frames)
12991299
else:
1300-
self._iter_gen = lambda: xrange(frames).__iter__()
1300+
self._iter_gen = lambda: iter(xrange(frames))
13011301
self.save_count = frames
13021302

13031303
# If we're passed in and using the default, set it to 100.

lib/matplotlib/artist.py

+16-20
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def contains(self, mouseevent):
392392
selection, such as which points are contained in the pick radius. See
393393
individual artists for details.
394394
"""
395-
if six.callable(self._contains):
395+
if callable(self._contains):
396396
return self._contains(self, mouseevent)
397397
warnings.warn("'%s' needs 'contains' method" % self.__class__.__name__)
398398
return False, {}
@@ -435,7 +435,7 @@ def pick(self, mouseevent):
435435
# Pick self
436436
if self.pickable():
437437
picker = self.get_picker()
438-
if six.callable(picker):
438+
if picker is not None:
439439
inside, prop = picker(self, mouseevent)
440440
else:
441441
inside, prop = self.contains(mouseevent)
@@ -446,8 +446,8 @@ def pick(self, mouseevent):
446446
for a in self.get_children():
447447
# make sure the event happened in the same axes
448448
ax = getattr(a, 'axes', None)
449-
if mouseevent.inaxes is None or ax is None or \
450-
mouseevent.inaxes == ax:
449+
if (mouseevent.inaxes is None or ax is None
450+
or mouseevent.inaxes == ax):
451451
# we need to check if mouseevent.inaxes is None
452452
# because some objects associated with an axes (e.g., a
453453
# tick label) can be outside the bounding box of the
@@ -873,7 +873,7 @@ def _update_property(self, k, v):
873873
return setattr(self, k, v)
874874
else:
875875
func = getattr(self, 'set_' + k, None)
876-
if func is None or not six.callable(func):
876+
if not callable(func):
877877
raise AttributeError('Unknown property %s' % k)
878878
return func(v)
879879

@@ -1075,7 +1075,7 @@ def matchfunc(x):
10751075
elif cbook.issubclass_safe(match, Artist):
10761076
def matchfunc(x):
10771077
return isinstance(x, match)
1078-
elif six.callable(match):
1078+
elif callable(match):
10791079
matchfunc = match
10801080
else:
10811081
raise ValueError('match must be None, a matplotlib.artist.Artist '
@@ -1166,9 +1166,9 @@ def get_aliases(self):
11661166
}
11671167
11681168
"""
1169-
names = [name for name in dir(self.o) if
1170-
(name.startswith('set_') or name.startswith('get_'))
1171-
and six.callable(getattr(self.o, name))]
1169+
names = [name for name in dir(self.o)
1170+
if name.startswith(('set_', 'get_'))
1171+
and callable(getattr(self.o, name))]
11721172
aliases = {}
11731173
for name in names:
11741174
func = getattr(self.o, name)
@@ -1222,17 +1222,14 @@ def _get_setters_and_targets(self):
12221222
for name in dir(self.o):
12231223
if not name.startswith('set_'):
12241224
continue
1225-
o = getattr(self.o, name)
1226-
if not six.callable(o):
1225+
func = getattr(self.o, name)
1226+
if not callable(func):
12271227
continue
12281228
if six.PY2:
1229-
nargs = len(inspect.getargspec(o)[0])
1229+
nargs = len(inspect.getargspec(func)[0])
12301230
else:
1231-
nargs = len(inspect.getfullargspec(o)[0])
1232-
if nargs < 2:
1233-
continue
1234-
func = o
1235-
if self.is_alias(func):
1231+
nargs = len(inspect.getfullargspec(func)[0])
1232+
if nargs < 2 or self.is_alias(func):
12361233
continue
12371234
source_class = self.o.__module__ + "." + self.o.__name__
12381235
for cls in self.o.mro():
@@ -1371,8 +1368,7 @@ def properties(self):
13711368
"""
13721369
o = self.oorig
13731370
getters = [name for name in dir(o)
1374-
if name.startswith('get_')
1375-
and six.callable(getattr(o, name))]
1371+
if name.startswith('get_') and callable(getattr(o, name))]
13761372
getters.sort()
13771373
d = dict()
13781374
for name in getters:
@@ -1432,7 +1428,7 @@ def matchfunc(x):
14321428
elif issubclass(match, Artist):
14331429
def matchfunc(x):
14341430
return isinstance(x, match)
1435-
elif six.callable(match):
1431+
elif callable(match):
14361432
matchfunc = func
14371433
else:
14381434
raise ValueError('match must be None, a matplotlib.artist.Artist '

lib/matplotlib/axes/_axes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2664,7 +2664,7 @@ def get_next_color():
26642664
yt = y + pctdistance * radius * math.sin(thetam)
26652665
if is_string_like(autopct):
26662666
s = autopct % (100. * frac)
2667-
elif six.callable(autopct):
2667+
elif callable(autopct):
26682668
s = autopct(100. * frac)
26692669
else:
26702670
raise TypeError(

lib/matplotlib/axes/_base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3807,9 +3807,8 @@ def contains(self, mouseevent):
38073807
38083808
Returns *True* / *False*, {}
38093809
"""
3810-
if six.callable(self._contains):
3810+
if callable(self._contains):
38113811
return self._contains(self, mouseevent)
3812-
38133812
return self.patch.contains(mouseevent)
38143813

38153814
def contains_point(self, point):

lib/matplotlib/axis.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def contains(self, mouseevent):
209209
This function always returns false. It is more useful to test if the
210210
axis as a whole contains the mouse rather than the set of tick marks.
211211
"""
212-
if six.callable(self._contains):
212+
if callable(self._contains):
213213
return self._contains(self, mouseevent)
214214
return False, {}
215215

@@ -1712,7 +1712,7 @@ class XAxis(Axis):
17121712
def contains(self, mouseevent):
17131713
"""Test whether the mouse event occured in the x axis.
17141714
"""
1715-
if six.callable(self._contains):
1715+
if callable(self._contains):
17161716
return self._contains(self, mouseevent)
17171717

17181718
x, y = mouseevent.x, mouseevent.y
@@ -2040,7 +2040,7 @@ def contains(self, mouseevent):
20402040
20412041
Returns *True* | *False*
20422042
"""
2043-
if six.callable(self._contains):
2043+
if callable(self._contains):
20442044
return self._contains(self, mouseevent)
20452045

20462046
x, y = mouseevent.x, mouseevent.y

lib/matplotlib/cbook.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ def is_hashable(obj):
715715

716716
def is_writable_file_like(obj):
717717
"""return true if *obj* looks like a file object with a *write* method"""
718-
return hasattr(obj, 'write') and six.callable(obj.write)
718+
return callable(getattr(obj, 'write', None))
719719

720720

721721
def file_requires_unicode(x):

lib/matplotlib/cm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def revcmap(data):
3636
"""Can only handle specification *data* in dictionary format."""
3737
data_r = {}
3838
for key, val in six.iteritems(data):
39-
if six.callable(val):
39+
if callable(val):
4040
valnew = _reverser(val)
4141
# This doesn't work: lambda x: val(1-x)
4242
# The same "val" (the first one) is used

lib/matplotlib/collections.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def contains(self, mouseevent):
360360
Returns True | False, ``dict(ind=itemlist)``, where every
361361
item in itemlist contains the event.
362362
"""
363-
if six.callable(self._contains):
363+
if callable(self._contains):
364364
return self._contains(self, mouseevent)
365365

366366
if not self.get_visible():

lib/matplotlib/colors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def makeMappingArray(N, data, gamma=1.0):
351351
gives the closest value for values of x between 0 and 1.
352352
"""
353353

354-
if six.callable(data):
354+
if callable(data):
355355
xind = np.linspace(0, 1, N) ** gamma
356356
lut = np.clip(np.array(data(xind), dtype=float), 0, 1)
357357
return lut
@@ -733,7 +733,7 @@ def func_r(x):
733733

734734
data_r = dict()
735735
for key, data in six.iteritems(self._segmentdata):
736-
if six.callable(data):
736+
if callable(data):
737737
data_r[key] = factory(data)
738738
else:
739739
new_data = [(1.0 - x, y1, y0) for x, y0, y1 in reversed(data)]

lib/matplotlib/contour.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def get_text(self, lev, fmt):
338338
else:
339339
if isinstance(fmt, dict):
340340
return fmt[lev]
341-
elif six.callable(fmt):
341+
elif callable(fmt):
342342
return fmt(lev)
343343
else:
344344
return fmt % lev

lib/matplotlib/dates.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -683,18 +683,15 @@ def __init__(self, locator, tz=None, defaultfmt='%Y-%m-%d'):
683683

684684
def __call__(self, x, pos=None):
685685
locator_unit_scale = float(self._locator._get_unit())
686-
fmt = self.defaultfmt
687-
688686
# Pick the first scale which is greater than the locator unit.
689-
for possible_scale in sorted(self.scaled):
690-
if possible_scale >= locator_unit_scale:
691-
fmt = self.scaled[possible_scale]
692-
break
687+
fmt = next((fmt for scale, fmt in sorted(self.scaled.items())
688+
if scale >= locator_unit_scale),
689+
self.defaultfmt)
693690

694691
if isinstance(fmt, six.string_types):
695692
self._formatter = DateFormatter(fmt, self._tz)
696693
result = self._formatter(x, pos)
697-
elif six.callable(fmt):
694+
elif callable(fmt):
698695
result = fmt(x, pos)
699696
else:
700697
raise TypeError('Unexpected type passed to {0!r}.'.format(self))

lib/matplotlib/figure.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -496,13 +496,11 @@ def contains(self, mouseevent):
496496
"""
497497
Test whether the mouse event occurred on the figure.
498498
499-
Returns True,{}
499+
Returns True, {}.
500500
"""
501-
if six.callable(self._contains):
501+
if callable(self._contains):
502502
return self._contains(self, mouseevent)
503-
# inside = mouseevent.x >= 0 and mouseevent.y >= 0
504503
inside = self.bbox.contains(mouseevent.x, mouseevent.y)
505-
506504
return inside, {}
507505

508506
def get_window_extent(self, *args, **kwargs):

lib/matplotlib/image.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ def contains(self, mouseevent):
500500
"""
501501
Test whether the mouse event occured within the image.
502502
"""
503-
if six.callable(self._contains):
503+
if callable(self._contains):
504504
return self._contains(self, mouseevent)
505505
# TODO: make sure this is consistent with patch and patch
506506
# collection on nonlinear transformed coordinates.
@@ -514,8 +514,7 @@ def contains(self, mouseevent):
514514
ymin, ymax = ymax, ymin
515515

516516
if x is not None and y is not None:
517-
inside = ((x >= xmin) and (x <= xmax) and
518-
(y >= ymin) and (y <= ymax))
517+
inside = (xmin <= x <= xmax) and (ymin <= y <= ymax)
519518
else:
520519
inside = False
521520

@@ -1136,14 +1135,14 @@ def get_window_extent(self, renderer=None):
11361135

11371136
if isinstance(self.bbox, BboxBase):
11381137
return self.bbox
1139-
elif six.callable(self.bbox):
1138+
elif callable(self.bbox):
11401139
return self.bbox(renderer)
11411140
else:
11421141
raise ValueError("unknown type of bbox")
11431142

11441143
def contains(self, mouseevent):
11451144
"""Test whether the mouse event occured within the image."""
1146-
if six.callable(self._contains):
1145+
if callable(self._contains):
11471146
return self._contains(self, mouseevent)
11481147

11491148
if not self.get_visible(): # or self.get_figure()._renderer is None:

lib/matplotlib/lines.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def contains(self, mouseevent):
457457
458458
TODO: sort returned indices by distance
459459
"""
460-
if six.callable(self._contains):
460+
if callable(self._contains):
461461
return self._contains(self, mouseevent)
462462

463463
if not is_numlike(self.pickradius):
@@ -598,7 +598,7 @@ def set_picker(self, p):
598598
ACCEPTS: float distance in points or callable pick function
599599
``fn(artist, event)``
600600
"""
601-
if six.callable(p):
601+
if callable(p):
602602
self._contains = p
603603
else:
604604
self.pickradius = p

lib/matplotlib/offsetbox.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,9 @@ def get_offset(self, width, height, xdescent, ydescent, renderer):
217217
218218
accepts extent of the box
219219
"""
220-
if six.callable(self._offset):
221-
return self._offset(width, height, xdescent, ydescent, renderer)
222-
else:
223-
return self._offset
220+
return (self._offset(width, height, xdescent, ydescent, renderer)
221+
if callable(self._offset)
222+
else self._offset)
224223

225224
def set_width(self, width):
226225
"""

lib/matplotlib/patches.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def contains(self, mouseevent, radius=None):
171171
172172
Returns T/F, {}
173173
"""
174-
if six.callable(self._contains):
174+
if callable(self._contains):
175175
return self._contains(self, mouseevent)
176176
radius = self._process_radius(radius)
177177
inside = self.get_path().contains_point(
@@ -2520,9 +2520,7 @@ def set_boxstyle(self, boxstyle=None, **kw):
25202520
if boxstyle is None:
25212521
return BoxStyle.pprint_styles()
25222522

2523-
if isinstance(boxstyle, BoxStyle._Base):
2524-
self._bbox_transmuter = boxstyle
2525-
elif six.callable(boxstyle):
2523+
if isinstance(boxstyle, BoxStyle._Base) or callable(boxstyle):
25262524
self._bbox_transmuter = boxstyle
25272525
else:
25282526
self._bbox_transmuter = BoxStyle(boxstyle, **kw)
@@ -4137,10 +4135,8 @@ def set_connectionstyle(self, connectionstyle, **kw):
41374135
if connectionstyle is None:
41384136
return ConnectionStyle.pprint_styles()
41394137

4140-
if isinstance(connectionstyle, ConnectionStyle._Base):
4141-
self._connector = connectionstyle
4142-
elif six.callable(connectionstyle):
4143-
# we may need check the calling convention of the given function
4138+
if (isinstance(connectionstyle, ConnectionStyle._Base)
4139+
or callable(connectionstyle)):
41444140
self._connector = connectionstyle
41454141
else:
41464142
self._connector = ConnectionStyle(connectionstyle, **kw)

lib/matplotlib/patheffects.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def _update_gc(self, gc, new_gc_dict):
5454

5555
for k, v in six.iteritems(new_gc_dict):
5656
set_method = getattr(gc, 'set_' + k, None)
57-
if set_method is None or not six.callable(set_method):
57+
if not callable(set_method):
5858
raise AttributeError('Unknown property {0}'.format(k))
5959
set_method(v)
6060
return gc

lib/matplotlib/table.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ def contains(self, mouseevent):
342342
343343
Returns T/F, {}
344344
"""
345-
if six.callable(self._contains):
345+
if callable(self._contains):
346346
return self._contains(self, mouseevent)
347347

348348
# TODO: Return index of the cell containing the cursor so that the user

0 commit comments

Comments
 (0)