From 1df63cc9dbf54864aed8b4f3d5631c3b6b80a3ee Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Mon, 31 Jan 2022 01:03:32 +0100 Subject: [PATCH 1/3] better warning filter for assert_* --- xarray/testing.py | 3 ++- xarray/tests/test_testing.py | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/xarray/testing.py b/xarray/testing.py index 4369b828daf..55d11878dfb 100644 --- a/xarray/testing.py +++ b/xarray/testing.py @@ -29,7 +29,8 @@ def wrapper(*args, **kwargs): __tracebackhide__ = True with warnings.catch_warnings(): - warnings.simplefilter("always") + # only remove filters that would "error" + warnings.filters = [f for f in warnings.filters if f[0] != "error"] return func(*args, **kwargs) diff --git a/xarray/tests/test_testing.py b/xarray/tests/test_testing.py index 2bde7529d1e..fcb2f1b9f15 100644 --- a/xarray/tests/test_testing.py +++ b/xarray/tests/test_testing.py @@ -155,10 +155,23 @@ def __array__(self): a = WarningVariable("x", [1]) b = WarningVariable("x", [2]) + # elevate warnings to errors + warnings.filterwarnings("error") + with warnings.catch_warnings(record=True) as w: - # elevate warnings to errors - warnings.filterwarnings("error") with pytest.raises(AssertionError): getattr(xr.testing, func)(a, b) assert len(w) > 0 + + # ensure warnings still raise outside of assert_* + with pytest.raises(UserWarning): + warnings.warn("test") + + with warnings.catch_warnings(record=True) as w: + # elevate warnings to errors + warnings.filterwarnings("ignore") + with pytest.raises(AssertionError): + getattr(xr.testing, func)(a, b) + + assert len(w) == 0 From 852524047efc79f0afb1e8666cb804805386e104 Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Mon, 31 Jan 2022 01:26:46 +0100 Subject: [PATCH 2/3] better comments --- xarray/tests/test_testing.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/xarray/tests/test_testing.py b/xarray/tests/test_testing.py index fcb2f1b9f15..114d74c2c39 100644 --- a/xarray/tests/test_testing.py +++ b/xarray/tests/test_testing.py @@ -155,21 +155,21 @@ def __array__(self): a = WarningVariable("x", [1]) b = WarningVariable("x", [2]) - # elevate warnings to errors - warnings.filterwarnings("error") - with warnings.catch_warnings(record=True) as w: + warnings.filterwarnings("error") + # elevate warnings to errors with pytest.raises(AssertionError): getattr(xr.testing, func)(a, b) assert len(w) > 0 - # ensure warnings still raise outside of assert_* - with pytest.raises(UserWarning): - warnings.warn("test") + # ensure warnings still raise outside of assert_* + with pytest.raises(UserWarning): + warnings.warn("test") + # ensure warnings stay ignored in assert_* with warnings.catch_warnings(record=True) as w: - # elevate warnings to errors + # ignore warnings warnings.filterwarnings("ignore") with pytest.raises(AssertionError): getattr(xr.testing, func)(a, b) From c069de8cf1b8b8c6b95f6ad3f110afc194fa677b Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Mon, 31 Jan 2022 01:27:57 +0100 Subject: [PATCH 3/3] fix comment --- xarray/tests/test_testing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_testing.py b/xarray/tests/test_testing.py index 114d74c2c39..ff59aa66fee 100644 --- a/xarray/tests/test_testing.py +++ b/xarray/tests/test_testing.py @@ -156,8 +156,8 @@ def __array__(self): b = WarningVariable("x", [2]) with warnings.catch_warnings(record=True) as w: - warnings.filterwarnings("error") # elevate warnings to errors + warnings.filterwarnings("error") with pytest.raises(AssertionError): getattr(xr.testing, func)(a, b)