Skip to content

Warn about unused kwargs in contour methods #7728

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 3 commits into from
Aug 14, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
First attempt at warning about unused kwargs
  • Loading branch information
dstansby committed Aug 10, 2017
commit 96f28243ecd163ff7ce4736dce951b6b62dcf21a
1 change: 1 addition & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
66 changes: 39 additions & 27 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,32 +800,32 @@ 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.
# The default for line contours will be taken from
# 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
Expand All @@ -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:
Expand Down Expand Up @@ -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,),
Expand All @@ -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,
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could just be ", ".join(map(repr, kwargs)).


def get_transform(self):
"""
Return the :class:`~matplotlib.transforms.Transform`
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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']

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, it's passed as a mutable value, so you don't technically need to return it. It should be the same object coming out as going in.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest renaming the method to _pop_and_process_unit_info (_pop_and_apply_unit_info to save two characters) for clarity -- sneakily modifying a mutable argument usually leads to a lot of head-scratching.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am 👎 on renaming it because it has had the current name for a long time and has always probably had this pop behavior.

In any case that change is outside of the scope if this PR.

x = self.ax.convert_xunits(x)
y = self.ax.convert_yunits(y)

Expand Down