Skip to content

Commit 1253214

Browse files
authored
tests don't use pytest.warns(None) (#6890)
* test_backends don't use pytest.warns(None) * remove unrelated line * fix warnings filters * another one
1 parent c3d45b3 commit 1253214

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

xarray/tests/test_backends.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2697,7 +2697,6 @@ def create_store(self):
26972697
with create_tmp_file() as tmp_file:
26982698
yield backends.H5NetCDFStore.open(tmp_file, "w")
26992699

2700-
@pytest.mark.filterwarnings("ignore:complex dtypes are supported by h5py")
27012700
@pytest.mark.parametrize(
27022701
"invalid_netcdf, warntype, num_warns",
27032702
[
@@ -2719,7 +2718,7 @@ def create_store(self):
27192718
def test_complex(self, invalid_netcdf, warntype, num_warns):
27202719
expected = Dataset({"x": ("y", np.ones(5) + 1j * np.ones(5))})
27212720
save_kwargs = {"invalid_netcdf": invalid_netcdf}
2722-
with pytest.warns(warntype) as record:
2721+
with warnings.catch_warnings(record=True) as record:
27232722
with self.roundtrip(expected, save_kwargs=save_kwargs) as actual:
27242723
assert_equal(expected, actual)
27252724

@@ -2747,6 +2746,7 @@ def test_complex_error(self, invalid_netcdf):
27472746
with self.roundtrip(expected, save_kwargs=save_kwargs) as actual:
27482747
assert_equal(expected, actual)
27492748

2749+
@pytest.mark.filterwarnings("ignore:You are writing invalid netcdf features")
27502750
def test_numpy_bool_(self):
27512751
# h5netcdf loads booleans as numpy.bool_, this type needs to be supported
27522752
# when writing invalid_netcdf datasets in order to support a roundtrip

xarray/tests/test_backends_file_manager.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from xarray.backends.file_manager import CachingFileManager
1111
from xarray.backends.lru_cache import LRUCache
1212
from xarray.core.options import set_options
13+
from xarray.tests import assert_no_warnings
1314

1415

1516
@pytest.fixture(params=[1, 2, 3, None])
@@ -38,8 +39,8 @@ def test_file_manager_mock_write(file_cache) -> None:
3839
lock.__enter__.assert_has_calls([mock.call(), mock.call()])
3940

4041

41-
@pytest.mark.parametrize("expected_warning", [None, RuntimeWarning])
42-
def test_file_manager_autoclose(expected_warning) -> None:
42+
@pytest.mark.parametrize("warn_for_unclosed_files", [True, False])
43+
def test_file_manager_autoclose(warn_for_unclosed_files) -> None:
4344
mock_file = mock.Mock()
4445
opener = mock.Mock(return_value=mock_file)
4546
cache: dict = {}
@@ -48,8 +49,14 @@ def test_file_manager_autoclose(expected_warning) -> None:
4849
manager.acquire()
4950
assert cache
5051

51-
with set_options(warn_for_unclosed_files=expected_warning is not None):
52-
with pytest.warns(expected_warning):
52+
# can no longer use pytest.warns(None)
53+
if warn_for_unclosed_files:
54+
ctx = pytest.warns(RuntimeWarning)
55+
else:
56+
ctx = assert_no_warnings()
57+
58+
with set_options(warn_for_unclosed_files=warn_for_unclosed_files):
59+
with ctx:
5360
del manager
5461
gc.collect()
5562

0 commit comments

Comments
 (0)