-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 4 commits
cb2eae7
8ec1552
8d8759c
9d6b5c6
3aa222b
a958b81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
|
@@ -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 = f"index {length} is out of bounds for axis 0 with size {length}" | ||
with pytest.raises(IndexError, match=msg): | ||
# either depending on numpy version | ||
indices.delete(len(indices)) | ||
indices.delete(length) | ||
|
||
def test_equals(self, indices): | ||
if isinstance(indices, IntervalIndex): | ||
|
@@ -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]) | ||
|
@@ -861,10 +865,19 @@ def test_getitem_2d_deprecated(self): | |
|
||
def test_contains_requires_hashable_raises(self): | ||
idx = self.create_index() | ||
with pytest.raises(TypeError, match="unhashable type"): | ||
|
||
msg = "unhashable type: 'list'" | ||
with pytest.raises(TypeError, match=msg): | ||
[] in idx | ||
|
||
with pytest.raises(TypeError): | ||
msg = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please use the pattern of: msg = "|".join([
"foo",
"bar",
"baz"
]) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
r"unhashable type: 'dict'|" | ||
r"must be real number, not dict|" | ||
r"an integer is required|" | ||
r"\{\}|" | ||
r"pandas\._libs\.interval\.IntervalTree' is not iterable" | ||
) | ||
with pytest.raises(TypeError, match=msg): | ||
{} in idx._engine | ||
|
||
def test_copy_copies_cache(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -175,6 +175,6 @@ def test_delete_base(idx): | |
assert result.equals(expected) | ||
assert result.name == expected.name | ||
|
||
with pytest.raises((IndexError, ValueError)): | ||
# Exception raised depends on NumPy version. | ||
msg = "index 6 is out of bounds for axis 0 with size 6" | ||
with pytest.raises(IndexError, match=msg): | ||
idx.delete(len(idx)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In earlier versions NumPy used to raise ValueError, pandas setup.py has defined min_numpy_version to 1.13.3. Since NumPy 1.13.3, IndexError is being raised There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sathyz Thank you for looking into this! |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -105,7 +105,11 @@ def test_unsortedindex(): | |||||
expected = df.iloc[0] | ||||||
tm.assert_series_equal(result, expected) | ||||||
|
||||||
with pytest.raises(UnsortedIndexError): | ||||||
msg = ( | ||||||
r"MultiIndex slicing requires the index to be lexsorted: " | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A single line is split into multiple lines, to adhere to the formatting rules. If I were to do this, one of the lines will be |
||||||
r"slicing on levels \[1\], lexsort depth 0" | ||||||
) | ||||||
with pytest.raises(UnsortedIndexError, match=msg): | ||||||
df.loc(axis=0)["z", slice("a")] | ||||||
df.sort_index(inplace=True) | ||||||
assert len(df.loc(axis=0)["z", :]) == 2 | ||||||
|
@@ -124,7 +128,8 @@ def test_unsortedindex_doc_examples(): | |||||
with tm.assert_produces_warning(PerformanceWarning): | ||||||
dfm.loc[(1, "z")] | ||||||
|
||||||
with pytest.raises(UnsortedIndexError): | ||||||
msg = r"Key length \(2\) was greater than MultiIndex lexsort depth \(1\)" | ||||||
with pytest.raises(UnsortedIndexError, match=msg): | ||||||
dfm.loc[(0, "y"):(1, "z")] | ||||||
|
||||||
assert not dfm.index.is_lexsorted() | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are we testing the missing-argument case somewhere else?
i think the "either depending..." comment is obsolete