Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 55fb9f0

Browse files
authored
Merge pull request #18577 from timhoffm/test-cleanup
Random test cleanups
2 parents 70c5f2b + 220894f commit 55fb9f0

File tree

6 files changed

+85
-89
lines changed

6 files changed

+85
-89
lines changed

lib/matplotlib/tests/test_afm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def test_font_manager_weight_normalization():
107107
def test_bad_afm(afm_data):
108108
fh = BytesIO(afm_data)
109109
with pytest.raises(RuntimeError):
110-
header = afm._parse_header(fh)
110+
afm._parse_header(fh)
111111

112112

113113
@pytest.mark.parametrize(
@@ -132,6 +132,6 @@ def test_bad_afm(afm_data):
132132
def test_malformed_header(afm_data, caplog):
133133
fh = BytesIO(afm_data)
134134
with caplog.at_level(logging.ERROR):
135-
header = afm._parse_header(fh)
135+
afm._parse_header(fh)
136136

137137
assert len(caplog.records) == 1

lib/matplotlib/tests/test_agg.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,30 +161,30 @@ def process_image(self, padded_src, dpi):
161161
fig, ax = plt.subplots()
162162

163163
# draw lines
164-
l1, = ax.plot([0.1, 0.5, 0.9], [0.1, 0.9, 0.5], "bo-",
165-
mec="b", mfc="w", lw=5, mew=3, ms=10, label="Line 1")
166-
l2, = ax.plot([0.1, 0.5, 0.9], [0.5, 0.2, 0.7], "ro-",
167-
mec="r", mfc="w", lw=5, mew=3, ms=10, label="Line 1")
164+
line1, = ax.plot([0.1, 0.5, 0.9], [0.1, 0.9, 0.5], "bo-",
165+
mec="b", mfc="w", lw=5, mew=3, ms=10, label="Line 1")
166+
line2, = ax.plot([0.1, 0.5, 0.9], [0.5, 0.2, 0.7], "ro-",
167+
mec="r", mfc="w", lw=5, mew=3, ms=10, label="Line 1")
168168

169169
gauss = DropShadowFilter(4)
170170

171-
for l in [l1, l2]:
171+
for line in [line1, line2]:
172172

173173
# draw shadows with same lines with slight offset.
174-
xx = l.get_xdata()
175-
yy = l.get_ydata()
174+
xx = line.get_xdata()
175+
yy = line.get_ydata()
176176
shadow, = ax.plot(xx, yy)
177-
shadow.update_from(l)
177+
shadow.update_from(line)
178178

179179
# offset transform
180-
ot = mtransforms.offset_copy(l.get_transform(), ax.figure,
180+
ot = mtransforms.offset_copy(line.get_transform(), ax.figure,
181181
x=4.0, y=-6.0, units='points')
182182

183183
shadow.set_transform(ot)
184184

185185
# adjust zorder of the shadow lines so that it is drawn below the
186186
# original lines
187-
shadow.set_zorder(l.get_zorder() - 0.5)
187+
shadow.set_zorder(line.get_zorder() - 0.5)
188188
shadow.set_agg_filter(gauss)
189189
shadow.set_rasterized(True) # to support mixed-mode renderers
190190

lib/matplotlib/tests/test_axes.py

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2938,15 +2938,14 @@ def test_boxplot_mod_artist_after_plotting():
29382938
def test_vert_violinplot_baseline():
29392939
# First 9 digits of frac(sqrt(2))
29402940
np.random.seed(414213562)
2941-
data = [np.random.normal(size=100) for i in range(4)]
2941+
data = [np.random.normal(size=100) for _ in range(4)]
29422942
ax = plt.axes()
29432943
ax.violinplot(data, positions=range(4), showmeans=0, showextrema=0,
29442944
showmedians=0)
29452945

29462946
# Reuse testcase from above for a labeled data test
29472947
data = {"d": data}
29482948
fig, ax = plt.subplots()
2949-
ax = plt.axes()
29502949
ax.violinplot("d", positions=range(4), showmeans=0, showextrema=0,
29512950
showmedians=0, data=data)
29522951

@@ -2956,7 +2955,7 @@ def test_vert_violinplot_showmeans():
29562955
ax = plt.axes()
29572956
# First 9 digits of frac(sqrt(3))
29582957
np.random.seed(732050807)
2959-
data = [np.random.normal(size=100) for i in range(4)]
2958+
data = [np.random.normal(size=100) for _ in range(4)]
29602959
ax.violinplot(data, positions=range(4), showmeans=1, showextrema=0,
29612960
showmedians=0)
29622961

@@ -2966,7 +2965,7 @@ def test_vert_violinplot_showextrema():
29662965
ax = plt.axes()
29672966
# First 9 digits of frac(sqrt(5))
29682967
np.random.seed(236067977)
2969-
data = [np.random.normal(size=100) for i in range(4)]
2968+
data = [np.random.normal(size=100) for _ in range(4)]
29702969
ax.violinplot(data, positions=range(4), showmeans=0, showextrema=1,
29712970
showmedians=0)
29722971

@@ -2976,7 +2975,7 @@ def test_vert_violinplot_showmedians():
29762975
ax = plt.axes()
29772976
# First 9 digits of frac(sqrt(7))
29782977
np.random.seed(645751311)
2979-
data = [np.random.normal(size=100) for i in range(4)]
2978+
data = [np.random.normal(size=100) for _ in range(4)]
29802979
ax.violinplot(data, positions=range(4), showmeans=0, showextrema=0,
29812980
showmedians=1)
29822981

@@ -2986,7 +2985,7 @@ def test_vert_violinplot_showall():
29862985
ax = plt.axes()
29872986
# First 9 digits of frac(sqrt(11))
29882987
np.random.seed(316624790)
2989-
data = [np.random.normal(size=100) for i in range(4)]
2988+
data = [np.random.normal(size=100) for _ in range(4)]
29902989
ax.violinplot(data, positions=range(4), showmeans=1, showextrema=1,
29912990
showmedians=1,
29922991
quantiles=[[0.1, 0.9], [0.2, 0.8], [0.3, 0.7], [0.4, 0.6]])
@@ -2997,7 +2996,7 @@ def test_vert_violinplot_custompoints_10():
29972996
ax = plt.axes()
29982997
# First 9 digits of frac(sqrt(13))
29992998
np.random.seed(605551275)
3000-
data = [np.random.normal(size=100) for i in range(4)]
2999+
data = [np.random.normal(size=100) for _ in range(4)]
30013000
ax.violinplot(data, positions=range(4), showmeans=0, showextrema=0,
30023001
showmedians=0, points=10)
30033002

@@ -3007,7 +3006,7 @@ def test_vert_violinplot_custompoints_200():
30073006
ax = plt.axes()
30083007
# First 9 digits of frac(sqrt(17))
30093008
np.random.seed(123105625)
3010-
data = [np.random.normal(size=100) for i in range(4)]
3009+
data = [np.random.normal(size=100) for _ in range(4)]
30113010
ax.violinplot(data, positions=range(4), showmeans=0, showextrema=0,
30123011
showmedians=0, points=200)
30133012

@@ -3017,7 +3016,7 @@ def test_horiz_violinplot_baseline():
30173016
ax = plt.axes()
30183017
# First 9 digits of frac(sqrt(19))
30193018
np.random.seed(358898943)
3020-
data = [np.random.normal(size=100) for i in range(4)]
3019+
data = [np.random.normal(size=100) for _ in range(4)]
30213020
ax.violinplot(data, positions=range(4), vert=False, showmeans=0,
30223021
showextrema=0, showmedians=0)
30233022

@@ -3027,7 +3026,7 @@ def test_horiz_violinplot_showmedians():
30273026
ax = plt.axes()
30283027
# First 9 digits of frac(sqrt(23))
30293028
np.random.seed(795831523)
3030-
data = [np.random.normal(size=100) for i in range(4)]
3029+
data = [np.random.normal(size=100) for _ in range(4)]
30313030
ax.violinplot(data, positions=range(4), vert=False, showmeans=0,
30323031
showextrema=0, showmedians=1)
30333032

@@ -3037,7 +3036,7 @@ def test_horiz_violinplot_showmeans():
30373036
ax = plt.axes()
30383037
# First 9 digits of frac(sqrt(29))
30393038
np.random.seed(385164807)
3040-
data = [np.random.normal(size=100) for i in range(4)]
3039+
data = [np.random.normal(size=100) for _ in range(4)]
30413040
ax.violinplot(data, positions=range(4), vert=False, showmeans=1,
30423041
showextrema=0, showmedians=0)
30433042

@@ -3047,7 +3046,7 @@ def test_horiz_violinplot_showextrema():
30473046
ax = plt.axes()
30483047
# First 9 digits of frac(sqrt(31))
30493048
np.random.seed(567764362)
3050-
data = [np.random.normal(size=100) for i in range(4)]
3049+
data = [np.random.normal(size=100) for _ in range(4)]
30513050
ax.violinplot(data, positions=range(4), vert=False, showmeans=0,
30523051
showextrema=1, showmedians=0)
30533052

@@ -3057,7 +3056,7 @@ def test_horiz_violinplot_showall():
30573056
ax = plt.axes()
30583057
# First 9 digits of frac(sqrt(37))
30593058
np.random.seed(82762530)
3060-
data = [np.random.normal(size=100) for i in range(4)]
3059+
data = [np.random.normal(size=100) for _ in range(4)]
30613060
ax.violinplot(data, positions=range(4), vert=False, showmeans=1,
30623061
showextrema=1, showmedians=1,
30633062
quantiles=[[0.1, 0.9], [0.2, 0.8], [0.3, 0.7], [0.4, 0.6]])
@@ -3068,7 +3067,7 @@ def test_horiz_violinplot_custompoints_10():
30683067
ax = plt.axes()
30693068
# First 9 digits of frac(sqrt(41))
30703069
np.random.seed(403124237)
3071-
data = [np.random.normal(size=100) for i in range(4)]
3070+
data = [np.random.normal(size=100) for _ in range(4)]
30723071
ax.violinplot(data, positions=range(4), vert=False, showmeans=0,
30733072
showextrema=0, showmedians=0, points=10)
30743073

@@ -3078,7 +3077,7 @@ def test_horiz_violinplot_custompoints_200():
30783077
ax = plt.axes()
30793078
# First 9 digits of frac(sqrt(43))
30803079
np.random.seed(557438524)
3081-
data = [np.random.normal(size=100) for i in range(4)]
3080+
data = [np.random.normal(size=100) for _ in range(4)]
30823081
ax.violinplot(data, positions=range(4), vert=False, showmeans=0,
30833082
showextrema=0, showmedians=0, points=200)
30843083

@@ -3087,7 +3086,7 @@ def test_violinplot_bad_positions():
30873086
ax = plt.axes()
30883087
# First 9 digits of frac(sqrt(47))
30893088
np.random.seed(855654600)
3090-
data = [np.random.normal(size=100) for i in range(4)]
3089+
data = [np.random.normal(size=100) for _ in range(4)]
30913090
with pytest.raises(ValueError):
30923091
ax.violinplot(data, positions=range(5))
30933092

@@ -3096,7 +3095,7 @@ def test_violinplot_bad_widths():
30963095
ax = plt.axes()
30973096
# First 9 digits of frac(sqrt(53))
30983097
np.random.seed(280109889)
3099-
data = [np.random.normal(size=100) for i in range(4)]
3098+
data = [np.random.normal(size=100) for _ in range(4)]
31003099
with pytest.raises(ValueError):
31013100
ax.violinplot(data, positions=range(4), widths=[1, 2, 3])
31023101

@@ -3722,16 +3721,16 @@ def test_hist_emptydata():
37223721
def test_hist_labels():
37233722
# test singleton labels OK
37243723
fig, ax = plt.subplots()
3725-
l = ax.hist([0, 1], label=0)
3726-
assert l[2][0].get_label() == '0'
3727-
l = ax.hist([0, 1], label=[0])
3728-
assert l[2][0].get_label() == '0'
3729-
l = ax.hist([0, 1], label=None)
3730-
assert l[2][0].get_label() == '_nolegend_'
3731-
l = ax.hist([0, 1], label='0')
3732-
assert l[2][0].get_label() == '0'
3733-
l = ax.hist([0, 1], label='00')
3734-
assert l[2][0].get_label() == '00'
3724+
_, _, bars = ax.hist([0, 1], label=0)
3725+
assert bars[0].get_label() == '0'
3726+
_, _, bars = ax.hist([0, 1], label=[0])
3727+
assert bars[0].get_label() == '0'
3728+
_, _, bars = ax.hist([0, 1], label=None)
3729+
assert bars[0].get_label() == '_nolegend_'
3730+
_, _, bars = ax.hist([0, 1], label='0')
3731+
assert bars[0].get_label() == '0'
3732+
_, _, bars = ax.hist([0, 1], label='00')
3733+
assert bars[0].get_label() == '00'
37353734

37363735

37373736
@image_comparison(['transparent_markers'], remove_text=True)
@@ -3910,7 +3909,7 @@ def test_eventplot_defaults():
39103909
])
39113910
def test_eventplot_colors(colors):
39123911
"""Test the *colors* parameter of eventplot. Inspired by issue #8193."""
3913-
data = [[i] for i in range(4)] # 4 successive events of different nature
3912+
data = [[0], [1], [2], [3]] # 4 successive events of different nature
39143913

39153914
# Build the list of the expected colors
39163915
expected = [c if c is not None else 'C0' for c in colors]
@@ -4557,9 +4556,9 @@ def test_rcparam_grid_minor():
45574556
matplotlib.rcParams['axes.grid'] = True
45584557

45594558
values = (
4560-
(('both'), (True, True)),
4561-
(('major'), (True, False)),
4562-
(('minor'), (False, True))
4559+
('both', (True, True)),
4560+
('major', (True, False)),
4561+
('minor', (False, True))
45634562
)
45644563

45654564
for locator, result in values:
@@ -4576,7 +4575,6 @@ def test_vline_limit():
45764575
ax = fig.gca()
45774576
ax.axvline(0.5)
45784577
ax.plot([-0.1, 0, 0.2, 0.1])
4579-
(ymin, ymax) = ax.get_ylim()
45804578
assert_allclose(ax.get_ylim(), (-.1, .2))
45814579

45824580

@@ -4674,10 +4672,10 @@ def test_relim_visible_only():
46744672
ax.plot(x1, y1)
46754673
assert ax.get_xlim() == x1
46764674
assert ax.get_ylim() == y1
4677-
l = ax.plot(x2, y2)
4675+
line, = ax.plot(x2, y2)
46784676
assert ax.get_xlim() == x2
46794677
assert ax.get_ylim() == y2
4680-
l[0].set_visible(False)
4678+
line.set_visible(False)
46814679
assert ax.get_xlim() == x2
46824680
assert ax.get_ylim() == y2
46834681

@@ -5109,7 +5107,7 @@ def test_rc_spines():
51095107
'axes.spines.top': False,
51105108
'axes.spines.bottom': False}
51115109
with matplotlib.rc_context(rc_dict):
5112-
fig, ax = plt.subplots()
5110+
plt.subplots() # create a figure and axes with the spine properties
51135111

51145112

51155113
@image_comparison(['rc_grid.png'], savefig_kwarg={'dpi': 40})
@@ -5484,7 +5482,7 @@ def test_adjust_numtick_aspect():
54845482
@image_comparison(["auto_numticks.png"], style='default')
54855483
def test_auto_numticks():
54865484
# Make tiny, empty subplots, verify that there are only 3 ticks.
5487-
fig, axs = plt.subplots(4, 4)
5485+
plt.subplots(4, 4)
54885486

54895487

54905488
@image_comparison(["auto_numticks_log.png"], style='default')
@@ -6121,7 +6119,7 @@ class DummySubplot(matplotlib.axes.SubplotBase, Dummy):
61216119
assert DummySubplot is FactoryDummySubplot
61226120

61236121

6124-
def test_gettightbbox_ignoreNaN():
6122+
def test_gettightbbox_ignore_nan():
61256123
fig, ax = plt.subplots()
61266124
remove_ticks_and_titles(fig)
61276125
ax.text(np.NaN, 1, 'Boo')
@@ -6196,6 +6194,7 @@ def test_secondary_fail():
61966194
def test_secondary_resize():
61976195
fig, ax = plt.subplots(figsize=(10, 5))
61986196
ax.plot(np.arange(2, 11), np.arange(2, 11))
6197+
61996198
def invert(x):
62006199
with np.errstate(divide='ignore'):
62016200
return 1 / x
@@ -6209,6 +6208,7 @@ def invert(x):
62096208
def test_secondary_minorloc():
62106209
fig, ax = plt.subplots(figsize=(10, 5))
62116210
ax.plot(np.arange(2, 11), np.arange(2, 11))
6211+
62126212
def invert(x):
62136213
with np.errstate(divide='ignore'):
62146214
return 1 / x
@@ -6332,10 +6332,6 @@ def test_nodecorator():
63326332
bbaxis, bbspines, bbax, bbtb = color_boxes(fig, ax)
63336333

63346334
# test the axis bboxes
6335-
target = [
6336-
None,
6337-
None
6338-
]
63396335
for nn, b in enumerate(bbaxis):
63406336
assert b is None
63416337

@@ -6366,14 +6362,15 @@ def test_displaced_spine():
63666362
fig.canvas.draw()
63676363
bbaxis, bbspines, bbax, bbtb = color_boxes(fig, ax)
63686364

6369-
target = [
6365+
targets = [
63706366
[150., 24., 930., 11.111111],
63716367
[150.0, 1080.0, 930.0, 0.0],
63726368
[150.0, 119.9999, 11.111, 960.0],
63736369
[1068.8888, 119.9999, 11.111, 960.0]
63746370
]
6375-
for nn, b in enumerate(bbspines):
6376-
targetbb = mtransforms.Bbox.from_bounds(*target[nn])
6371+
for target, bbspine in zip(targets, bbspines):
6372+
targetbb = mtransforms.Bbox.from_bounds(*target)
6373+
assert_allclose(bbspine.bounds, targetbb.bounds, atol=1e-2)
63776374

63786375
target = [150.0, 119.99999999999997, 930.0, 960.0]
63796376
targetbb = mtransforms.Bbox.from_bounds(*target)

lib/matplotlib/tests/test_bbox_tight.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,26 @@ def test_bbox_inches_tight():
1919
[78415, 81858, 150656, 193263, 69638],
2020
[139361, 331509, 343164, 781380, 52269]]
2121

22-
colLabels = rowLabels = [''] * 5
22+
col_labels = row_labels = [''] * 5
2323

2424
rows = len(data)
25-
ind = np.arange(len(colLabels)) + 0.3 # the x locations for the groups
26-
cellText = []
25+
ind = np.arange(len(col_labels)) + 0.3 # the x locations for the groups
26+
cell_text = []
2727
width = 0.4 # the width of the bars
28-
yoff = np.zeros(len(colLabels))
28+
yoff = np.zeros(len(col_labels))
2929
# the bottom values for stacked bar chart
3030
fig, ax = plt.subplots(1, 1)
3131
for row in range(rows):
3232
ax.bar(ind, data[row], width, bottom=yoff, align='edge', color='b')
3333
yoff = yoff + data[row]
34-
cellText.append([''])
34+
cell_text.append([''])
3535
plt.xticks([])
3636
plt.xlim(0, 5)
3737
plt.legend([''] * 5, loc=(1.2, 0.2))
3838
fig.legend([''] * 5, bbox_to_anchor=(0, 0.2), loc='lower left')
3939
# Add a table at the bottom of the axes
40-
cellText.reverse()
41-
plt.table(cellText=cellText, rowLabels=rowLabels, colLabels=colLabels,
40+
cell_text.reverse()
41+
plt.table(cellText=cell_text, rowLabels=row_labels, colLabels=col_labels,
4242
loc='bottom')
4343

4444

0 commit comments

Comments
 (0)