From 96f28243ecd163ff7ce4736dce951b6b62dcf21a Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 2 Jan 2017 19:52:58 +0000 Subject: [PATCH 1/3] First attempt at warning about unused kwargs --- lib/matplotlib/axes/_base.py | 1 + lib/matplotlib/contour.py | 66 +++++++++++++++++++++--------------- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 1697e91e0862..a26c263ccffb 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1986,6 +1986,7 @@ def _process_unit_info(self, xdata=None, ydata=None, kwargs=None): # we need to update. if ydata is not None: self.yaxis.update_units(ydata) + return kwargs def in_axes(self, mouseevent): """ diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index 1b27906e9ef9..ca231090f69f 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -800,23 +800,23 @@ def __init__(self, ax, *args, **kwargs): :attr:`matplotlib.contour.QuadContourSet.contour_doc`. """ self.ax = ax - self.levels = kwargs.get('levels', None) - self.filled = kwargs.get('filled', False) - self.linewidths = kwargs.get('linewidths', None) - self.linestyles = kwargs.get('linestyles', None) - - self.hatches = kwargs.get('hatches', [None]) - - self.alpha = kwargs.get('alpha', None) - self.origin = kwargs.get('origin', None) - self.extent = kwargs.get('extent', None) - cmap = kwargs.get('cmap', None) - self.colors = kwargs.get('colors', None) - norm = kwargs.get('norm', None) - vmin = kwargs.get('vmin', None) - vmax = kwargs.get('vmax', None) - self.extend = kwargs.get('extend', 'neither') - self.antialiased = kwargs.get('antialiased', None) + self.levels = kwargs.pop('levels', None) + self.filled = kwargs.pop('filled', False) + self.linewidths = kwargs.pop('linewidths', None) + self.linestyles = kwargs.pop('linestyles', None) + + self.hatches = kwargs.pop('hatches', [None]) + + self.alpha = kwargs.pop('alpha', None) + self.origin = kwargs.pop('origin', None) + self.extent = kwargs.pop('extent', None) + cmap = kwargs.pop('cmap', None) + self.colors = kwargs.pop('colors', None) + norm = kwargs.pop('norm', None) + vmin = kwargs.pop('vmin', None) + vmax = kwargs.pop('vmax', None) + self.extend = kwargs.pop('extend', 'neither') + self.antialiased = kwargs.pop('antialiased', None) if self.antialiased is None and self.filled: self.antialiased = False # eliminate artifacts; we are not # stroking the boundaries. @@ -824,8 +824,8 @@ def __init__(self, ax, *args, **kwargs): # the LineCollection default, which uses the # rcParams['lines.antialiased'] - self.nchunk = kwargs.get('nchunk', 0) - self.locator = kwargs.get('locator', None) + self.nchunk = kwargs.pop('nchunk', 0) + self.locator = kwargs.pop('locator', None) if (isinstance(norm, colors.LogNorm) or isinstance(self.locator, ticker.LogLocator)): self.logscale = True @@ -848,9 +848,9 @@ def __init__(self, ax, *args, **kwargs): if self.origin == 'image': self.origin = mpl.rcParams['image.origin'] - self._transform = kwargs.get('transform', None) + self._transform = kwargs.pop('transform', None) - self._process_args(*args, **kwargs) + kwargs = self._process_args(*args, **kwargs) self._process_levels() if self.colors is not None: @@ -915,11 +915,12 @@ def __init__(self, ax, *args, **kwargs): if self.allkinds is None: self.allkinds = [None] * len(self.allsegs) + # Default zorder taken from Collection + zorder = kwargs.pop('zorder', 1) for level, level_upper, segs, kinds in \ zip(lowers, uppers, self.allsegs, self.allkinds): paths = self._make_paths(segs, kinds) - # Default zorder taken from Collection - zorder = kwargs.get('zorder', 1) + col = mcoll.PathCollection( paths, antialiaseds=(self.antialiased,), @@ -936,10 +937,10 @@ def __init__(self, ax, *args, **kwargs): aa = self.antialiased if aa is not None: aa = (self.antialiased,) + # Default zorder taken from LineCollection + zorder = kwargs.pop('zorder', 2) for level, width, lstyle, segs in \ zip(self.levels, tlinewidths, tlinestyles, self.allsegs): - # Default zorder taken from LineCollection - zorder = kwargs.get('zorder', 2) col = mcoll.LineCollection( segs, antialiaseds=aa, @@ -960,6 +961,13 @@ def __init__(self, ax, *args, **kwargs): self.changed() # set the colors + if kwargs: + s = '' + for key, value in kwargs.items(): + s += '"' + str(key) + '"' + ', ' + warnings.warn('The following kwargs were not used by contour: ' + + s) + def get_transform(self): """ Return the :class:`~matplotlib.transforms.Transform` @@ -1068,6 +1076,8 @@ def _process_args(self, *args, **kwargs): self._mins = points.min(axis=0) self._maxs = points.max(axis=0) + return kwargs + def _get_allsegs_and_allkinds(self): """ Override in derived classes to create and return allsegs and allkinds. @@ -1431,7 +1441,7 @@ def _process_args(self, *args, **kwargs): self._mins = args[0]._mins self._maxs = args[0]._maxs else: - self._corner_mask = kwargs.get('corner_mask', None) + self._corner_mask = kwargs.pop('corner_mask', None) if self._corner_mask is None: self._corner_mask = mpl.rcParams['contour.corner_mask'] @@ -1470,6 +1480,8 @@ def _process_args(self, *args, **kwargs): else: self._contour_generator = contour_generator + return kwargs + def _get_allsegs_and_allkinds(self): """ Create and return allsegs and allkinds by calling underlying C code. @@ -1539,7 +1551,7 @@ def _check_xyz(self, args, kwargs): Exception class (here and elsewhere). """ x, y = args[:2] - self.ax._process_unit_info(xdata=x, ydata=y, kwargs=kwargs) + kwargs = self.ax._process_unit_info(xdata=x, ydata=y, kwargs=kwargs) x = self.ax.convert_xunits(x) y = self.ax.convert_yunits(y) From cc46285bf4f916879a8b205adc0b9b4ae4598bcf Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 2 Jan 2017 21:24:32 +0000 Subject: [PATCH 2/3] Add one more missing kwarg return --- lib/matplotlib/tri/tricontour.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/tri/tricontour.py b/lib/matplotlib/tri/tricontour.py index c1727b7fbaa9..ce113544eb08 100644 --- a/lib/matplotlib/tri/tricontour.py +++ b/lib/matplotlib/tri/tricontour.py @@ -54,6 +54,7 @@ def _process_args(self, *args, **kwargs): self._maxs = [tri.x.max(), tri.y.max()] self.cppContourGenerator = C + return kwargs def _get_allsegs_and_allkinds(self): """ From 4481344d493740e0892e82ff1e26c0ad2d66ceb9 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Tue, 23 May 2017 18:37:47 +0100 Subject: [PATCH 3/3] Simplify warning message generation --- lib/matplotlib/contour.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index ca231090f69f..a435cf467031 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -962,9 +962,7 @@ def __init__(self, ax, *args, **kwargs): self.changed() # set the colors if kwargs: - s = '' - for key, value in kwargs.items(): - s += '"' + str(key) + '"' + ', ' + s = ", ".join(map(repr, kwargs)) warnings.warn('The following kwargs were not used by contour: ' + s)