Skip to content

TST: GH30999 Add match=msg to all pytest.raises in tests/indexes #38697

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
2 changes: 1 addition & 1 deletion pandas/tests/indexes/multi/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ def test_get_loc_duplicates(self):
xp = 0
assert rs == xp

with pytest.raises(KeyError):
with pytest.raises(KeyError, match="2"):
index.get_loc(2)

def test_get_loc_level(self):
Expand Down
6 changes: 2 additions & 4 deletions pandas/tests/indexes/multi/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,12 @@ def test_difference_sort_incomparable():
tm.assert_index_equal(result, idx)


@pytest.mark.xfail(reason="Not implemented.")
def test_difference_sort_incomparable_true():
# TODO decide on True behaviour
# # sort=True, raises
idx = pd.MultiIndex.from_product([[1, pd.Timestamp("2000"), 2], ["a", "b"]])
other = pd.MultiIndex.from_product([[3, pd.Timestamp("2000"), 4], ["c", "d"]])

with pytest.raises(TypeError):
msg = "The 'sort' keyword only takes the values of None or False; True was passed."
with pytest.raises(ValueError, match=msg):
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 was marked as failing. But when I switched it to checking that a ValueError was raised, it passed. I think this works as intended now.

Copy link
Contributor

Choose a reason for hiding this comment

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

this is fine, a lot of things have been cleaned up recently I guess this was missed. cc @jbrockmendel

idx.difference(other, sort=True)


Expand Down
11 changes: 9 additions & 2 deletions pandas/tests/indexes/period/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,15 @@ def test_get_indexer_mismatched_dtype_with_method(self, non_comparable_idx, meth
other2 = other.astype(dtype)
if dtype == "object" and isinstance(other, PeriodIndex):
continue
# For object dtype we are liable to get a different exception message
with pytest.raises(TypeError):
# Two different error message patterns depending on dtypes
msg = "|".join(
re.escape(msg)
for msg in (
f"Cannot compare dtypes {pi.dtype} and {other.dtype}",
" not supported between instances of ",
)
)
with pytest.raises(TypeError, match=msg):
pi.get_indexer(other2, method=method)

def test_get_indexer_non_unique(self):
Expand Down
15 changes: 12 additions & 3 deletions pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ def test_droplevel(self, index):
if isinstance(index.name, tuple) and level is index.name:
# GH 21121 : droplevel with tuple name
continue
with pytest.raises(ValueError):
msg = (
"Cannot remove 1 levels from an index with 1 levels: at least one "
"level must be left."
)
with pytest.raises(ValueError, match=msg):
index.droplevel(level)

for level in "wrong", ["wrong"]:
Expand Down Expand Up @@ -77,7 +81,11 @@ def test_constructor_unwraps_index(self, index):
# FutureWarning from non-tuple sequence of nd indexing
@pytest.mark.filterwarnings("ignore::FutureWarning")
def test_getitem_error(self, index, itm):
with pytest.raises(IndexError):
msg = r"index 101 is out of bounds for axis 0 with size [\d]+|" + re.escape(
"only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) "
"and integer or boolean arrays are valid indices"
)
with pytest.raises(IndexError, match=msg):
index[itm]

def test_to_flat_index(self, index):
Expand Down Expand Up @@ -249,7 +257,8 @@ def test_searchsorted_monotonic(self, index):
assert expected_right == ssm_right
else:
# non-monotonic should raise.
with pytest.raises(ValueError):
msg = "index must be monotonic increasing or decreasing"
with pytest.raises(ValueError, match=msg):
index._searchsorted_monotonic(value, side="left")

def test_pickle(self, index):
Expand Down
14 changes: 5 additions & 9 deletions pandas/tests/indexes/test_numpy_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ def test_numpy_ufuncs_basic(index, func):
# https://numpy.org/doc/stable/reference/ufuncs.html

if isinstance(index, DatetimeIndexOpsMixin):
# raise TypeError or ValueError (PeriodIndex)
with pytest.raises(Exception):
with tm.external_error_raised((TypeError, AttributeError)):
with np.errstate(all="ignore"):
func(index)
elif isinstance(index, (Float64Index, Int64Index, UInt64Index)):
Expand All @@ -66,7 +65,7 @@ def test_numpy_ufuncs_basic(index, func):
if len(index) == 0:
pass
else:
with pytest.raises(Exception):
with tm.external_error_raised((TypeError, AttributeError)):
with np.errstate(all="ignore"):
func(index)

Expand All @@ -77,7 +76,6 @@ def test_numpy_ufuncs_basic(index, func):
def test_numpy_ufuncs_other(index, func, request):
# test ufuncs of numpy, see:
# https://numpy.org/doc/stable/reference/ufuncs.html

if isinstance(index, (DatetimeIndex, TimedeltaIndex)):
if isinstance(index, DatetimeIndex) and index.tz is not None:
if func in [np.isfinite, np.isnan, np.isinf]:
Expand All @@ -96,13 +94,11 @@ def test_numpy_ufuncs_other(index, func, request):
result = func(index)
assert isinstance(result, np.ndarray)
else:
# raise TypeError or ValueError (PeriodIndex)
with pytest.raises(Exception):
with tm.external_error_raised(TypeError):
Copy link
Contributor

Choose a reason for hiding this comment

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

guess these are older comments, cc @jbrockmendel

Copy link
Member

Choose a reason for hiding this comment

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

i think so yah

func(index)

elif isinstance(index, PeriodIndex):
# raise TypeError or ValueError (PeriodIndex)
with pytest.raises(Exception):
with tm.external_error_raised(TypeError):
func(index)

elif isinstance(index, (Float64Index, Int64Index, UInt64Index)):
Expand All @@ -114,5 +110,5 @@ def test_numpy_ufuncs_other(index, func, request):
if len(index) == 0:
pass
else:
with pytest.raises(Exception):
with tm.external_error_raised(TypeError):
func(index)