Skip to content

STY: Check for pytest.raises without context #25866

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

Merged
merged 2 commits into from
Mar 26, 2019
Merged
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
4 changes: 4 additions & 0 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
invgrep -r -E --include '*.py' 'pytest\.warns' pandas/tests/
RET=$(($RET + $?)) ; echo $MSG "DONE"

MSG='Check for pytest raises without context' ; echo $MSG
invgrep -r -E --include '*.py' "[[:space:]] pytest.raises" pandas/tests/
RET=$(($RET + $?)) ; echo $MSG "DONE"

# Check for the following code in testing: `np.testing` and `np.array_equal`
MSG='Check for invalid testing' ; echo $MSG
invgrep -r -E --include '*.py' --exclude testing.py '(numpy|np)(\.testing|\.array_equal)' pandas/tests/
Expand Down
10 changes: 7 additions & 3 deletions pandas/tests/arrays/sparse/test_libsparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
elen = [3, 2, 3, 2]
_check_case(xloc, xlen, yloc, ylen, eloc, elen)

def test_intindex_make_union(self):
def test_int_index_make_union(self):
a = IntIndex(5, np.array([0, 3, 4], dtype=np.int32))
b = IntIndex(5, np.array([0, 2], dtype=np.int32))
res = a.make_union(b)
Expand All @@ -184,7 +184,9 @@ def test_intindex_make_union(self):

a = IntIndex(5, np.array([0, 1], dtype=np.int32))
b = IntIndex(4, np.array([0, 1], dtype=np.int32))
with pytest.raises(ValueError):

msg = "Indices must reference same underlying length"
with pytest.raises(ValueError, match=msg):
a.make_union(b)


Expand All @@ -197,7 +199,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)
else:
tm.assert_almost_equal(result, expected)

Expand Down
9 changes: 6 additions & 3 deletions pandas/tests/frame/test_nonunique_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,12 @@ def test_columns_with_dups(self):
[[1, 2, 1., 2., 3., 'foo', 'bar']], columns=list('ABCDEFG'))
assert_frame_equal(df, expected)

# 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']))
df = DataFrame([[1, 2, 'foo', 'bar']], columns=['a', 'a', 'a', 'a'])
df.columns = ['a', 'a.1', 'a.2', 'a.3']
str(df)
expected = DataFrame([[1, 2, 'foo', 'bar']],
columns=['a', 'a.1', 'a.2', 'a.3'])
assert_frame_equal(df, expected)

# dups across blocks
df_float = DataFrame(np.random.randn(10, 3), dtype='float64')
Expand Down
21 changes: 14 additions & 7 deletions pandas/tests/indexes/interval/test_interval_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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=str(scalar)):
idx.get_loc(scalar)

def test_slice_locs_with_interval(self):

Expand Down Expand Up @@ -89,13 +90,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):
index.slice_locs(start=Interval(0, 2), end=Interval(2, 4))

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

with pytest.raises(KeyError):
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
6 changes: 4 additions & 2 deletions pandas/tests/indexing/multiindex/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ def test_partial_ix_missing(
# 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):
ymd.loc[(2000, 6)]
with pytest.raises(Exception):
ymd.loc[(2000, 6), 0]

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

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexing/test_floats.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ def test_scalar_non_numeric(self):
# for idxr in [lambda x: x.ix,
# lambda x: x]:
# s2 = s.copy()
# def f():
#
# with pytest.raises(TypeError):
# idxr(s2)[3.0] = 0
# pytest.raises(TypeError, f)
pass

else:
Expand Down
Loading