Skip to content

[WIP] STY: activate pytest.raises context manager code check #25750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/arrays/sparse/test_libsparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't convert this on first pass since test is skipped on windows. looking at it again, the assertion error check is used in lieu of a tm.assert_numpy_array_not_equal function so message check not appropriate.

else:
tm.assert_almost_equal(result, expected)

Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/frame/test_nonunique_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="<enter message here>"):
# DataFrame([[1, 2, 'foo', 'bar']], columns=['a', 'a', 'a', 'a'])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does not raise. added a TODO. not in this PR though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this not failing CI before?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WillAyd . case of lambda x: with no x raises TypeError and so passed CI.


# dups across blocks
df_float = DataFrame(np.random.randn(10, 3), dtype='float64')
Expand Down
22 changes: 15 additions & 7 deletions pandas/tests/indexes/interval/test_interval_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whole module is skipped. added a TODO. not in this PR though.

pytestmark = pytest.mark.skip(reason="new indexing tests for issue 16316")


Expand Down Expand Up @@ -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="<enter message here>"):
idx.get_loc(scalar)

def test_slice_locs_with_interval(self):

Expand Down Expand Up @@ -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="<enter message here>"):
index.slice_locs(start=Interval(0, 2), end=Interval(2, 4))

with pytest.raises(KeyError, match="<enter message here>"):
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="<enter message here>"):
index.slice_locs(end=Interval(0, 2))

with pytest.raises(KeyError, match="<enter message here>"):
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)])
Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/indexing/multiindex/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is skipped. added a TODO. not in this PR though.


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="<enter message here>"):
ymd.loc.__getitem__((2000, 6))
with pytest.raises(Exception, match="<enter message here>"):
ymd.loc.__getitem__((2000, 6), 0)

# ---------------------------------------------------------------------

Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="<enter message here>"):
# td.astype('m8[%s]' % t)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does not raise. added a TODO. not in this PR though.


# valid astype
td.astype('int64')
Expand Down