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

Skip to content

Commit fd399aa

Browse files
committed
TST: do not use "ignore" to filter warnings
When a warning is ignored (or raised once) in python, the warnings module will tag on a `__warningregistry__` dictionary to be able to filter these warnings in the future. This is tagged on to the current context, causing leakage to later calls (this is a bit more complex, since where the registry ends up depends on the layers between the original caller and warner). In short, tests should typically not use ignore but catch the warnings to avoid changing the user experience (or errors on duplicate test runs). Fixes an error on duplicate test runs (does not remove all "ignores" which may change behaviour outside tests). Closes gh-4340
1 parent 8997167 commit fd399aa

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

numpy/lib/tests/test_function_base.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,8 +1222,8 @@ def test_xy(self):
12221222
assert_allclose(np.corrcoef(x, y), np.array([[1., -1.j], [1.j, 1.]]))
12231223

12241224
def test_empty(self):
1225-
with warnings.catch_warnings():
1226-
warnings.simplefilter('ignore', RuntimeWarning)
1225+
with warnings.catch_warnings(record=True):
1226+
warnings.simplefilter('always', RuntimeWarning)
12271227
assert_array_equal(corrcoef(np.array([])), np.nan)
12281228
assert_array_equal(corrcoef(np.array([]).reshape(0, 2)),
12291229
np.array([]).reshape(0, 0))
@@ -1232,8 +1232,8 @@ def test_empty(self):
12321232

12331233
def test_wrong_ddof(self):
12341234
x = np.array([[0, 2], [1, 1], [2, 0]]).T
1235-
with warnings.catch_warnings():
1236-
warnings.simplefilter('ignore', RuntimeWarning)
1235+
with warnings.catch_warnings(record=True):
1236+
warnings.simplefilter('always', RuntimeWarning)
12371237
assert_array_equal(corrcoef(x, ddof=5),
12381238
np.array([[np.nan, np.nan], [np.nan, np.nan]]))
12391239

@@ -1253,8 +1253,8 @@ def test_xy(self):
12531253
assert_allclose(cov(x, y), np.array([[1., -1.j], [1.j, 1.]]))
12541254

12551255
def test_empty(self):
1256-
with warnings.catch_warnings():
1257-
warnings.simplefilter('ignore', RuntimeWarning)
1256+
with warnings.catch_warnings(record=True):
1257+
warnings.simplefilter('always', RuntimeWarning)
12581258
assert_array_equal(cov(np.array([])), np.nan)
12591259
assert_array_equal(cov(np.array([]).reshape(0, 2)),
12601260
np.array([]).reshape(0, 0))
@@ -1263,8 +1263,8 @@ def test_empty(self):
12631263

12641264
def test_wrong_ddof(self):
12651265
x = np.array([[0, 2], [1, 1], [2, 0]]).T
1266-
with warnings.catch_warnings():
1267-
warnings.simplefilter('ignore', RuntimeWarning)
1266+
with warnings.catch_warnings(record=True):
1267+
warnings.simplefilter('always', RuntimeWarning)
12681268
assert_array_equal(cov(x, ddof=5),
12691269
np.array([[np.inf, -np.inf], [-np.inf, np.inf]]))
12701270

numpy/lib/tests/test_nanfunctions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ def test_mutation(self):
130130
def test_result_values(self):
131131
for f, fcmp in zip(self.nanfuncs, [np.greater, np.less]):
132132
for row in _ndat:
133-
with warnings.catch_warnings():
134-
warnings.simplefilter('ignore')
133+
with warnings.catch_warnings(record=True):
134+
warnings.simplefilter('always')
135135
ind = f(row)
136136
val = row[ind]
137137
# comparing with NaN is tricky as the result

0 commit comments

Comments
 (0)