Skip to content

Commit ebe50c1

Browse files
authored
Merge pull request #10592 from anntzer/unpackgen
Rely on generalized * and ** unpackings where possible.
2 parents c1eeb4c + d520128 commit ebe50c1

38 files changed

+171
-223
lines changed

examples/api/filled_step.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def filled_hist(ax, edges, values, bottoms=None, orientation='v',
4848
Artist added to the Axes
4949
"""
5050
print(orientation)
51-
if orientation not in set('hv'):
51+
if orientation not in 'hv':
5252
raise ValueError("orientation must be in {{'h', 'v'}} "
5353
"not {o}".format(o=orientation))
5454

examples/images_contours_and_fields/custom_cmap.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,13 @@
143143

144144
# Make a modified version of cdict3 with some transparency
145145
# in the middle of the range.
146-
cdict4 = cdict3.copy()
147-
cdict4['alpha'] = ((0.0, 1.0, 1.0),
146+
cdict4 = {**cdict3,
147+
'alpha': ((0.0, 1.0, 1.0),
148148
# (0.25,1.0, 1.0),
149-
(0.5, 0.3, 0.3),
149+
(0.5, 0.3, 0.3),
150150
# (0.75,1.0, 1.0),
151-
(1.0, 1.0, 1.0))
151+
(1.0, 1.0, 1.0)),
152+
}
152153

153154

154155
###############################################################################

examples/mplot3d/polys3d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def polygon_under_graph(xlist, ylist):
3030
Construct the vertex list which defines the polygon filling the space under
3131
the (xlist, ylist) line graph. Assumes the xs are in ascending order.
3232
'''
33-
return [(xlist[0], 0.)] + list(zip(xlist, ylist)) + [(xlist[-1], 0.)]
33+
return [(xlist[0], 0.), *zip(xlist, ylist), (xlist[-1], 0.)]
3434

3535

3636
fig = plt.figure()

examples/showcase/integral.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def func(x):
3232
# Make the shaded region
3333
ix = np.linspace(a, b)
3434
iy = func(ix)
35-
verts = [(a, 0)] + list(zip(ix, iy)) + [(b, 0)]
35+
verts = [(a, 0), *zip(ix, iy), (b, 0)]
3636
poly = Polygon(verts, facecolor='0.9', edgecolor='0.5')
3737
ax.add_patch(poly)
3838

examples/subplots_axes_and_figures/axes_zoom_effect.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
================
55
66
"""
7-
from matplotlib.transforms import Bbox, TransformedBbox, \
8-
blended_transform_factory
7+
from matplotlib.transforms import (
8+
Bbox, TransformedBbox, blended_transform_factory)
99

10-
from mpl_toolkits.axes_grid1.inset_locator import BboxPatch, BboxConnector,\
11-
BboxConnectorPatch
10+
from mpl_toolkits.axes_grid1.inset_locator import (
11+
BboxPatch, BboxConnector, BboxConnectorPatch)
1212

1313

1414
def connect_bbox(bbox1, bbox2,
1515
loc1a, loc2a, loc1b, loc2b,
1616
prop_lines, prop_patches=None):
1717
if prop_patches is None:
18-
prop_patches = prop_lines.copy()
19-
prop_patches["alpha"] = prop_patches.get("alpha", 1) * 0.2
18+
prop_patches = {
19+
**prop_lines,
20+
"alpha": prop_lines.get("alpha", 1) * 0.2,
21+
}
2022

2123
c1 = BboxConnector(bbox1, bbox2, loc1=loc1a, loc2=loc2a, **prop_lines)
2224
c1.set_clip_on(False)
@@ -55,14 +57,12 @@ def zoom_effect01(ax1, ax2, xmin, xmax, **kwargs):
5557
mybbox1 = TransformedBbox(bbox, trans1)
5658
mybbox2 = TransformedBbox(bbox, trans2)
5759

58-
prop_patches = kwargs.copy()
59-
prop_patches["ec"] = "none"
60-
prop_patches["alpha"] = 0.2
60+
prop_patches = {**kwargs, "ec": "none", "alpha": 0.2}
6161

62-
c1, c2, bbox_patch1, bbox_patch2, p = \
63-
connect_bbox(mybbox1, mybbox2,
64-
loc1a=3, loc2a=2, loc1b=4, loc2b=1,
65-
prop_lines=kwargs, prop_patches=prop_patches)
62+
c1, c2, bbox_patch1, bbox_patch2, p = connect_bbox(
63+
mybbox1, mybbox2,
64+
loc1a=3, loc2a=2, loc1b=4, loc2b=1,
65+
prop_lines=kwargs, prop_patches=prop_patches)
6666

6767
ax1.add_patch(bbox_patch1)
6868
ax2.add_patch(bbox_patch2)
@@ -88,14 +88,12 @@ def zoom_effect02(ax1, ax2, **kwargs):
8888
mybbox1 = ax1.bbox
8989
mybbox2 = TransformedBbox(ax1.viewLim, trans)
9090

91-
prop_patches = kwargs.copy()
92-
prop_patches["ec"] = "none"
93-
prop_patches["alpha"] = 0.2
91+
prop_patches = {**kwargs, "ec": "none", "alpha": 0.2}
9492

95-
c1, c2, bbox_patch1, bbox_patch2, p = \
96-
connect_bbox(mybbox1, mybbox2,
97-
loc1a=3, loc2a=2, loc1b=4, loc2b=1,
98-
prop_lines=kwargs, prop_patches=prop_patches)
93+
c1, c2, bbox_patch1, bbox_patch2, p = connect_bbox(
94+
mybbox1, mybbox2,
95+
loc1a=3, loc2a=2, loc1b=4, loc2b=1,
96+
prop_lines=kwargs, prop_patches=prop_patches)
9997

10098
ax1.add_patch(bbox_patch1)
10199
ax2.add_patch(bbox_patch2)

examples/tests/backend_driver_sgskip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def report_missing(dir, flist):
315315
globstr = os.path.join(dir, '*.py')
316316
fnames = glob.glob(globstr)
317317

318-
pyfiles = {os.path.split(fullpath)[-1] for fullpath in set(fnames)}
318+
pyfiles = {os.path.split(fullpath)[-1] for fullpath in fnames}
319319

320320
exclude = set(excluded.get(dir, []))
321321
flist = set(flist)

lib/matplotlib/_constrained_layout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
161161
invTransFig = fig.transFigure.inverted().transform_bbox
162162

163163
# list of unique gridspecs that contain child axes:
164-
gss = set([])
164+
gss = set()
165165
for ax in fig.axes:
166166
if hasattr(ax, 'get_subplotspec'):
167167
gs = ax.get_subplotspec().get_gridspec()

lib/matplotlib/animation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ def _blit_clear(self, artists, bg_cache):
12711271
# Get a list of the axes that need clearing from the artists that
12721272
# have been drawn. Grab the appropriate saved background from the
12731273
# cache and restore.
1274-
axes = set(a.axes for a in artists)
1274+
axes = {a.axes for a in artists}
12751275
for a in axes:
12761276
if a in bg_cache:
12771277
a.figure.canvas.restore_region(bg_cache[a])

lib/matplotlib/axes/_axes.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3077,8 +3077,9 @@ def errorbar(self, x, y, yerr=None, xerr=None,
30773077
fmt_style_kwargs = {}
30783078
else:
30793079
fmt_style_kwargs = {k: v for k, v in
3080-
zip(('linestyle', 'marker', 'color'),
3081-
_process_plot_format(fmt)) if v is not None}
3080+
zip(('linestyle', 'marker', 'color'),
3081+
_process_plot_format(fmt))
3082+
if v is not None}
30823083
if fmt == 'none':
30833084
# Remove alpha=0 color that _process_plot_format returns
30843085
fmt_style_kwargs.pop('color')
@@ -3114,12 +3115,12 @@ def errorbar(self, x, y, yerr=None, xerr=None,
31143115
yerr = [yerr] * len(y)
31153116

31163117
# make the style dict for the 'normal' plot line
3117-
plot_line_style = dict(base_style)
3118-
plot_line_style.update(**kwargs)
3119-
if barsabove:
3120-
plot_line_style['zorder'] = kwargs['zorder'] - .1
3121-
else:
3122-
plot_line_style['zorder'] = kwargs['zorder'] + .1
3118+
plot_line_style = {
3119+
**base_style,
3120+
**kwargs,
3121+
'zorder': (kwargs['zorder'] - .1 if barsabove else
3122+
kwargs['zorder'] + .1),
3123+
}
31233124

31243125
# make the style dict for the line collections (the bars)
31253126
eb_lines_style = dict(base_style)
@@ -7689,8 +7690,8 @@ def matshow(self, Z, **kwargs):
76897690
nr, nc = Z.shape
76907691
kw = {'origin': 'upper',
76917692
'interpolation': 'nearest',
7692-
'aspect': 'equal'} # (already the imshow default)
7693-
kw.update(kwargs)
7693+
'aspect': 'equal', # (already the imshow default)
7694+
**kwargs}
76947695
im = self.imshow(Z, **kw)
76957696
self.title.set_y(1.05)
76967697
self.xaxis.tick_top()

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,7 @@ def _setdefaults(self, defaults, *kwargs):
287287
kw[k] = defaults[k]
288288

289289
def _makeline(self, x, y, kw, kwargs):
290-
kw = kw.copy() # Don't modify the original kw.
291-
kw.update(kwargs)
290+
kw = {**kw, **kwargs} # Don't modify the original kw.
292291
default_dict = self._getdefaults(None, kw)
293292
self._setdefaults(default_dict, kw)
294293
seg = mlines.Line2D(x, y, **kw)
@@ -527,8 +526,7 @@ def __init__(self, fig, rect,
527526
if yscale:
528527
self.set_yscale(yscale)
529528

530-
if len(kwargs):
531-
self.update(kwargs)
529+
self.update(kwargs)
532530

533531
if self.xaxis is not None:
534532
self._xcid = self.xaxis.callbacks.connect(

lib/matplotlib/backends/backend_nbagg.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,15 @@ def destroy(self):
135135

136136
def clearup_closed(self):
137137
"""Clear up any closed Comms."""
138-
self.web_sockets = set([socket for socket in self.web_sockets
139-
if socket.is_open()])
138+
self.web_sockets = {socket for socket in self.web_sockets
139+
if socket.is_open()}
140140

141141
if len(self.web_sockets) == 0:
142142
self.canvas.close_event()
143143

144144
def remove_comm(self, comm_id):
145-
self.web_sockets = set([socket for socket in self.web_sockets
146-
if not socket.comm.comm_id == comm_id])
145+
self.web_sockets = {socket for socket in self.web_sockets
146+
if not socket.comm.comm_id == comm_id}
147147

148148

149149
class FigureCanvasNbAgg(FigureCanvasWebAggCore):

lib/matplotlib/backends/backend_pdf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,7 +1647,7 @@ def track_characters(self, font, s):
16471647
realpath, stat_key = get_realpath_and_stat(fname)
16481648
used_characters = self.file.used_characters.setdefault(
16491649
stat_key, (realpath, set()))
1650-
used_characters[1].update([ord(x) for x in s])
1650+
used_characters[1].update(map(ord, s))
16511651

16521652
def merge_used_characters(self, other):
16531653
for stat_key, (realpath, charset) in other.items():
@@ -2288,15 +2288,15 @@ def rgb_cmd(self, rgb):
22882288
if rgb[0] == rgb[1] == rgb[2]:
22892289
return [rgb[0], Op.setgray_stroke]
22902290
else:
2291-
return list(rgb[:3]) + [Op.setrgb_stroke]
2291+
return [*rgb[:3], Op.setrgb_stroke]
22922292

22932293
def fillcolor_cmd(self, rgb):
22942294
if rgb is None or rcParams['pdf.inheritcolor']:
22952295
return []
22962296
elif rgb[0] == rgb[1] == rgb[2]:
22972297
return [rgb[0], Op.setgray_nonstroke]
22982298
else:
2299-
return list(rgb[:3]) + [Op.setrgb_nonstroke]
2299+
return [*rgb[:3], Op.setrgb_nonstroke]
23002300

23012301
def push(self):
23022302
parent = GraphicsContextPdf(self.file)

lib/matplotlib/backends/backend_ps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def track_characters(self, font, s):
230230
realpath, stat_key = get_realpath_and_stat(font.fname)
231231
used_characters = self.used_characters.setdefault(
232232
stat_key, (realpath, set()))
233-
used_characters[1].update([ord(x) for x in s])
233+
used_characters[1].update(map(ord, s))
234234

235235
def merge_used_characters(self, other):
236236
for stat_key, (realpath, charset) in six.iteritems(other):

lib/matplotlib/backends/backend_svg.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,11 @@ def start(self, tag, attrib={}, **extra):
143143
self.__tags.append(tag)
144144
self.__write(self.__indentation[:len(self.__tags) - 1])
145145
self.__write("<%s" % tag)
146-
if attrib or extra:
147-
attrib = attrib.copy()
148-
attrib.update(extra)
149-
attrib = sorted(six.iteritems(attrib))
150-
for k, v in attrib:
151-
if not v == '':
152-
k = escape_cdata(k)
153-
v = escape_attrib(v)
154-
self.__write(" %s=\"%s\"" % (k, v))
146+
for k, v in sorted({**attrib, **extra}.items()):
147+
if not v == '':
148+
k = escape_cdata(k)
149+
v = escape_attrib(v)
150+
self.__write(" %s=\"%s\"" % (k, v))
155151
self.__open = 1
156152
return len(self.__tags)-1
157153

lib/matplotlib/backends/backend_webagg_core.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,7 @@ def get_static_file_path(cls):
499499
return os.path.join(os.path.dirname(__file__), 'web_backend')
500500

501501
def _send_event(self, event_type, **kwargs):
502-
payload = {'type': event_type}
503-
payload.update(kwargs)
502+
payload = {'type': event_type, **kwargs}
504503
for s in self.web_sockets:
505504
s.send_json(payload)
506505

lib/matplotlib/backends/backend_wx.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -790,15 +790,17 @@ def gui_repaint(self, drawDC=None, origin='WX'):
790790
else:
791791
drawDC.DrawBitmap(self.bitmap, 0, 0)
792792

793-
filetypes = FigureCanvasBase.filetypes.copy()
794-
filetypes['bmp'] = 'Windows bitmap'
795-
filetypes['jpeg'] = 'JPEG'
796-
filetypes['jpg'] = 'JPEG'
797-
filetypes['pcx'] = 'PCX'
798-
filetypes['png'] = 'Portable Network Graphics'
799-
filetypes['tif'] = 'Tagged Image Format File'
800-
filetypes['tiff'] = 'Tagged Image Format File'
801-
filetypes['xpm'] = 'X pixmap'
793+
filetypes = {
794+
**FigureCanvasBase.filetypes,
795+
'bmp': 'Windows bitmap',
796+
'jpeg': 'JPEG',
797+
'jpg': 'JPEG',
798+
'pcx': 'PCX',
799+
'png': 'Portable Network Graphics',
800+
'tif': 'Tagged Image Format File',
801+
'tiff': 'Tagged Image Format File',
802+
'xpm': 'X pixmap',
803+
}
802804

803805
def print_figure(self, filename, *args, **kwargs):
804806
super().print_figure(filename, *args, **kwargs)

lib/matplotlib/backends/qt_editor/formlayout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def __init__(self, value, parent=None):
176176
# Font size
177177
self.size = QtWidgets.QComboBox(parent)
178178
self.size.setEditable(True)
179-
sizelist = list(range(6, 12)) + list(range(12, 30, 2)) + [36, 48, 72]
179+
sizelist = [*range(6, 12), *range(12, 30, 2), 36, 48, 72]
180180
size = font.pointSize()
181181
if size not in sizelist:
182182
sizelist.append(size)

lib/matplotlib/gridspec.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,7 @@ def get_subplot_params(self, figure=None, fig=None):
280280
else:
281281
subplotpars = copy.copy(figure.subplotpars)
282282

283-
update_kw = {k: getattr(self, k) for k in self._AllowedKeys}
284-
subplotpars.update(**update_kw)
283+
subplotpars.update(**{k: getattr(self, k) for k in self._AllowedKeys})
285284

286285
return subplotpars
287286

lib/matplotlib/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ def make_image(self, renderer, magnification=1.0, unsampled=False):
928928
if A.dtype != np.uint8:
929929
A = (255*A).astype(np.uint8)
930930
if A.shape[2] == 3:
931-
B = np.zeros(tuple(list(A.shape[0:2]) + [4]), np.uint8)
931+
B = np.zeros(tuple([*A.shape[0:2], 4]), np.uint8)
932932
B[:, :, 0:3] = A
933933
B[:, :, 3] = 255
934934
A = B

lib/matplotlib/lines.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,9 @@ class Line2D(Artist):
256256
}
257257

258258
# drawStyles should now be deprecated.
259-
drawStyles = {}
260-
drawStyles.update(_drawStyles_l)
261-
drawStyles.update(_drawStyles_s)
259+
drawStyles = {**_drawStyles_l, **_drawStyles_s}
262260
# Need a list ordered with long names first:
263-
drawStyleKeys = list(_drawStyles_l) + list(_drawStyles_s)
261+
drawStyleKeys = [*_drawStyles_l, *_drawStyles_s]
264262

265263
# Referenced here to maintain API. These are defined in
266264
# MarkerStyle

lib/matplotlib/mathtext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,7 +2369,7 @@ def __init__(self):
23692369

23702370
p.accent <<= Group(
23712371
Suppress(p.bslash)
2372-
+ oneOf(list(self._accent_map) + list(self._wide_accents))
2372+
+ oneOf([*self._accent_map, *self._wide_accents])
23732373
- p.placeable
23742374
)
23752375

@@ -2407,7 +2407,7 @@ def __init__(self):
24072407
p.ambi_delim <<= oneOf(list(self._ambi_delim))
24082408
p.left_delim <<= oneOf(list(self._left_delim))
24092409
p.right_delim <<= oneOf(list(self._right_delim))
2410-
p.right_delim_safe <<= oneOf(list(self._right_delim - {'}'}) + [r'\}'])
2410+
p.right_delim_safe <<= oneOf([*(self._right_delim - {'}'}), r'\}'])
24112411

24122412
p.genfrac <<= Group(
24132413
Suppress(Literal(r"\genfrac"))

0 commit comments

Comments
 (0)