8000 Use more kwonly arguments, less manual kwargs-popping. · matplotlib/matplotlib@6ada6c4 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6ada6c4

Browse files
committed
Use more kwonly arguments, less manual kwargs-popping.
1 parent fc413b3 commit 6ada6c4

File tree

9 files changed

+48
-71
lines changed

9 files changed

+48
-71
lines changed

lib/matplotlib/artist.py

Lines changed: 6 additions & 10 deletions
from numbers import Number
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55
66
import re
7+
import sys
78
import warnings
89

910
import numpy as np
@@ -1527,7 +1528,7 @@ def getp(obj, property=None):
15271528
get = getp
15281529

15291530

1530-
def setp(obj, *args, **kwargs):
1531+
def setp(obj, *args, file=sys.stdout, **kwargs):
15311532
"""
15321533
Set a property on an artist object.
15331534
@@ -1551,8 +1552,8 @@ def setp(obj, *args, **kwargs):
15511552
>>> setp(line)
15521553
... long output listing omitted
15531554
1554-
You may specify another output file to `setp` if `sys.stdout` is not
1555-
acceptable for some reason using the *file* keyword-only argument::
1555+
By default `setp` prints to `sys.stdout`, but this can be modified using
1556+
the *file* keyword-only argument::
15561557
15571558
>>> with fopen('output.log') as f:
15581559
>>> setp(line, file=f)
@@ -1587,16 +1588,11 @@ def setp(obj, *args, **kwargs):
15871588

15881589
insp = ArtistInspector(objs[0])
15891590

1590-
# file has to be popped before checking if kwargs is empty
1591-
printArgs = {}
1592-
if 'file' in kwargs:
1593-
printArgs['file'] = kwargs.pop('file')
1594-
15951591
if not kwargs and len(args) < 2:
15961592
if args:
1597-
print(insp.pprint_setters(prop=args[0]), **printArgs)
1593+
print(insp.pprint_setters(prop=args[0]), file=file)
15981594
else:
1599-
print('\n'.join(insp.pprint_setters()), **printArgs)
1595+
print('\n'.join(insp.pprint_setters()), file=file)
16001596
return
16011597

16021598
if len(args) % 2:

lib/matplotlib/axes/_axes.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ def legend(self, *args, **kwargs):
404404
def _remove_legend(self, legend):
405405
self.legend_ = None
406406

407-
def inset_axes(self, bounds, *, transform=None, zorder=5,
408-
**kwargs):
407+
def inset_axes(self, bounds, *,
408+
transform=None, zorder=5, label='inset_axes', **kwargs):
409409
"""
410410
Add a child inset axes to this existing axes.
411411
@@ -449,14 +449,13 @@ def inset_axes(self, bounds, *, transform=None, zorder=5,
449449
"""
450450
if transform is None:
451451
transform = self.transAxes
452-
label = kwargs.pop('label', 'inset_axes')
453452

454453
# This puts the rectangle into figure-relative coordinates.
455454
inset_locator = _make_inset_locator(bounds, transform, self)
456455
bb = inset_locator(None, None)
457456

458-
inset_ax = Axes(self.figure, bb.bounds, zorder=zorder,
459-
label=label, **kwargs)
457+
inset_ax = Axes(self.figure, bb.bounds, zorder=zorder, label=label,
458+
**kwargs)
460459

461460
# this locator lets the axes move if in data coordinates.
462461
# it gets called in `ax.apply_aspect() (of all places)
@@ -468,7 +467,7 @@ def inset_axes(self, bounds, *, transform=None, zorder=5,
468467

469468
def indicate_inset(self, bounds, inset_ax=None, *, transform=None,
470469
facecolor='none', edgecolor='0.5', alpha=0.5,
471-
zorder=4.99, **kwargs):
470+
zorder=4.99, label='indicate_inset', **kwargs):
472471
"""
473472
Add an inset indicator to the axes. This is a rectangle on the plot
474473
at the position indicated by *bounds* that optionally has lines that
@@ -530,13 +529,12 @@ def indicate_inset(self, bounds, inset_ax=None, *, transform=None,
530529

531530
if transform is None:
532531
transform = self.transData
533-
label = kwargs.pop('label', 'indicate_inset')
534532

535533
x, y, width, height = bounds
536534
rectangle_patch = mpatches.Rectangle(
537535
(x, y), width, height,
538536
facecolor=facecolor, edgecolor=edgecolor, alpha=alpha,
539-
zorder=zorder, label=label, transform=transform, **kwargs)
537+
zorder=zorder, label=label, transform=transform, **kwargs)
540538
self.add_patch(rectangle_patch)
541539

542540
connects = []

lib/matplotlib/axes/_base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def set_prop_cycle(self, *args, **kwargs):
150150
# This should make a copy
151151
self._prop_keys = prop_cycler.keys
152152 10000

153-
def __call__(self, *args, **kwargs):
153+
def __call__(self, *args, data=None, **kwargs):
154154
self.axes._process_unit_info(kwargs=kwargs)
155155

156156
for pos_only in "xy":
@@ -161,9 +161,7 @@ def __call__(self, *args, **kwargs):
161161
if not args:
162162
return
163163

164-
# Process the 'data' kwarg.
165-
data = kwargs.pop("data", None)
166-
if data is not None:
164+
if data is not None: # Process the 'data' kwarg.
167165
replaced = [mpl._replacer(data, arg) for arg in args]
168166
if len(args) == 1:
169167
label_namer_idx = 0
@@ -385,6 +383,8 @@ def __init__(self, fig, rect,
385383
xscale=None,
386384
yscale=None,
387385
box_aspect=None,
386+
*,
387+
axes_locator=None,
388388
**kwargs
389389
):
390390
"""
@@ -444,7 +444,7 @@ def __init__(self, fig, rect,
444444
self.set_label(label)
445445
self.set_figure(fig)
446446
self.set_box_aspect(box_aspect)
447-
self.set_axes_locator(kwargs.get("axes_locator", None))
447+
self.set_axes_locator(axes_locator)
448448

449449
self.spines = self._gen_axes_spines()
450450

lib/matplotlib/backend_bases.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,9 +1987,11 @@ def _get_output_canvas(self, backend, fmt):
19871987
"Format {!r} is not supported (supported formats: {})"
19881988
.format(fmt, ", ".join(sorted(self.get_supported_filetypes()))))
19891989

1990-
def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
1991-
orientation='portrait', format=None,
1992-
*, bbox_inches=None, backend=None, **kwargs):
1990+
def print_figure(
1991+
self, filename, dpi=None, facecolor=None, edgecolor=None,
1992+
orientation='portrait', format=None, *,
1993+
bbox_inches=None, pad_inches=None, bbox_extra_artists=None,
1994+
backend=None, **kwargs):
19931995
"""
19941996
Render the figure to hardcopy. Set the figure patch face and edge
19951997
colors. This is useful because some of the GUIs have a gray figure
@@ -2086,14 +2088,11 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
20862088
functools.partial(
20872089
print_method, dpi=dpi, orientation=orientation))
20882090
self.figure.draw(renderer)
2089-
bbox_artists = kwargs.pop("bbox_extra_artists", None)
2090-
bbox_inches = self.figure.get_tightbbox(renderer,
2091-
bbox_extra_artists=bbox_artists)
2092-
pad = kwargs.pop("pad_inches", None)
2093-
if pad is None:
2094-
pad = rcParams['savefig.pad_inches']
2095-
2096-
bbox_inches = bbox_inches.padded(pad)
2091+
bbox_inches = self.figure.get_tightbbox(
2092+
renderer, bbox_extra_artists=bbox_extra_artists)
2093+
if pad_inches is None:
2094+
pad_inches = rcParams['savefig.pad_inches']
2095+
bbox_inches = bbox_inches.padded(pad_inches)
20972096

20982097
# call adjust_bbox to save only the given area
20992098
restore_bbox = tight_bbox.adjust_bbox(self.figure, bbox_inches,

lib/matplotlib/backends/backend_cairo.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,8 @@ def print_svg(self, fobj, *args, **kwargs):
423423
def print_svgz(self, fobj, *args, **kwargs):
424424
return self._save(fobj, 'svgz', *args, **kwargs)
425425

426-
def _save(self, fo, fmt, **kwargs):
426+
def _save(self, fo, fmt, *, orientation='portrait', **kwargs):
427427
# save PDF/PS/SVG
428-
orientation = kwargs.get('orientation', 'portrait')
429428

430429
dpi = 72
431430
self.figure.dpi = dpi

lib/matplotlib/contour.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -732,9 +732,10 @@ class ContourSet(cm.ScalarMappable, ContourLabeler):
732732

733733
def __init__(self, ax, *args,
734734
levels=None, filled=False, linewidths=None, linestyles=None,
735-
alpha=None, origin=None, extent=None,
735+
hatches=(None,), alpha=None, origin=None, extent=None,
736736
cmap=None, colors=None, norm=None, vmin=None, vmax=None,
737-
extend='neither', antialiased=None,
737+
extend='neither', antialiased=None, nchunk=0, locator=None,
738+
transform=None,
738739
**kwargs):
739740
"""
740741
Draw contour lines or filled regions, depending on
@@ -785,7 +786,7 @@ def __init__(self, ax, *args,
785786
self.filled = filled
786787
self.linewidths = linewidths
787788
self.linestyles = linestyles
788-
self.hatches = kwargs.pop('hatches', [None])
789+
self.hatches = hatches
789790
self.alpha = alpha
790791
self.origin = origin
791792
self.extent = extent
@@ -798,8 +799,8 @@ def __init__(self, ax, *args,
798799
# The default for line contours will be taken from the
799800
# LineCollection default, which uses :rc:`lines.antialiased`.
800801

801-
self.nchunk = kwargs.pop('nchunk', 0)
802-
self.locator = kwargs.pop('locator', None)
802+
self.nchunk = nchunk
803+
self.locator = locator
803804
if (isinstance(norm, mcolors.LogNorm)
804805
or isinstance(self.locator, ticker.LogLocator)):
805806
self.logscale = True
@@ -817,7 +818,7 @@ def __init__(self, ax, *args,
817818
if self.origin == 'image':
818819
self.origin = mpl.rcParams['image.origin']
819820

820-
self._transform = kwargs.pop('transform', None)
821+
self._transform = transform
821822

822823
kwargs = self._process_args(*args, **kwargs)
823824
self._process_levels()
@@ -1396,7 +1397,7 @@ class QuadContourSet(ContourSet):
13961397
levels for filled contours. See :meth:`_process_colors` method.
13971398
"""
13981399

1399-
def _process_args(self, *args, **kwargs):
1400+
def _process_args(self, *args, corner_mask=None, **kwargs):
14001401
"""
14011402
Process args and kwargs.
14021403
"""
@@ -1412,9 +1413,9 @@ def _process_args(self, *args, **kwargs):
14121413
else:
14131414
import matplotlib._contour as _contour
14141415

1415-
self._corner_mask = kwargs.pop('corner_mask', None)
1416-
if self._corner_mask is None:
1417-
self._corner_mask = mpl.rcParams['contour.corner_mask']
1416+
if corner_mask is None:
1417+
corner_mask = mpl.rcParams['contour.corner_mask']
1418+
self._corner_mask = corner_mask
14181419

14191420
x, y, z = self._contour_args(args, kwargs)
14201421

lib/matplotlib/legend.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,8 @@ def _get_legend_handles_labels(axs, legend_handler_map=None):
12121212
return handles, labels
12131213

12141214

1215-
def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs):
1215+
def _parse_legend_args(axs, *args, handles=None, labels=None, handler_map=None,
1216+
**kwargs):
12161217
"""
12171218
Get the handles and labels from the calls to either ``figure.legend``
12181219
or ``axes.legend``.
@@ -1221,7 +1222,6 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs):
12211222
"""
12221223
log = logging.getLogger(__name__)
12231224

1224-
handlers = kwargs.get('handler_map', {}) or {}
12251225
extra_args = ()
12261226

12271227
if (handles is not None or labels is not None) and args:
@@ -1238,11 +1238,11 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs):
12381238
elif labels is not None and handles is None:
12391239
# Get as many handles as there are labels.
12401240
handles = [handle for handle, label
1241-
in zip(_get_legend_handles(axs, handlers), labels)]
1241+
in zip(_get_legend_handles(axs, handler_map), labels)]
12421242

12431243
# No arguments - automatically detect labels and handles.
12441244
elif len(args) == 0:
1245-
handles, labels = _get_legend_handles_labels(axs, handlers)
1245+
handles, labels = _get_legend_handles_labels(axs, handler_map)
12461246
if not handles:
12471247
log.warning('No handles with labels found to put in legend.')
12481248

@@ -1251,7 +1251,7 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs):
12511251
labels, = args
12521252
# Get as many handles as there are labels.
12531253
handles = [handle for handle, label
1254-
in zip(_get_legend_handles(axs, handlers), labels)]
1254+
in zip(_get_legend_handles(axs, handler_map), labels)]
12551255

12561256
# Two arguments:
12571257
# * user defined handles and labels

lib/matplotlib/tests/test_axes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,8 +2060,7 @@ def get_next_color():
20602060
get_next_color_func=get_next_color)
20612061

20622062

2063-
def _params(c=None, xsize=2, **kwargs):
2064-
edgecolors = kwargs.pop('edgecolors', None)
2063+
def _params(c=None, xsize=2, *, edgecolors=None, **kwargs):
20652064
return (c, edgecolors, kwargs if kwargs is not None else {}, xsize)
20662065
_result = namedtuple('_result', 'c, colors')
20672066

lib/mpl_toolkits/axisartist/floating_axes.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -308,30 +308,15 @@ def get_boundary(self):
308308
class FloatingAxesBase:
309309

310310
def __init__(self, *args, **kwargs):
311-
grid_helper = kwargs.get("grid_helper", None)
312-
if grid_helper is None:
313-
raise ValueError("FloatingAxes requires grid_helper argument")
314-
if not hasattr(grid_helper, "get_boundary"):
315-
raise ValueError("grid_helper must implement get_boundary method")
316-
317311
self._axes_class_floating.__init__(self, *args, **kwargs)
318-
319312
self.set_aspect(1.)
320313
self.adjust_axes_lim()
321314

322315
def _gen_axes_patch(self):
323-
"""
324-
Returns the patch used to draw the background of the axes. It
325-
is also used as the clipping path for any data elements on the
326-
axes.
327-
328-
In the standard axes, this is a rectangle, but in other
329-
projections it may not be.
330-
331-
.. note::
332-
Intended to be overridden by new projection types.
333-
"""
316+
# docstring inherited
334317
grid_helper = self.get_grid_helper()
318+
if not hasattr(grid_helper, "get_boundary"):
319+
raise ValueError("grid_helper must implement get_boundary method")
335320
t = grid_helper.get_boundary()
336321
return mpatches.Polygon(t)
337322

0 commit comments

Comments
 (0)
0