From cb2eae79bdb4252675fec47084323fba0ee33a91 Mon Sep 17 00:00:00 2001 From: Satheesh Kumar Mohan Date: Sat, 28 Mar 2020 13:44:51 +0530 Subject: [PATCH 1/4] add msg for groupby test_categorical --- pandas/tests/groupby/test_categorical.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index 9ea5252b91e13..cbf475e4260ec 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -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() From 8ec155211d9ded70baba89f9be31d400daf5b124 Mon Sep 17 00:00:00 2001 From: Satheesh Kumar Mohan Date: Sat, 28 Mar 2020 22:48:48 +0530 Subject: [PATCH 2/4] add message when checking for exceptions. --- .../indexes/categorical/test_category.py | 3 ++- pandas/tests/indexes/common.py | 24 ++++++++++--------- pandas/tests/indexes/datetimelike.py | 9 +++---- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/pandas/tests/indexes/categorical/test_category.py b/pandas/tests/indexes/categorical/test_category.py index 6e8e81230b2bb..1b45dcf12d358 100644 --- a/pandas/tests/indexes/categorical/test_category.py +++ b/pandas/tests/indexes/categorical/test_category.py @@ -192,7 +192,8 @@ def test_delete(self): expected = CategoricalIndex(list("aabbc"), categories=categories) tm.assert_index_equal(result, expected, exact=True) - with pytest.raises((IndexError, ValueError)): + msg = "index 10 is out of bounds for axis 0 with size 6" + with pytest.raises((IndexError, ValueError), match=msg): # Either depending on NumPy version ci.delete(10) diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 964cf320a422b..0be5cb609f096 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -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 = 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() 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]) @@ -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 def test_copy_copies_cache(self): # GH32898 diff --git a/pandas/tests/indexes/datetimelike.py b/pandas/tests/indexes/datetimelike.py index ba10976a67e9a..85d670e9dbffa 100644 --- a/pandas/tests/indexes/datetimelike.py +++ b/pandas/tests/indexes/datetimelike.py @@ -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): From 9d6b5c684d350eecd88b3c1a9165398a61196d96 Mon Sep 17 00:00:00 2001 From: Satheesh Kumar Mohan Date: Tue, 31 Mar 2020 09:13:24 +0530 Subject: [PATCH 3/4] add match for pytest.raises() --- pandas/tests/indexes/common.py | 25 ++++++++++++++++------ pandas/tests/indexes/multi/test_compat.py | 6 +++++- pandas/tests/indexes/multi/test_reshape.py | 4 ++-- pandas/tests/indexes/multi/test_sorting.py | 9 ++++++-- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 0be5cb609f096..24d08f892ae19 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -539,10 +539,10 @@ def test_delete_base(self, indices): assert result.name == expected.name length = len(indices) - msg = r"delete\(\) missing 1 required positional argument: 'loc'" - with pytest.raises(TypeError, match=msg): + 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() + indices.delete(length) def test_equals(self, indices): if isinstance(indices, IntervalIndex): @@ -863,11 +863,22 @@ def test_getitem_2d_deprecated(self): assert isinstance(res, np.ndarray), type(res) - @pytest.mark.parametrize("value", [[], {}]) - def test_contains_requires_hashable_raises(self, value): + def test_contains_requires_hashable_raises(self): idx = self.create_index() - with pytest.raises(TypeError, match="unhashable type"): - value in idx + + msg = "unhashable type: 'list'" + with pytest.raises(TypeError, match=msg): + [] in idx + + msg = ( + 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): # GH32898 diff --git a/pandas/tests/indexes/multi/test_compat.py b/pandas/tests/indexes/multi/test_compat.py index 9273de9c20412..d1f66af4a8e83 100644 --- a/pandas/tests/indexes/multi/test_compat.py +++ b/pandas/tests/indexes/multi/test_compat.py @@ -53,7 +53,11 @@ def test_boolean_context_compat2(): i2 = MultiIndex.from_tuples([("A", 1), ("A", 3)]) common = i1.intersection(i2) - with pytest.raises(ValueError): + msg = ( + r"The truth value of a MultiIndex is ambiguous\. " + r"Use a\.empty, a\.bool\(\), a\.item\(\), a\.any\(\) or a\.all\(\)\." + ) + with pytest.raises(ValueError, match=msg): bool(common) diff --git a/pandas/tests/indexes/multi/test_reshape.py b/pandas/tests/indexes/multi/test_reshape.py index de32bd94be491..6d8a396119ef3 100644 --- a/pandas/tests/indexes/multi/test_reshape.py +++ b/pandas/tests/indexes/multi/test_reshape.py @@ -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)) diff --git a/pandas/tests/indexes/multi/test_sorting.py b/pandas/tests/indexes/multi/test_sorting.py index bb40612b9a55a..129bf34edddde 100644 --- a/pandas/tests/indexes/multi/test_sorting.py +++ b/pandas/tests/indexes/multi/test_sorting.py @@ -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: " + 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() From 3aa222b3b6eacf305aa0da1d3af55f044885eeee Mon Sep 17 00:00:00 2001 From: Satheesh Kumar Mohan Date: Mon, 6 Apr 2020 09:48:35 +0530 Subject: [PATCH 4/4] remove numpy comment and use "|".join() --- pandas/tests/indexes/common.py | 15 ++++++++------- pandas/tests/indexes/multi/test_sorting.py | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 24d08f892ae19..fd23e95106ab0 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -541,7 +541,6 @@ def test_delete_base(self, indices): 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(length) def test_equals(self, indices): @@ -870,12 +869,14 @@ def test_contains_requires_hashable_raises(self): with pytest.raises(TypeError, match=msg): [] in idx - msg = ( - 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" + msg = "|".join( + [ + 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 diff --git a/pandas/tests/indexes/multi/test_sorting.py b/pandas/tests/indexes/multi/test_sorting.py index 129bf34edddde..423bbed831b87 100644 --- a/pandas/tests/indexes/multi/test_sorting.py +++ b/pandas/tests/indexes/multi/test_sorting.py @@ -106,7 +106,7 @@ def test_unsortedindex(): tm.assert_series_equal(result, expected) msg = ( - r"MultiIndex slicing requires the index to be lexsorted: " + "MultiIndex slicing requires the index to be lexsorted: " r"slicing on levels \[1\], lexsort depth 0" ) with pytest.raises(UnsortedIndexError, match=msg):