From 90bc3d2c0e7d4f3ba210af4fc4456885f5f167ef Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 6 Jan 2023 14:16:24 -0800 Subject: [PATCH 1/6] Remove old deprecations, make assert_produces_warning check when exceptions are raised --- pandas/_libs/tslibs/offsets.pyx | 1 - pandas/_testing/_warnings.py | 32 ++++++++++--------- pandas/core/arrays/datetimes.py | 4 +-- pandas/tests/extension/test_sparse.py | 3 +- pandas/tests/frame/indexing/test_getitem.py | 6 ++-- pandas/tests/frame/indexing/test_setitem.py | 3 +- pandas/tests/frame/methods/test_reindex.py | 9 ++---- .../tests/groupby/transform/test_transform.py | 6 ++-- 8 files changed, 28 insertions(+), 36 deletions(-) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index b59d17321d8bf..cf02b0cb396bd 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -389,7 +389,6 @@ cdef class BaseOffset: _attributes = tuple(["n", "normalize"]) _use_relativedelta = False _adjust_dst = True - _deprecations = frozenset(["isAnchored", "onOffset"]) # cdef readonly: # int64_t n diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py index f42004bdfdef3..fe5e2206ddf99 100644 --- a/pandas/_testing/_warnings.py +++ b/pandas/_testing/_warnings.py @@ -87,22 +87,24 @@ class for all warnings. To raise multiple types of exceptions, with warnings.catch_warnings(record=True) as w: warnings.simplefilter(filter_level) - yield w - - if expected_warning: - expected_warning = cast(Type[Warning], expected_warning) - _assert_caught_expected_warning( - caught_warnings=w, - expected_warning=expected_warning, - match=match, - check_stacklevel=check_stacklevel, - ) + try: + yield w + finally: + + if expected_warning: + expected_warning = cast(Type[Warning], expected_warning) + _assert_caught_expected_warning( + caught_warnings=w, + expected_warning=expected_warning, + match=match, + check_stacklevel=check_stacklevel, + ) - if raise_on_extra_warnings: - _assert_caught_no_extra_warnings( - caught_warnings=w, - expected_warning=expected_warning, - ) + if raise_on_extra_warnings: + _assert_caught_no_extra_warnings( + caught_warnings=w, + expected_warning=expected_warning, + ) def maybe_produces_warning(warning: type[Warning], condition: bool, **kwargs): diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 01584a66f424b..8ad2ed0681074 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -2186,8 +2186,8 @@ def objects_to_datetime64ns( def maybe_convert_dtype(data, copy: bool, tz: tzinfo | None = None): """ - Convert data based on dtype conventions, issuing deprecation warnings - or errors where appropriate. + Convert data based on dtype conventions, issuing + errors where appropriate. Parameters ---------- diff --git a/pandas/tests/extension/test_sparse.py b/pandas/tests/extension/test_sparse.py index 022e5cb764e14..9c5b3426246a8 100644 --- a/pandas/tests/extension/test_sparse.py +++ b/pandas/tests/extension/test_sparse.py @@ -480,8 +480,7 @@ class TestParsing(BaseSparseTests, base.BaseParsingTests): def test_EA_types(self, engine, data): expected_msg = r".*must implement _from_sequence_of_strings.*" with pytest.raises(NotImplementedError, match=expected_msg): - with tm.assert_produces_warning(FutureWarning, match="astype from"): - super().test_EA_types(engine, data) + super().test_EA_types(engine, data) class TestNoNumericAccumulations(base.BaseAccumulateTests): diff --git a/pandas/tests/frame/indexing/test_getitem.py b/pandas/tests/frame/indexing/test_getitem.py index 6427c68dc76b5..ad65ea4ee3fed 100644 --- a/pandas/tests/frame/indexing/test_getitem.py +++ b/pandas/tests/frame/indexing/test_getitem.py @@ -156,8 +156,7 @@ def test_getitem_listlike(self, idx_type, levels, float_frame): idx = idx_type(keys + [missing]) with pytest.raises(KeyError, match="not in index"): - with tm.assert_produces_warning(FutureWarning): - frame[idx] + frame[idx] def test_getitem_iloc_generator(self): # GH#39614 @@ -357,8 +356,7 @@ def test_getitem_boolean_frame_unaligned_with_duplicate_columns(self, df_dup_col df = df_dup_cols msg = "cannot reindex on an axis with duplicate labels" with pytest.raises(ValueError, match=msg): - with tm.assert_produces_warning(FutureWarning, match="non-unique"): - df[df.A > 6] + df[df.A > 6] def test_getitem_boolean_series_with_duplicate_columns(self, df_dup_cols): # boolean indexing diff --git a/pandas/tests/frame/indexing/test_setitem.py b/pandas/tests/frame/indexing/test_setitem.py index 7b3d0382d7b01..d8d626b3af84a 100644 --- a/pandas/tests/frame/indexing/test_setitem.py +++ b/pandas/tests/frame/indexing/test_setitem.py @@ -85,8 +85,7 @@ def test_setitem_error_msmgs(self): ) msg = "cannot reindex on an axis with duplicate labels" with pytest.raises(ValueError, match=msg): - with tm.assert_produces_warning(FutureWarning, match="non-unique"): - df["newcol"] = ser + df["newcol"] = ser # GH 4107, more descriptive error message df = DataFrame(np.random.randint(0, 2, (4, 4)), columns=["a", "b", "c", "d"]) diff --git a/pandas/tests/frame/methods/test_reindex.py b/pandas/tests/frame/methods/test_reindex.py index b30bd69806a94..b045e57e0e88f 100644 --- a/pandas/tests/frame/methods/test_reindex.py +++ b/pandas/tests/frame/methods/test_reindex.py @@ -808,8 +808,7 @@ def test_reindex_dups(self): # reindex fails msg = "cannot reindex on an axis with duplicate labels" with pytest.raises(ValueError, match=msg): - with tm.assert_produces_warning(FutureWarning, match="non-unique"): - df.reindex(index=list(range(len(df)))) + df.reindex(index=list(range(len(df)))) def test_reindex_with_duplicate_columns(self): @@ -819,11 +818,9 @@ def test_reindex_with_duplicate_columns(self): ) msg = "cannot reindex on an axis with duplicate labels" with pytest.raises(ValueError, match=msg): - with tm.assert_produces_warning(FutureWarning, match="non-unique"): - df.reindex(columns=["bar"]) + df.reindex(columns=["bar"]) with pytest.raises(ValueError, match=msg): - with tm.assert_produces_warning(FutureWarning, match="non-unique"): - df.reindex(columns=["bar", "foo"]) + df.reindex(columns=["bar", "foo"]) def test_reindex_axis_style(self): # https://github.com/pandas-dev/pandas/issues/12392 diff --git a/pandas/tests/groupby/transform/test_transform.py b/pandas/tests/groupby/transform/test_transform.py index 8f1d52c2ea03d..ee041d5714c2a 100644 --- a/pandas/tests/groupby/transform/test_transform.py +++ b/pandas/tests/groupby/transform/test_transform.py @@ -1418,7 +1418,7 @@ def test_null_group_str_reducer_series(request, dropna, reduction_func): tm.assert_series_equal(result, expected) -def test_null_group_str_transformer_series(request, dropna, transformation_func): +def test_null_group_str_transformer_series(dropna, transformation_func): # GH 17093 ser = Series([1, 2, 2], index=[1, 2, 3]) args = get_groupby_method_args(transformation_func, ser) @@ -1439,9 +1439,7 @@ def test_null_group_str_transformer_series(request, dropna, transformation_func) buffer.append(Series([np.nan], index=[3], dtype=dtype)) expected = concat(buffer) - warn = FutureWarning if transformation_func in ("backfill", "pad") else None - msg = f"{transformation_func} is deprecated" - with tm.assert_produces_warning(warn, match=msg): + with tm.assert_produces_warning(None): result = gb.transform(transformation_func, *args) tm.assert_equal(result, expected) From 83c3d72aa8d2dd2817bca03c62eaf3a10210d958 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 6 Jan 2023 17:00:21 -0800 Subject: [PATCH 2/6] cleanup more tests, add test for change --- pandas/_testing/_warnings.py | 2 -- pandas/tests/frame/methods/test_reindex.py | 3 +- pandas/tests/frame/methods/test_sort_index.py | 8 ++--- pandas/tests/groupby/test_categorical.py | 5 +-- .../tests/groupby/transform/test_transform.py | 5 +-- pandas/tests/indexes/interval/test_base.py | 3 +- pandas/tests/indexes/multi/test_reindex.py | 3 +- pandas/tests/indexes/test_common.py | 10 +----- pandas/tests/indexing/test_indexing.py | 3 +- pandas/tests/io/parser/test_parse_dates.py | 4 +-- .../reshape/concat/test_append_common.py | 36 +++++++------------ pandas/tests/series/indexing/test_getitem.py | 1 - .../series/methods/test_convert_dtypes.py | 12 +------ .../util/test_assert_produces_warning.py | 12 +++++++ 14 files changed, 37 insertions(+), 70 deletions(-) diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py index fe5e2206ddf99..69ef57d9f450d 100644 --- a/pandas/_testing/_warnings.py +++ b/pandas/_testing/_warnings.py @@ -90,7 +90,6 @@ class for all warnings. To raise multiple types of exceptions, try: yield w finally: - if expected_warning: expected_warning = cast(Type[Warning], expected_warning) _assert_caught_expected_warning( @@ -99,7 +98,6 @@ class for all warnings. To raise multiple types of exceptions, match=match, check_stacklevel=check_stacklevel, ) - if raise_on_extra_warnings: _assert_caught_no_extra_warnings( caught_warnings=w, diff --git a/pandas/tests/frame/methods/test_reindex.py b/pandas/tests/frame/methods/test_reindex.py index b045e57e0e88f..eaf417e6b6ca9 100644 --- a/pandas/tests/frame/methods/test_reindex.py +++ b/pandas/tests/frame/methods/test_reindex.py @@ -1092,8 +1092,7 @@ def test_reindex_with_categoricalindex(self): # passed duplicate indexers are not allowed msg = "cannot reindex on an axis with duplicate labels" with pytest.raises(ValueError, match=msg): - with tm.assert_produces_warning(FutureWarning, match="non-unique"): - df2.reindex(["a", "b"]) + df2.reindex(["a", "b"]) # args NotImplemented ATM msg = r"argument {} is not implemented for CategoricalIndex\.reindex" diff --git a/pandas/tests/frame/methods/test_sort_index.py b/pandas/tests/frame/methods/test_sort_index.py index 806abc1e7c012..9e4eb78428802 100644 --- a/pandas/tests/frame/methods/test_sort_index.py +++ b/pandas/tests/frame/methods/test_sort_index.py @@ -44,14 +44,12 @@ def test_sort_index_and_reconstruction_doc_example(self): assert result.index.is_monotonic_increasing tm.assert_frame_equal(result, expected) - # FIXME: the FutureWarning is issued on a setitem-with-expansion - # which will *not* change behavior, so should not get a warning. - @pytest.mark.filterwarnings("ignore:.*will attempt to set.*:FutureWarning") def test_sort_index_non_existent_label_multiindex(self): # GH#12261 df = DataFrame(0, columns=[], index=MultiIndex.from_product([[], []])) - df.loc["b", "2"] = 1 - df.loc["a", "3"] = 1 + with tm.assert_produces_warning(None): + df.loc["b", "2"] = 1 + df.loc["a", "3"] = 1 result = df.sort_index().index.is_monotonic_increasing assert result is True diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index 0fabdf84e5e86..e6f0705c2c647 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -1893,10 +1893,7 @@ def test_category_order_transformer( df = df.set_index(keys) args = get_groupby_method_args(transformation_func, df) gb = df.groupby(keys, as_index=as_index, sort=sort, observed=observed) - msg = "is deprecated and will be removed in a future version" - warn = FutureWarning if transformation_func == "tshift" else None - with tm.assert_produces_warning(warn, match=msg): - op_result = getattr(gb, transformation_func)(*args) + op_result = getattr(gb, transformation_func)(*args) result = op_result.index.get_level_values("a").categories expected = Index([1, 4, 3, 2]) tm.assert_index_equal(result, expected) diff --git a/pandas/tests/groupby/transform/test_transform.py b/pandas/tests/groupby/transform/test_transform.py index 3d9af55f02059..fedc219b42681 100644 --- a/pandas/tests/groupby/transform/test_transform.py +++ b/pandas/tests/groupby/transform/test_transform.py @@ -1362,10 +1362,7 @@ def test_null_group_str_transformer(request, dropna, transformation_func): # ngroup/cumcount always returns a Series as it counts the groups, not values expected = expected["B"].rename(None) - warn = FutureWarning if transformation_func in ("backfill", "pad") else None - msg = f"{transformation_func} is deprecated" - with tm.assert_produces_warning(warn, match=msg): - result = gb.transform(transformation_func, *args) + result = gb.transform(transformation_func, *args) tm.assert_equal(result, expected) diff --git a/pandas/tests/indexes/interval/test_base.py b/pandas/tests/indexes/interval/test_base.py index c44303aa2c862..de9d8be21ae60 100644 --- a/pandas/tests/indexes/interval/test_base.py +++ b/pandas/tests/indexes/interval/test_base.py @@ -61,8 +61,7 @@ def test_getitem_2d_deprecated(self, simple_index): # GH#30588 multi-dim indexing is deprecated, but raising is also acceptable idx = simple_index with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"): - with tm.assert_produces_warning(FutureWarning): - idx[:, None] + idx[:, None] with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"): # GH#44051 idx[True] diff --git a/pandas/tests/indexes/multi/test_reindex.py b/pandas/tests/indexes/multi/test_reindex.py index 8cf729454c15a..77a527134b79f 100644 --- a/pandas/tests/indexes/multi/test_reindex.py +++ b/pandas/tests/indexes/multi/test_reindex.py @@ -106,8 +106,7 @@ def test_reindex_non_unique(): msg = "cannot handle a non-unique multi-index!" with pytest.raises(ValueError, match=msg): - with tm.assert_produces_warning(FutureWarning, match="non-unique"): - a.reindex(new_idx) + a.reindex(new_idx) @pytest.mark.parametrize("values", [[["a"], ["x"]], [[], []]]) diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index cc88390de2ab6..1476a93ea0a11 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -23,7 +23,6 @@ import pandas as pd from pandas import ( CategoricalIndex, - DatetimeIndex, MultiIndex, PeriodIndex, RangeIndex, @@ -389,14 +388,7 @@ def test_astype_preserves_name(self, index, dtype): index.name = "idx" warn = None - if ( - isinstance(index, DatetimeIndex) - and index.tz is not None - and dtype == "datetime64[ns]" - ): - # This astype is deprecated in favor of tz_localize - warn = FutureWarning - elif index.dtype.kind == "c" and dtype in ["float64", "int64", "uint64"]: + if index.dtype.kind == "c" and dtype in ["float64", "int64", "uint64"]: # imaginary components discarded warn = np.ComplexWarning diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index 35da972dd1a81..8ebe9f87c81db 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -69,8 +69,7 @@ def test_setitem_ndarray_1d_2(self): msg = "Must have equal len keys and value when setting with an iterable" with pytest.raises(ValueError, match=msg): - with tm.assert_produces_warning(FutureWarning, match="label-based"): - df[2:5] = np.arange(1, 4) * 1j + df[2:5] = np.arange(1, 4) * 1j def test_getitem_ndarray_3d( self, index, frame_or_series, indexer_sli, using_array_manager diff --git a/pandas/tests/io/parser/test_parse_dates.py b/pandas/tests/io/parser/test_parse_dates.py index 6c05f5defe4fb..fc477a899d089 100644 --- a/pandas/tests/io/parser/test_parse_dates.py +++ b/pandas/tests/io/parser/test_parse_dates.py @@ -924,9 +924,7 @@ def test_parse_dates_custom_euro_format(all_parsers, kwargs): tm.assert_frame_equal(df, expected) else: msg = "got an unexpected keyword argument 'day_first'" - with pytest.raises(TypeError, match=msg), tm.assert_produces_warning( - FutureWarning - ): + with pytest.raises(TypeError, match=msg): parser.read_csv( StringIO(data), names=["time", "Q", "NTU"], diff --git a/pandas/tests/reshape/concat/test_append_common.py b/pandas/tests/reshape/concat/test_append_common.py index 938d18be8657a..2d84de8145111 100644 --- a/pandas/tests/reshape/concat/test_append_common.py +++ b/pandas/tests/reshape/concat/test_append_common.py @@ -192,7 +192,6 @@ def test_concatlike_dtypes_coercion(self, item, item2, request): # instead of a list; we have separate dedicated tests for categorical return - warn = None # specify expected dtype if typ1 == "bool" and typ2 in ("int64", "float64"): # series coerces to numeric based on numpy rule @@ -200,12 +199,10 @@ def test_concatlike_dtypes_coercion(self, item, item2, request): exp_series_dtype = typ2 mark = pytest.mark.xfail(reason="GH#39187 casting to object") request.node.add_marker(mark) - warn = FutureWarning elif typ2 == "bool" and typ1 in ("int64", "float64"): exp_series_dtype = typ1 mark = pytest.mark.xfail(reason="GH#39187 casting to object") request.node.add_marker(mark) - warn = FutureWarning elif ( typ1 == "datetime64[ns, US/Eastern]" or typ2 == "datetime64[ns, US/Eastern]" @@ -221,9 +218,8 @@ def test_concatlike_dtypes_coercion(self, item, item2, request): # ----- Index ----- # # index.append - with tm.assert_produces_warning(warn, match="concatenating bool-dtype"): - # GH#39817 - res = Index(vals1).append(Index(vals2)) + # GH#39817 + res = Index(vals1).append(Index(vals2)) exp = Index(exp_data, dtype=exp_index_dtype) tm.assert_index_equal(res, exp) @@ -235,33 +231,27 @@ def test_concatlike_dtypes_coercion(self, item, item2, request): # ----- Series ----- # # series._append - with tm.assert_produces_warning(warn, match="concatenating bool-dtype"): - # GH#39817 - res = Series(vals1)._append(Series(vals2), ignore_index=True) + # GH#39817 + res = Series(vals1)._append(Series(vals2), ignore_index=True) exp = Series(exp_data, dtype=exp_series_dtype) tm.assert_series_equal(res, exp, check_index_type=True) # concat - with tm.assert_produces_warning(warn, match="concatenating bool-dtype"): - # GH#39817 - res = pd.concat([Series(vals1), Series(vals2)], ignore_index=True) + # GH#39817 + res = pd.concat([Series(vals1), Series(vals2)], ignore_index=True) tm.assert_series_equal(res, exp, check_index_type=True) # 3 elements - with tm.assert_produces_warning(warn, match="concatenating bool-dtype"): - # GH#39817 - res = Series(vals1)._append( - [Series(vals2), Series(vals3)], ignore_index=True - ) + # GH#39817 + res = Series(vals1)._append([Series(vals2), Series(vals3)], ignore_index=True) exp = Series(exp_data3, dtype=exp_series_dtype) tm.assert_series_equal(res, exp) - with tm.assert_produces_warning(warn, match="concatenating bool-dtype"): - # GH#39817 - res = pd.concat( - [Series(vals1), Series(vals2), Series(vals3)], - ignore_index=True, - ) + # GH#39817 + res = pd.concat( + [Series(vals1), Series(vals2), Series(vals3)], + ignore_index=True, + ) tm.assert_series_equal(res, exp) def test_concatlike_common_coerce_to_pandas_object(self): diff --git a/pandas/tests/series/indexing/test_getitem.py b/pandas/tests/series/indexing/test_getitem.py index 53c237d2d47bb..b074f30fa3299 100644 --- a/pandas/tests/series/indexing/test_getitem.py +++ b/pandas/tests/series/indexing/test_getitem.py @@ -272,7 +272,6 @@ def test_getitem_slice_2d(self, datetime_series): with pytest.raises(ValueError, match="Multi-dimensional indexing"): datetime_series[:, np.newaxis] - # FutureWarning from NumPy. def test_getitem_median_slice_bug(self): index = date_range("20090415", "20090519", freq="2B") ser = Series(np.random.randn(13), index=index) diff --git a/pandas/tests/series/methods/test_convert_dtypes.py b/pandas/tests/series/methods/test_convert_dtypes.py index dd28f28f89bcb..d91cd6a43daea 100644 --- a/pandas/tests/series/methods/test_convert_dtypes.py +++ b/pandas/tests/series/methods/test_convert_dtypes.py @@ -187,17 +187,7 @@ def test_convert_dtypes( if all(params_dict[key] is val for key, val in zip(spec[::2], spec[1::2])): expected_dtype = dtype - warn2 = None - if ( - hasattr(data, "dtype") - and data.dtype == "M8[ns]" - and isinstance(expected_dtype, pd.DatetimeTZDtype) - ): - # this astype is deprecated in favor of tz_localize - warn2 = FutureWarning - - with tm.assert_produces_warning(warn2): - expected = pd.Series(data, dtype=expected_dtype) + expected = pd.Series(data, dtype=expected_dtype) tm.assert_series_equal(result, expected) # Test that it is a copy diff --git a/pandas/tests/util/test_assert_produces_warning.py b/pandas/tests/util/test_assert_produces_warning.py index 70a95bd7cdb48..953918fe6ed88 100644 --- a/pandas/tests/util/test_assert_produces_warning.py +++ b/pandas/tests/util/test_assert_produces_warning.py @@ -210,3 +210,15 @@ def test_no_raise_without_warning(self, false_or_none): 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() + + +def test_raises_during_exception(): + msg = "Did not see expected warning of class 'UserWarning'" + with pytest.raises(AssertionError, match=msg): + with tm.assert_produces_warning(UserWarning): + raise Exception + + with pytest.raises(AssertionError, match=msg): + with tm.assert_produces_warning(UserWarning): + warnings.warn("FutureWarning", FutureWarning) + raise Exception From 90adce900447f0f14f4cc0124709a3f179297ea4 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 6 Jan 2023 17:16:26 -0800 Subject: [PATCH 3/6] Add more thorough tests --- .../util/test_assert_produces_warning.py | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/pandas/tests/util/test_assert_produces_warning.py b/pandas/tests/util/test_assert_produces_warning.py index 953918fe6ed88..5c27a3ee79d4a 100644 --- a/pandas/tests/util/test_assert_produces_warning.py +++ b/pandas/tests/util/test_assert_produces_warning.py @@ -216,9 +216,26 @@ def test_raises_during_exception(): msg = "Did not see expected warning of class 'UserWarning'" with pytest.raises(AssertionError, match=msg): with tm.assert_produces_warning(UserWarning): - raise Exception + raise ValueError with pytest.raises(AssertionError, match=msg): with tm.assert_produces_warning(UserWarning): warnings.warn("FutureWarning", FutureWarning) - raise Exception + raise IndexError + + msg = "Caused unexpected warning" + with pytest.raises(AssertionError, match=msg): + with tm.assert_produces_warning(None): + warnings.warn("FutureWarning", FutureWarning) + raise SystemError + + +def test_passes_during_exception(): + with pytest.raises(SyntaxError, match="Error"): + with tm.assert_produces_warning(None): + raise SyntaxError("Error") + + with pytest.raises(ValueError, match="Error"): + with tm.assert_produces_warning(FutureWarning, match="FutureWarning"): + warnings.warn("FutureWarning", FutureWarning) + raise ValueError("Error") From e78aeb3594e6f816817851036334bc484e7cdf1e Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 9 Jan 2023 13:44:39 -0800 Subject: [PATCH 4/6] Fix other uncessary warnings --- pandas/tests/apply/test_invalid_arg.py | 2 +- pandas/tests/frame/test_arithmetic.py | 6 +++--- pandas/tests/io/test_stata.py | 3 +-- pandas/tests/test_algos.py | 3 +-- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/pandas/tests/apply/test_invalid_arg.py b/pandas/tests/apply/test_invalid_arg.py index 252eff8f9a823..3f2e8e9ea07a6 100644 --- a/pandas/tests/apply/test_invalid_arg.py +++ b/pandas/tests/apply/test_invalid_arg.py @@ -348,7 +348,7 @@ def test_transform_wont_agg_series(string_series, func): warn = RuntimeWarning if func[0] == "sqrt" else None warn_msg = "invalid value encountered in sqrt" with pytest.raises(ValueError, match=msg): - with tm.assert_produces_warning(warn, match=warn_msg): + with tm.assert_produces_warning(warn, match=warn_msg, check_stacklevel=False): string_series.transform(func) diff --git a/pandas/tests/frame/test_arithmetic.py b/pandas/tests/frame/test_arithmetic.py index 241e2df377af6..c4ecffd4fe984 100644 --- a/pandas/tests/frame/test_arithmetic.py +++ b/pandas/tests/frame/test_arithmetic.py @@ -1067,7 +1067,7 @@ def test_frame_with_frame_reindex(self): ], ids=lambda x: x.__name__, ) - def test_binop_other(self, op, value, dtype, switch_numexpr_min_elements): + def test_binop_other(self, op, value, dtype, switch_numexpr_min_elements, request): skip = { (operator.truediv, "bool"), @@ -1099,10 +1099,10 @@ def test_binop_other(self, op, value, dtype, switch_numexpr_min_elements): msg = None elif dtype == "complex128": msg = "ufunc 'remainder' not supported for the input types" - warn = UserWarning # "evaluating in Python space because ..." elif op is operator.sub: msg = "numpy boolean subtract, the `-` operator, is " - warn = UserWarning # "evaluating in Python space because ..." + if dtype == "bool" and "numexpr" in request.node.callspec.id: + warn = UserWarning # "evaluating in Python space because ..." else: msg = ( f"cannot perform __{op.__name__}__ with this " diff --git a/pandas/tests/io/test_stata.py b/pandas/tests/io/test_stata.py index 530934df72606..2859bf593f0e0 100644 --- a/pandas/tests/io/test_stata.py +++ b/pandas/tests/io/test_stata.py @@ -1725,8 +1725,7 @@ def test_invalid_file_not_written(self, version): r"ordinal not in range\(128\)" ) with pytest.raises(UnicodeEncodeError, match=f"{msg1}|{msg2}"): - with tm.assert_produces_warning(ResourceWarning): - df.to_stata(path) + df.to_stata(path) def test_strl_latin1(self): # GH 23573, correct GSO data to reflect correct size diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 767203838728b..19dd65e6435f7 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -2163,8 +2163,7 @@ def test_int64_add_overflow(): b_mask=np.array([False, True]), ) with pytest.raises(OverflowError, match=msg): - with tm.assert_produces_warning(RuntimeWarning): - algos.checked_add_with_arr(np.array([m, m]), np.array([np.nan, m])) + algos.checked_add_with_arr(np.array([m, m]), np.array([np.nan, m])) # Check that the nan boolean arrays override whether or not # the addition overflows. We don't check the result but just From 80c0cb698abdbbf024eb1d2444bf1a199feca273 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 9 Jan 2023 16:44:05 -0800 Subject: [PATCH 5/6] Fix min version pyarrow tests --- pandas/tests/extension/test_arrow.py | 20 +++++++++++++++----- pandas/tests/frame/test_arithmetic.py | 6 +++++- pandas/tests/series/test_constructors.py | 4 ++-- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py index c02fa0aecdacc..dd45d0ee53a39 100644 --- a/pandas/tests/extension/test_arrow.py +++ b/pandas/tests/extension/test_arrow.py @@ -527,7 +527,9 @@ def test_groupby_extension_transform(self, data_for_grouping, request): ) ) with tm.maybe_produces_warning( - PerformanceWarning, pa_version_under7p0, check_stacklevel=False + PerformanceWarning, + pa_version_under7p0 and not pa.types.is_duration(pa_dtype), + check_stacklevel=False, ): super().test_groupby_extension_transform(data_for_grouping) @@ -543,7 +545,9 @@ def test_groupby_extension_apply( ) ) with tm.maybe_produces_warning( - PerformanceWarning, pa_version_under7p0, check_stacklevel=False + PerformanceWarning, + pa_version_under7p0 and not pa.types.is_duration(pa_dtype), + check_stacklevel=False, ): super().test_groupby_extension_apply(data_for_grouping, groupby_apply_op) @@ -565,7 +569,9 @@ def test_groupby_extension_agg(self, as_index, data_for_grouping, request): ) ) with tm.maybe_produces_warning( - PerformanceWarning, pa_version_under7p0, check_stacklevel=False + PerformanceWarning, + pa_version_under7p0 and not pa.types.is_duration(pa_dtype), + check_stacklevel=False, ): super().test_groupby_extension_agg(as_index, data_for_grouping) @@ -804,7 +810,9 @@ def test_value_counts_with_normalize(self, data, request): ) ) with tm.maybe_produces_warning( - PerformanceWarning, pa_version_under7p0, check_stacklevel=False + PerformanceWarning, + pa_version_under7p0 and not pa.types.is_duration(pa_dtype), + check_stacklevel=False, ): super().test_value_counts_with_normalize(data) @@ -923,7 +931,9 @@ def test_sort_values_frame(self, data_for_sorting, ascending, request): ) ) with tm.maybe_produces_warning( - PerformanceWarning, pa_version_under7p0, check_stacklevel=False + PerformanceWarning, + pa_version_under7p0 and not pa.types.is_duration(pa_dtype), + check_stacklevel=False, ): super().test_sort_values_frame(data_for_sorting, ascending) diff --git a/pandas/tests/frame/test_arithmetic.py b/pandas/tests/frame/test_arithmetic.py index c4ecffd4fe984..e7c5142bc05ca 100644 --- a/pandas/tests/frame/test_arithmetic.py +++ b/pandas/tests/frame/test_arithmetic.py @@ -1101,7 +1101,11 @@ def test_binop_other(self, op, value, dtype, switch_numexpr_min_elements, reques msg = "ufunc 'remainder' not supported for the input types" elif op is operator.sub: msg = "numpy boolean subtract, the `-` operator, is " - if dtype == "bool" and "numexpr" in request.node.callspec.id: + if ( + dtype == "bool" + and expr.USE_NUMEXPR + and switch_numexpr_min_elements == 0 + ): warn = UserWarning # "evaluating in Python space because ..." else: msg = ( diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index f856f18552594..317aecebf09a5 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -760,11 +760,11 @@ def test_constructor_signed_int_overflow_raises(self): msg = "Values are too large to be losslessly converted" numpy_warning = DeprecationWarning if is_numpy_dev else None with pytest.raises(ValueError, match=msg): - with tm.assert_produces_warning(numpy_warning): + with tm.assert_produces_warning(numpy_warning, check_stacklevel=False): Series([1, 200, 923442], dtype="int8") with pytest.raises(ValueError, match=msg): - with tm.assert_produces_warning(numpy_warning): + with tm.assert_produces_warning(numpy_warning, check_stacklevel=False): Series([1, 200, 923442], dtype="uint8") @pytest.mark.parametrize( From 10d8d7d9510e3b26f83ac7e490f1d9c576e46ec0 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Tue, 10 Jan 2023 16:29:22 -0800 Subject: [PATCH 6/6] Fix for 32 bit build --- pandas/tests/series/test_constructors.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index fc3eb36c291cf..214b2115ad310 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -14,7 +14,10 @@ iNaT, lib, ) -from pandas.compat import is_numpy_dev +from pandas.compat import ( + IS64, + is_numpy_dev, +) from pandas.errors import IntCastingNaNError import pandas.util._test_decorators as td @@ -758,7 +761,7 @@ def test_constructor_cast(self): def test_constructor_signed_int_overflow_raises(self): # GH#41734 disallow silent overflow, enforced in 2.0 msg = "Values are too large to be losslessly converted" - numpy_warning = DeprecationWarning if is_numpy_dev else None + numpy_warning = DeprecationWarning if is_numpy_dev or not IS64 else None with pytest.raises(ValueError, match=msg): with tm.assert_produces_warning(numpy_warning, check_stacklevel=False): Series([1, 200, 923442], dtype="int8")