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

Skip to content

Commit c81cddb

Browse files
committed
Catch deprecations in tests
1 parent a454b15 commit c81cddb

File tree

1 file changed

+75
-58
lines changed

1 file changed

+75
-58
lines changed

lib/matplotlib/tests/test_mlab.py

Lines changed: 75 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import matplotlib.mlab as mlab
1717
import matplotlib.cbook as cbook
18+
from matplotlib.cbook.deprecation import MatplotlibDeprecationWarning
1819

1920

2021
try:
@@ -24,12 +25,21 @@
2425
HAS_NATGRID = False
2526

2627

28+
'''
29+
A lot of mlab.py has been deprecated in Matplotlib 2.2 and is scheduled for
30+
removal in the future. The tests that use deprecated methods have a block
31+
to catch the deprecation warning, and can be removed with the mlab code is
32+
removed.
33+
'''
34+
35+
2736
def test_colinear_pca():
28-
a = mlab.PCA._get_colinear()
29-
pca = mlab.PCA(a)
37+
with pytest.warns(MatplotlibDeprecationWarning):
38+
a = mlab.PCA._get_colinear()
39+
pca = mlab.PCA(a)
3040

31-
assert_allclose(pca.fracs[2:], 0., atol=1e-8)
32-
assert_allclose(pca.Y[:, 2:], 0., atol=1e-8)
41+
assert_allclose(pca.fracs[2:], 0., atol=1e-8)
42+
assert_allclose(pca.Y[:, 2:], 0., atol=1e-8)
3343

3444

3545
@pytest.mark.parametrize('input', [
@@ -53,8 +63,9 @@ def test_colinear_pca():
5363
[0, 75, 100],
5464
])
5565
def test_prctile(input, percentile):
56-
assert_allclose(mlab.prctile(input, percentile),
57-
np.percentile(input, percentile))
66+
with pytest.warns(MatplotlibDeprecationWarning):
67+
assert_allclose(mlab.prctile(input, percentile),
68+
np.percentile(input, percentile))
5869

5970

6071
@pytest.mark.parametrize('xmin, xmax, N', [
@@ -69,10 +80,11 @@ def test_prctile(input, percentile):
6980
'single',
7081
])
7182
def test_logspace(xmin, xmax, N):
72-
res = mlab.logspace(xmin, xmax, N)
73-
targ = np.logspace(np.log10(xmin), np.log10(xmax), N)
74-
assert_allclose(targ, res)
75-
assert res.size == N
83+
with pytest.warns(MatplotlibDeprecationWarning):
84+
res = mlab.logspace(xmin, xmax, N)
85+
targ = np.logspace(np.log10(xmin), np.log10(xmax), N)
86+
assert_allclose(targ, res)
87+
assert res.size == N
7688

7789

7890
class TestStride(object):
@@ -210,40 +222,43 @@ def tempcsv():
210222

211223

212224
def test_recarray_csv_roundtrip(tempcsv):
213-
expected = np.recarray((99,),
214-
[(str('x'), float),
215-
(str('y'), float),
216-
(str('t'), float)])
217-
# initialising all values: uninitialised memory sometimes produces
218-
# floats that do not round-trip to string and back.
219-
expected['x'][:] = np.linspace(-1e9, -1, 99)
220-
expected['y'][:] = np.linspace(1, 1e9, 99)
221-
expected['t'][:] = np.linspace(0, 0.01, 99)
222-
223-
mlab.rec2csv(expected, tempcsv)
224-
tempcsv.seek(0)
225-
actual = mlab.csv2rec(tempcsv)
226-
227-
assert_allclose(expected['x'], actual['x'])
228-
assert_allclose(expected['y'], actual['y'])
229-
assert_allclose(expected['t'], actual['t'])
225+
with pytest.warns(MatplotlibDeprecationWarning):
226+
expected = np.recarray((99,),
227+
[(str('x'), float),
228+
(str('y'), float),
229+
(str('t'), float)])
230+
# initialising all values: uninitialised memory sometimes produces
231+
# floats that do not round-trip to string and back.
232+
expected['x'][:] = np.linspace(-1e9, -1, 99)
233+
expected['y'][:] = np.linspace(1, 1e9, 99)
234+
expected['t'][:] = np.linspace(0, 0.01, 99)
235+
236+
mlab.rec2csv(expected, tempcsv)
237+
tempcsv.seek(0)
238+
actual = mlab.csv2rec(tempcsv)
239+
240+
assert_allclose(expected['x'], actual['x'])
241+
assert_allclose(expected['y'], actual['y'])
242+
assert_allclose(expected['t'], actual['t'])
230243

231244

232245
def test_rec2csv_bad_shape_ValueError(tempcsv):
233-
bad = np.recarray((99, 4), [(str('x'), float),
234-
(str('y'), float)])
246+
with pytest.warns(MatplotlibDeprecationWarning):
247+
bad = np.recarray((99, 4), [(str('x'), float),
248+
(str('y'), float)])
235249

236-
# the bad recarray should trigger a ValueError for having ndim > 1.
237-
with pytest.raises(ValueError):
238-
mlab.rec2csv(bad, tempcsv)
250+
# the bad recarray should trigger a ValueError for having ndim > 1.
251+
with pytest.raises(ValueError):
252+
mlab.rec2csv(bad, tempcsv)
239253

240254

241255
def test_csv2rec_names_with_comments(tempcsv):
242-
tempcsv.write('# comment\n1,2,3\n4,5,6\n')
243-
tempcsv.seek(0)
244-
array = mlab.csv2rec(tempcsv, names='a,b,c')
245-
assert len(array) == 2
246-
assert len(array.dtype) == 3
256+
with pytest.warns(MatplotlibDeprecationWarning):
257+
tempcsv.write('# comment\n1,2,3\n4,5,6\n')
258+
tempcsv.seek(0)
259+
array = mlab.csv2rec(tempcsv, names='a,b,c')
260+
assert len(array) == 2
261+
assert len(array.dtype) == 3
247262

248263

249264
@pytest.mark.parametrize('input, kwargs', [
@@ -267,30 +282,32 @@ def test_csv2rec_names_with_comments(tempcsv):
267282
{'yearfirst': True}),
268283
], ids=['usdate', 'dayfirst', 'yearfirst'])
269284
def test_csv2rec_dates(tempcsv, input, kwargs):
270-
tempcsv.write(input)
271-
expected = [datetime.datetime(2014, 1, 11, 0, 0),
272-
datetime.datetime(1976, 3, 5, 0, 0, 1),
273-
datetime.datetime(1983, 7, 9, 17, 17, 34),
274-
datetime.datetime(2054, 6, 20, 14, 31, 45),
275-
datetime.datetime(2000, 10, 31, 11, 50, 23)]
276-
tempcsv.seek(0)
277-
array = mlab.csv2rec(tempcsv, names='a', **kwargs)
278-
assert_array_equal(array['a'].tolist(), expected)
285+
with pytest.warns(MatplotlibDeprecationWarning):
286+
tempcsv.write(input)
287+
expected = [datetime.datetime(2014, 1, 11, 0, 0),
288+
datetime.datetime(1976, 3, 5, 0, 0, 1),
289+
datetime.datetime(1983, 7, 9, 17, 17, 34),
290+
datetime.datetime(2054, 6, 20, 14, 31, 45),
291+
datetime.datetime(2000, 10, 31, 11, 50, 23)]
292+
tempcsv.seek(0)
293+
array = mlab.csv2rec(tempcsv, names='a', **kwargs)
294+
assert_array_equal(array['a'].tolist(), expected)
279295

280296

281297
def test_rec2txt_basic():
282-
# str() calls around field names necessary b/c as of numpy 1.11
283-
# dtype doesn't like unicode names (caused by unicode_literals import)
284-
a = np.array([(1.0, 2, 'foo', 'bing'),
285-
(2.0, 3, 'bar', 'blah')],
286-
dtype=np.dtype([(str('x'), np.float32),
287-
(str('y'), np.int8),
288-
(str('s'), str, 3),
289-
(str('s2'), str, 4)]))
290-
truth = (' x y s s2\n'
291-
' 1.000 2 foo bing \n'
292-
' 2.000 3 bar blah ').splitlines()
293-
assert mlab.rec2txt(a).splitlines() == truth
298+
with pytest.warns(MatplotlibDeprecationWarning):
299+
# str() calls around field names necessary b/c as of numpy 1.11
300+
# dtype doesn't like unicode names (caused by unicode_literals import)
301+
a = np.array([(1.0, 2, 'foo', 'bing'),
302+
(2.0, 3, 'bar', 'blah')],
303+
dtype=np.dtype([(str('x'), np.float32),
304+
(str('y'), np.int8),
305+
(str('s'), str, 3),
306+
(str('s2'), str, 4)]))
307+
truth = (' x y s s2\n'
308+
' 1.000 2 foo bing \n'
309+
' 2.000 3 bar blah ').splitlines()
310+
assert mlab.rec2txt(a).splitlines() == truth
294311

295312

296313
class TestWindow(object):

0 commit comments

Comments
 (0)