diff --git a/ci/code_checks.sh b/ci/code_checks.sh index 51df779341ed5..fc5b175d9b958 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -159,9 +159,8 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then # For any flake8-compliant code, the only way this regex gets # matched is if there is no "with" statement preceding "pytest.raises" MSG='Check for pytest.raises as context manager (a line starting with `pytest.raises` is invalid, needs a `with` to precede it)' ; echo $MSG - MSG='TODO: This check is currently skipped because so many files fail this. Please enable when all are corrected (xref gh-24332)' ; echo $MSG - # invgrep -R --include '*.py' -E '[[:space:]] pytest.raises' pandas/tests - # RET=$(($RET + $?)) ; echo $MSG "DONE" + invgrep -R --include '*.py' -E '[[:space:]] pytest.raises' pandas/tests + RET=$(($RET + $?)) ; echo $MSG "DONE" MSG='Check for wrong space after code-block directive and before colon (".. code-block ::" instead of ".. code-block::")' ; echo $MSG invgrep -R --include="*.rst" ".. code-block ::" doc/source diff --git a/pandas/tests/arrays/sparse/test_libsparse.py b/pandas/tests/arrays/sparse/test_libsparse.py index 2cbe7d9ea084c..b4dff5f4fdf61 100644 --- a/pandas/tests/arrays/sparse/test_libsparse.py +++ b/pandas/tests/arrays/sparse/test_libsparse.py @@ -197,7 +197,9 @@ def _check_correct(a, b, expected): assert (result.equals(expected)) def _check_length_exc(a, longer): - pytest.raises(Exception, a.intersect, longer) + msg = "Indices must reference same underlying length" + with pytest.raises(Exception, match=msg): + a.intersect(longer) def _check_case(xloc, xlen, yloc, ylen, eloc, elen): xindex = BlockIndex(TEST_LENGTH, xloc, xlen) diff --git a/pandas/tests/computation/test_eval.py b/pandas/tests/computation/test_eval.py index 49d263feab664..857567622514f 100644 --- a/pandas/tests/computation/test_eval.py +++ b/pandas/tests/computation/test_eval.py @@ -339,8 +339,8 @@ def check_pow(self, lhs, arith1, rhs): if (is_scalar(lhs) and is_scalar(rhs) and _is_py3_complex_incompat(result, expected)): - pytest.raises(AssertionError, tm.assert_numpy_array_equal, - result, expected) + with pytest.raises(AssertionError): + tm.assert_numpy_array_equal(result, expected) else: tm.assert_almost_equal(result, expected) diff --git a/pandas/tests/frame/test_nonunique_indexes.py b/pandas/tests/frame/test_nonunique_indexes.py index 799d548100b5e..65775bce93136 100644 --- a/pandas/tests/frame/test_nonunique_indexes.py +++ b/pandas/tests/frame/test_nonunique_indexes.py @@ -416,9 +416,10 @@ def test_columns_with_dups(self): [[1, 2, 1., 2., 3., 'foo', 'bar']], columns=list('ABCDEFG')) assert_frame_equal(df, expected) + # TODO: fix this. does not raise. # this is an error because we cannot disambiguate the dup columns - pytest.raises(Exception, lambda x: DataFrame( - [[1, 2, 'foo', 'bar']], columns=['a', 'a', 'a', 'a'])) + # with pytest.raises(Exception, match=""): + # DataFrame([[1, 2, 'foo', 'bar']], columns=['a', 'a', 'a', 'a']) # dups across blocks df_float = DataFrame(np.random.randn(10, 3), dtype='float64') diff --git a/pandas/tests/indexes/interval/test_interval_new.py b/pandas/tests/indexes/interval/test_interval_new.py index fcffa29f7eadb..69091f19449e7 100644 --- a/pandas/tests/indexes/interval/test_interval_new.py +++ b/pandas/tests/indexes/interval/test_interval_new.py @@ -6,6 +6,7 @@ from pandas import Int64Index, Interval, IntervalIndex import pandas.util.testing as tm +# TODO: unskip these tests and enter error messages for pytest.raises pytestmark = pytest.mark.skip(reason="new indexing tests for issue 16316") @@ -49,7 +50,8 @@ def test_get_loc_scalar(self, closed, scalar): if scalar in correct[closed].keys(): assert idx.get_loc(scalar) == correct[closed][scalar] else: - pytest.raises(KeyError, idx.get_loc, scalar) + with pytest.raises(KeyError, match=""): + idx.get_loc(scalar) def test_slice_locs_with_interval(self): @@ -89,13 +91,19 @@ def test_slice_locs_with_interval(self): # unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)]) - pytest.raises(KeyError, index.slice_locs( - start=Interval(0, 2), end=Interval(2, 4))) - pytest.raises(KeyError, index.slice_locs(start=Interval(0, 2))) + with pytest.raises(KeyError, match=""): + index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) + + with pytest.raises(KeyError, match=""): + index.slice_locs(start=Interval(0, 2)) + assert index.slice_locs(end=Interval(2, 4)) == (0, 2) - pytest.raises(KeyError, index.slice_locs(end=Interval(0, 2))) - pytest.raises(KeyError, index.slice_locs( - start=Interval(2, 4), end=Interval(0, 2))) + + with pytest.raises(KeyError, match=""): + index.slice_locs(end=Interval(0, 2)) + + with pytest.raises(KeyError, match=""): + index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) # another unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)]) diff --git a/pandas/tests/indexing/multiindex/test_partial.py b/pandas/tests/indexing/multiindex/test_partial.py index 473463def2b87..9f0d505510b08 100644 --- a/pandas/tests/indexing/multiindex/test_partial.py +++ b/pandas/tests/indexing/multiindex/test_partial.py @@ -134,21 +134,23 @@ def test_partial_set( def test_partial_ix_missing( self, multiindex_year_month_day_dataframe_random_data): - pytest.skip("skipping for now") + pytest.skip("skipping for now") # TODO: fix this ymd = multiindex_year_month_day_dataframe_random_data result = ymd.loc[2000, 0] expected = ymd.loc[2000]['A'] tm.assert_series_equal(result, expected) - # need to put in some work here + # TODO: need to put in some work here # self.ymd.loc[2000, 0] = 0 # assert (self.ymd.loc[2000]['A'] == 0).all() # Pretty sure the second (and maybe even the first) is already wrong. - pytest.raises(Exception, ymd.loc.__getitem__, (2000, 6)) - pytest.raises(Exception, ymd.loc.__getitem__, (2000, 6), 0) + with pytest.raises(Exception, match=""): + ymd.loc.__getitem__((2000, 6)) + with pytest.raises(Exception, match=""): + ymd.loc.__getitem__((2000, 6), 0) # --------------------------------------------------------------------- diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 96e18c6a60cac..c5ee20e748453 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1118,8 +1118,10 @@ def test_constructor_dtype_timedelta64(self): assert td.dtype == 'timedelta64[ns]' # these are frequency conversion astypes + # TODO: fix this. does not raise. # for t in ['s', 'D', 'us', 'ms']: - # pytest.raises(TypeError, td.astype, 'm8[%s]' % t) + # with pytest.raises(TypeError, match=""): + # td.astype('m8[%s]' % t) # valid astype td.astype('int64')