From 724a2af17aea2f6d7e0f2fe5cf9a064d1d2792bc Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Mon, 21 Dec 2020 14:34:01 -0500 Subject: [PATCH 1/7] BUG: assert_produces_warning() with None or False doesn't raise --- pandas/_testing.py | 10 +++++----- .../util/test_assert_produces_warning.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index 2d74cc89b8063..de0f7c39ca94e 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -2724,11 +2724,10 @@ class for all warnings. To check that no warning is returned, extra_warnings = [] for actual_warning in w: - if not expected_warning: - continue - expected_warning = cast(Type[Warning], expected_warning) - if issubclass(actual_warning.category, expected_warning): + if expected_warning and issubclass( + actual_warning.category, expected_warning + ): saw_warning = True if check_stacklevel and issubclass( @@ -2763,7 +2762,8 @@ class for all warnings. To check that no warning is returned, f"matching {match}" ) - if raise_on_extra_warnings and extra_warnings: + # if raise_on_extra_warnings and extra_warnings: + if (raise_on_extra_warnings or not expected_warning) and extra_warnings: raise AssertionError( f"Caused unexpected warning(s): {repr(extra_warnings)}" ) diff --git a/pandas/tests/util/test_assert_produces_warning.py b/pandas/tests/util/test_assert_produces_warning.py index 5f87824713175..dbb36e5571136 100644 --- a/pandas/tests/util/test_assert_produces_warning.py +++ b/pandas/tests/util/test_assert_produces_warning.py @@ -152,3 +152,22 @@ def test_right_category_wrong_match_raises(pair_different_warnings): with tm.assert_produces_warning(target_category, match=r"^Match this"): warnings.warn("Do not match it", target_category) warnings.warn("Match this", other_category) + + +def test_none_or_false_expected_warning_raises_on_warning(): + msg = r"Caused unexpected warning\(s\)" + with pytest.raises(AssertionError, match=msg): + with tm.assert_produces_warning(None): + f() + + with pytest.raises(AssertionError, match=msg): + with tm.assert_produces_warning(False): + f() + + +def test_none_or_false_expected_warning_no_raise_on_pass(): + with tm.assert_produces_warning(None): + pass + + with tm.assert_produces_warning(False): + pass From 0a625f3254c5e87e020cd4214f19039926c9f7fa Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Mon, 21 Dec 2020 14:38:24 -0500 Subject: [PATCH 2/7] Remove commented line --- pandas/_testing.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index de0f7c39ca94e..28ecaa2a62c70 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -2762,7 +2762,6 @@ class for all warnings. To check that no warning is returned, f"matching {match}" ) - # if raise_on_extra_warnings and extra_warnings: if (raise_on_extra_warnings or not expected_warning) and extra_warnings: raise AssertionError( f"Caused unexpected warning(s): {repr(extra_warnings)}" From 2e1d58ae338bc2340e5915739ed3f85326c2e82f Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Mon, 21 Dec 2020 14:46:58 -0500 Subject: [PATCH 3/7] Honor extra warnings arg --- pandas/_testing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index 28ecaa2a62c70..cad95ca571258 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -2762,7 +2762,7 @@ class for all warnings. To check that no warning is returned, f"matching {match}" ) - if (raise_on_extra_warnings or not expected_warning) and extra_warnings: + if raise_on_extra_warnings and extra_warnings: raise AssertionError( f"Caused unexpected warning(s): {repr(extra_warnings)}" ) From aed34656bd58856b6512f666f755b144fae90e47 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Mon, 21 Dec 2020 14:51:21 -0500 Subject: [PATCH 4/7] Add test for None with false raise on extra --- pandas/tests/util/test_assert_produces_warning.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/tests/util/test_assert_produces_warning.py b/pandas/tests/util/test_assert_produces_warning.py index dbb36e5571136..42c8867315fcb 100644 --- a/pandas/tests/util/test_assert_produces_warning.py +++ b/pandas/tests/util/test_assert_produces_warning.py @@ -163,6 +163,9 @@ def test_none_or_false_expected_warning_raises_on_warning(): with pytest.raises(AssertionError, match=msg): with tm.assert_produces_warning(False): f() + # If not raising on extra errors, this shouldn't raise + with tm.assert_produces_warning(None, raise_on_extra_warnings=False): + f() def test_none_or_false_expected_warning_no_raise_on_pass(): From c82b859568dfd0a3ef9aecf502a6f6eacd78d2dd Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Mon, 21 Dec 2020 15:05:02 -0500 Subject: [PATCH 5/7] Refactor added tests --- .../util/test_assert_produces_warning.py | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/pandas/tests/util/test_assert_produces_warning.py b/pandas/tests/util/test_assert_produces_warning.py index 42c8867315fcb..296fa3b6cf537 100644 --- a/pandas/tests/util/test_assert_produces_warning.py +++ b/pandas/tests/util/test_assert_produces_warning.py @@ -154,23 +154,18 @@ def test_right_category_wrong_match_raises(pair_different_warnings): warnings.warn("Match this", other_category) -def test_none_or_false_expected_warning_raises_on_warning(): - msg = r"Caused unexpected warning\(s\)" - with pytest.raises(AssertionError, match=msg): - with tm.assert_produces_warning(None): - f() - - with pytest.raises(AssertionError, match=msg): - with tm.assert_produces_warning(False): +@pytest.mark.parametrize("false_or_none", [False, None]) +class TestFalseOrNoneExpectedWarning: + def test_raise_on_warning(self, false_or_none): + msg = r"Caused unexpected warning\(s\)" + with pytest.raises(AssertionError, match=msg): + with tm.assert_produces_warning(false_or_none): + f() + + def test_no_raise_without_warning(self, false_or_none): + with tm.assert_produces_warning(false_or_none): + pass + + def test_no_raise_with_false_raise_on_extra(self, false_or_none): + with tm.assert_produces_warning(false_or_none, raise_on_extra_warnings=False): f() - # If not raising on extra errors, this shouldn't raise - with tm.assert_produces_warning(None, raise_on_extra_warnings=False): - f() - - -def test_none_or_false_expected_warning_no_raise_on_pass(): - with tm.assert_produces_warning(None): - pass - - with tm.assert_produces_warning(False): - pass From 6f3c8ff867c9167c48f3cb61e4d5199fdef7cd79 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Mon, 21 Dec 2020 21:47:04 -0500 Subject: [PATCH 6/7] xfail failing tests --- pandas/tests/arithmetic/test_timedelta64.py | 1 + pandas/tests/frame/test_arithmetic.py | 1 + pandas/tests/indexes/test_common.py | 1 + 3 files changed, 3 insertions(+) diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index 092a3f0d4402f..110f55a49eea3 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -543,6 +543,7 @@ def test_tda_add_sub_index(self): expected = tdi - tdi tm.assert_index_equal(result, expected) + @pytest.mark.xfail(reason="GH38630", strict=False) def test_tda_add_dt64_object_array(self, box_with_array, tz_naive_fixture): # Result should be cast back to DatetimeArray box = box_with_array diff --git a/pandas/tests/frame/test_arithmetic.py b/pandas/tests/frame/test_arithmetic.py index e5ec3c5641bd2..e6d1cd5f47d8d 100644 --- a/pandas/tests/frame/test_arithmetic.py +++ b/pandas/tests/frame/test_arithmetic.py @@ -821,6 +821,7 @@ def test_frame_with_frame_reindex(self): (np.datetime64(20, "ns"), " Date: Mon, 21 Dec 2020 22:46:43 -0500 Subject: [PATCH 7/7] xfail parser test --- pandas/tests/io/parser/test_common.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/io/parser/test_common.py b/pandas/tests/io/parser/test_common.py index df20db0c7ee84..ce3557e098bfd 100644 --- a/pandas/tests/io/parser/test_common.py +++ b/pandas/tests/io/parser/test_common.py @@ -1136,6 +1136,7 @@ def test_parse_integers_above_fp_precision(all_parsers): tm.assert_frame_equal(result, expected) +@pytest.mark.xfail(reason="GH38630, sometimes gives ResourceWarning", strict=False) def test_chunks_have_consistent_numerical_type(all_parsers): parser = all_parsers integers = [str(i) for i in range(499999)]