Skip to content

add match message for pytest.raises() #33144

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
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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/tests/groupby/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,8 @@ def test_bins_unequal_len():
bins = pd.cut(series.dropna().values, 4)

# len(bins) != len(series) here
with pytest.raises(ValueError):
msg = r"Length of grouper \(8\) and axis \(10\) must be same length"
with pytest.raises(ValueError, match=msg):
series.groupby(bins).mean()


Expand Down
24 changes: 13 additions & 11 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ def test_take(self, indices):

if not isinstance(indices, (DatetimeIndex, PeriodIndex, TimedeltaIndex)):
# GH 10791
with pytest.raises(AttributeError):
msg = r"'(.*Index)' object has no attribute 'freq'"
with pytest.raises(AttributeError, match=msg):
indices.freq

def test_take_invalid_kwargs(self):
Expand Down Expand Up @@ -537,9 +538,11 @@ def test_delete_base(self, indices):
assert result.equals(expected)
assert result.name == expected.name

with pytest.raises((IndexError, ValueError)):
length = len(indices)
msg = r"delete\(\) missing 1 required positional argument: 'loc'"
with pytest.raises(TypeError, match=msg):
# either depending on numpy version
indices.delete(len(indices))
indices.delete()
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 you forgot to put length inside the indices.delete(), on L545.

And I think that this also means you need to change the error message.

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 @MomIsBestFriend is correct here, the unused length is also the source of the linting failure


def test_equals(self, indices):
if isinstance(indices, IntervalIndex):
Expand Down Expand Up @@ -787,13 +790,14 @@ def test_putmask_with_wrong_mask(self):
# GH18368
index = self.create_index()

with pytest.raises(ValueError):
msg = "putmask: mask and data must be the same size"
with pytest.raises(ValueError, match=msg):
index.putmask(np.ones(len(index) + 1, np.bool), 1)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
index.putmask(np.ones(len(index) - 1, np.bool), 1)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
index.putmask("foo", 1)

@pytest.mark.parametrize("copy", [True, False])
Expand Down Expand Up @@ -859,13 +863,11 @@ def test_getitem_2d_deprecated(self):

assert isinstance(res, np.ndarray), type(res)

def test_contains_requires_hashable_raises(self):
@pytest.mark.parametrize("value", [[], {}])
def test_contains_requires_hashable_raises(self, value):
idx = self.create_index()
with pytest.raises(TypeError, match="unhashable type"):
[] in idx

with pytest.raises(TypeError):
{} in idx._engine
value in idx
Copy link
Member

Choose a reason for hiding this comment

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

we also need to check for value in idx._engine

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @jbrockmendel added it now.


def test_copy_copies_cache(self):
# GH32898
Expand Down
9 changes: 5 additions & 4 deletions pandas/tests/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
class DatetimeLike(Base):
def test_argmax_axis_invalid(self):
# GH#23081
msg = r"`axis` must be fewer than the number of dimensions \(1\)"
rng = self.create_index()
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
rng.argmax(axis=1)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
rng.argmin(axis=2)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
rng.min(axis=-2)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
rng.max(axis=-3)

def test_can_hold_identifiers(self):
Expand Down