Skip to content

TST: add messages to pytest.raises #33650

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 1 commit into from
Apr 19, 2020
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
6 changes: 4 additions & 2 deletions pandas/tests/arrays/boolean/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def test_ufuncs_binary(ufunc):
tm.assert_extension_array_equal(result, expected)

# not handled types
with pytest.raises(TypeError):
msg = r"operand type\(s\) all returned NotImplemented from __array_ufunc__"
with pytest.raises(TypeError, match=msg):
ufunc(a, "test")


Expand All @@ -76,7 +77,8 @@ def test_ufuncs_unary(ufunc):
@pytest.mark.parametrize("values", [[True, False], [True, None]])
def test_ufunc_reduce_raises(values):
a = pd.array(values, dtype="boolean")
with pytest.raises(NotImplementedError):
msg = "The 'reduce' method is not supported"
with pytest.raises(NotImplementedError, match=msg):
np.add.reduce(a)


Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/ranges/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ def test_constructor_corner(self):
tm.assert_index_equal(index, Index(arr))

# non-int raise Exception
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=r"Wrong type \<class 'str'\>"):
RangeIndex("1", "10", "1")
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=r"Wrong type \<class 'float'\>"):
RangeIndex(1.1, 10.2, 1.3)

# invalid passed type
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ def test_delete(self):
tm.assert_index_equal(result, expected)
assert result.name == expected.name

with pytest.raises((IndexError, ValueError)):
msg = "index 5 is out of bounds for axis 0 with size 5"
with pytest.raises((IndexError, ValueError), match=msg):
# either depending on numpy version
result = idx.delete(len(idx))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def test_detect_chained_assignment():
multiind = MultiIndex.from_tuples(tuples, names=["part", "side"])
zed = DataFrame(events, index=["a", "b"], columns=multiind)

with pytest.raises(com.SettingWithCopyError):
msg = "A value is trying to be set on a copy of a slice from a DataFrame"
with pytest.raises(com.SettingWithCopyError, match=msg):
zed["eyes"]["right"].fillna(value=555, inplace=True)


Expand Down
30 changes: 20 additions & 10 deletions pandas/tests/indexing/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ def test_partial_setting(self):
# iloc/iat raise
s = s_orig.copy()

with pytest.raises(IndexError):
msg = "iloc cannot enlarge its target object"
with pytest.raises(IndexError, match=msg):
s.iloc[3] = 5.0

with pytest.raises(IndexError):
msg = "index 3 is out of bounds for axis 0 with size 3"
with pytest.raises(IndexError, match=msg):
s.iat[3] = 5.0

# ## frame ##
Expand All @@ -58,10 +60,12 @@ def test_partial_setting(self):
# iloc/iat raise
df = df_orig.copy()

with pytest.raises(IndexError):
msg = "iloc cannot enlarge its target object"
with pytest.raises(IndexError, match=msg):
df.iloc[4, 2] = 5.0

with pytest.raises(IndexError):
msg = "index 2 is out of bounds for axis 0 with size 2"
with pytest.raises(IndexError, match=msg):
df.iat[4, 2] = 5.0

# row setting where it exists
Expand Down Expand Up @@ -162,7 +166,8 @@ def test_partial_setting_mixed_dtype(self):
# list-like must conform
df = DataFrame(columns=["A", "B"])

with pytest.raises(ValueError):
msg = "cannot set a row with mismatched columns"
with pytest.raises(ValueError, match=msg):
df.loc[0] = [1, 2, 3]

# TODO: #15657, these are left as object and not coerced
Expand Down Expand Up @@ -330,10 +335,12 @@ def test_partial_set_invalid(self):
df = orig.copy()

# don't allow not string inserts
with pytest.raises(TypeError):
msg = "cannot insert DatetimeIndex with incompatible label"

with pytest.raises(TypeError, match=msg):
df.loc[100.0, :] = df.iloc[0]

with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
df.loc[100, :] = df.iloc[0]

# allow object conversion here
Expand Down Expand Up @@ -375,13 +382,16 @@ def test_partial_set_empty_frame(self):
# frame
df = DataFrame()

with pytest.raises(ValueError):
msg = "cannot set a frame with no defined columns"

with pytest.raises(ValueError, match=msg):
df.loc[1] = 1

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
df.loc[1] = Series([1], index=["foo"])

with pytest.raises(ValueError):
msg = "cannot set a frame with no defined index and a scalar"
with pytest.raises(ValueError, match=msg):
df.loc[:, 1] = 1

# these work as they don't really change
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/series/indexing/test_alter_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ def test_reindex_datetimeindexes_tz_naive_and_aware():
idx = date_range("20131101", tz="America/Chicago", periods=7)
newidx = date_range("20131103", periods=10, freq="H")
s = Series(range(7), index=idx)
with pytest.raises(TypeError):
msg = "Cannot compare tz-naive and tz-aware timestamps"
with pytest.raises(TypeError, match=msg):
s.reindex(newidx, method="ffill")


Expand Down