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

Skip to content

Commit 2ad47ca

Browse files
committed
Rely on pytest to record warnings, rather than doing it manually.
Technically we could even get rid of the `assert len(recwarn) == 0` lines because we fail the tests on any warning, but I left them there for explicitness.
1 parent 9984f9c commit 2ad47ca

File tree

4 files changed

+20
-36
lines changed

4 files changed

+20
-36
lines changed

lib/matplotlib/tests/test_artist.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import io
22
from itertools import chain
3-
import warnings
43

54
import numpy as np
65

@@ -220,13 +219,10 @@ def test_default_edges():
220219
ax4.add_patch(pp1)
221220

222221

223-
def test_properties():
222+
def test_properties(recwarn):
224223
ln = mlines.Line2D([], [])
225-
with warnings.catch_warnings(record=True) as w:
226-
# Cause all warnings to always be triggered.
227-
warnings.simplefilter("always")
228-
ln.properties()
229-
assert len(w) == 0
224+
ln.properties()
225+
assert len(recwarn) == 0
230226

231227

232228
def test_setp():

lib/matplotlib/tests/test_axes.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5283,17 +5283,13 @@ def test_length_one_hist():
52835283
ax.hist([1])
52845284

52855285

5286-
def test_pathological_hexbin():
5286+
def test_pathological_hexbin(recwarn):
52875287
# issue #2863
5288-
out = io.BytesIO()
5289-
5290-
with warnings.catch_warnings(record=True) as w:
5291-
warnings.simplefilter("always")
5292-
mylist = [10] * 100
5293-
fig, ax = plt.subplots(1, 1)
5294-
ax.hexbin(mylist, mylist)
5295-
fig.savefig(out)
5296-
assert len(w) == 0
5288+
mylist = [10] * 100
5289+
fig, ax = plt.subplots(1, 1)
5290+
ax.hexbin(mylist, mylist)
5291+
fig.savefig(io.BytesIO())
5292+
assert len(recwarn) == 0
52975293

52985294

52995295
def test_color_None():

lib/matplotlib/tests/test_image.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import platform
77
import sys
88
import urllib.request
9-
import warnings
109

1110
import numpy as np
1211
from numpy import ma
@@ -981,11 +980,9 @@ def test_imshow_masked_interpolation():
981980
ax.axis('off')
982981

983982

984-
def test_imshow_no_warn_invalid():
985-
with warnings.catch_warnings(record=True) as warns:
986-
warnings.simplefilter("always")
987-
plt.imshow([[1, 2], [3, np.nan]])
988-
assert len(warns) == 0
983+
def test_imshow_no_warn_invalid(recwarn):
984+
plt.imshow([[1, 2], [3, np.nan]])
985+
assert len(recwarn) == 0
989986

990987

991988
@pytest.mark.parametrize(

lib/matplotlib/tests/test_quiver.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import warnings
21
import numpy as np
32
import pytest
43
import sys
@@ -72,29 +71,25 @@ def test_quiver_arg_sizes():
7271
plt.quiver(X2, X2, X2, X2, X3)
7372

7473

75-
def test_no_warnings():
74+
def test_no_warnings(recwarn):
7675
fig, ax = plt.subplots()
77-
7876
X, Y = np.meshgrid(np.arange(15), np.arange(10))
7977
U = V = np.ones_like(X)
80-
8178
phi = (np.random.rand(15, 10) - .5) * 150
82-
with warnings.catch_warnings(record=True) as w:
83-
ax.quiver(X, Y, U, V, angles=phi)
84-
fig.canvas.draw()
85-
assert len(w) == 0
79+
ax.quiver(X, Y, U, V, angles=phi)
80+
fig.canvas.draw()
81+
assert len(recwarn) == 0
8682

8783

88-
def test_zero_headlength():
84+
def test_zero_headlength(recwarn):
8985
# Based on report by Doug McNeil:
9086
# http://matplotlib.1069221.n5.nabble.com/quiver-warnings-td28107.html
9187
fig, ax = plt.subplots()
9288
X, Y = np.meshgrid(np.arange(10), np.arange(10))
9389
U, V = np.cos(X), np.sin(Y)
94-
with warnings.catch_warnings(record=True) as w:
95-
ax.quiver(U, V, headlength=0, headaxislength=0)
96-
fig.canvas.draw()
97-
assert len(w) == 0
90+
ax.quiver(U, V, headlength=0, headaxislength=0)
91+
fig.canvas.draw()
92+
assert len(recwarn) == 0
9893

9994

10095
@image_comparison(['quiver_animated_test_image.png'])

0 commit comments

Comments
 (0)