Skip to content

Check error message for raised exception #33103

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 6 commits into from
Mar 29, 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
3 changes: 2 additions & 1 deletion pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ def __init__(self, values, freq=None, dtype=None, copy=False):
@classmethod
def _simple_new(cls, values: np.ndarray, freq=None, **kwargs) -> "PeriodArray":
# alias for PeriodArray.__init__
assert isinstance(values, np.ndarray) and values.dtype == "i8"
assertion_msg = "Should be numpy array of type i8"
assert isinstance(values, np.ndarray) and values.dtype == "i8", assertion_msg
return cls(values, freq=freq, **kwargs)

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/categorical/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def test_delete(self):
expected = CategoricalIndex(list("aabbc"), categories=categories)
tm.assert_index_equal(result, expected, exact=True)

with pytest.raises((IndexError, ValueError)):
with tm.external_error_raised((IndexError, ValueError)):
# Either depending on NumPy version
ci.delete(10)

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/period/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ def test_constructor_simple_new(self):
result = idx._simple_new(idx._data, name="p")
tm.assert_index_equal(result, idx)

with pytest.raises(AssertionError):
msg = "Should be numpy array of type i8"
with pytest.raises(AssertionError, match=msg):
# Need ndarray, not Int64Index
type(idx._data)._simple_new(idx.astype("i8"), freq=idx.freq)

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/period/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ def test_join_does_not_recur(self):
def test_join_mismatched_freq_raises(self):
index = period_range("1/1/2000", "1/20/2000", freq="D")
index3 = period_range("1/1/2000", "1/20/2000", freq="2D")
with pytest.raises(IncompatibleFrequency):
msg = r".*Input has different freq=2D from PeriodIndex\(freq=D\)"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
msg = r".*Input has different freq=2D from PeriodIndex\(freq=D\)"
msg = r"Input has different freq=2D from PeriodIndex\(freq=D\)"

with pytest.raises(IncompatibleFrequency, match=msg):
index.join(index3)
8 changes: 5 additions & 3 deletions pandas/tests/indexes/period/test_partial_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def test_range_slice_day(self):
didx = pd.date_range(start="2013/01/01", freq="D", periods=400)
pidx = period_range(start="2013/01/01", freq="D", periods=400)

msg = "slice indices must be integers or None or have an __index__ method"
for idx in [didx, pidx]:
# slices against index should raise IndexError
values = [
Expand All @@ -69,7 +70,7 @@ def test_range_slice_day(self):
"2013/02/01 09:00",
]
for v in values:
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
idx[v:]

s = Series(np.random.rand(len(idx)), index=idx)
Expand All @@ -81,13 +82,14 @@ def test_range_slice_day(self):

invalid = ["2013/02/01 9H", "2013/02/01 09:00"]
for v in invalid:
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
idx[v:]

def test_range_slice_seconds(self):
# GH#6716
didx = pd.date_range(start="2013/01/01 09:00:00", freq="S", periods=4000)
pidx = period_range(start="2013/01/01 09:00:00", freq="S", periods=4000)
msg = "slice indices must be integers or None or have an __index__ method"

for idx in [didx, pidx]:
# slices against index should raise IndexError
Expand All @@ -99,7 +101,7 @@ def test_range_slice_seconds(self):
"2013/02/01 09:00",
]
for v in values:
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
idx[v:]

s = Series(np.random.rand(len(idx)), index=idx)
Expand Down
9 changes: 6 additions & 3 deletions pandas/tests/indexes/period/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ def test_union_misc(self, sort):
# raise if different frequencies
index = period_range("1/1/2000", "1/20/2000", freq="D")
index2 = period_range("1/1/2000", "1/20/2000", freq="W-WED")
with pytest.raises(IncompatibleFrequency):
msg = r"Input has different freq=W-WED from PeriodIndex\(freq=D\)"
with pytest.raises(IncompatibleFrequency, match=msg):
index.union(index2, sort=sort)

# TODO: belongs elsewhere
Expand Down Expand Up @@ -180,11 +181,13 @@ def test_intersection(self, sort):
# raise if different frequencies
index = period_range("1/1/2000", "1/20/2000", freq="D")
index2 = period_range("1/1/2000", "1/20/2000", freq="W-WED")
with pytest.raises(IncompatibleFrequency):
msg = r"Input has different freq=W-WED from PeriodIndex\(freq=D\)"
with pytest.raises(IncompatibleFrequency, match=msg):
index.intersection(index2, sort=sort)

index3 = period_range("1/1/2000", "1/20/2000", freq="2D")
with pytest.raises(IncompatibleFrequency):
msg = r"Input has different freq=2D from PeriodIndex\(freq=D\)"
with pytest.raises(IncompatibleFrequency, match=msg):
index.intersection(index3, sort=sort)

def test_intersection_cases(self, sort):
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/indexes/timedeltas/test_delete.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import pytest

from pandas import TimedeltaIndex, timedelta_range
import pandas._testing as tm

Expand Down Expand Up @@ -30,7 +28,7 @@ def test_delete(self):
assert result.name == expected.name
assert result.freq == expected.freq

with pytest.raises((IndexError, ValueError)):
with tm.external_error_raised((IndexError, ValueError)):
# either depending on numpy version
idx.delete(5)

Expand Down