From 5dc80947593077c8f4053947458170a26393b03e Mon Sep 17 00:00:00 2001 From: moink Date: Wed, 30 Dec 2020 11:11:46 +0100 Subject: [PATCH 1/2] TST: GH30999 Add match=msg to all pytest.raises in pandas/tests/reshape --- pandas/tests/reshape/test_get_dummies.py | 17 ++++++++++++++--- pandas/tests/reshape/test_union_categoricals.py | 3 ++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/pandas/tests/reshape/test_get_dummies.py b/pandas/tests/reshape/test_get_dummies.py index a32adeb612e7c..42907b3b4e23f 100644 --- a/pandas/tests/reshape/test_get_dummies.py +++ b/pandas/tests/reshape/test_get_dummies.py @@ -1,3 +1,5 @@ +import re + import numpy as np import pytest @@ -30,7 +32,8 @@ def effective_dtype(self, dtype): return dtype def test_get_dummies_raises_on_dtype_object(self, df): - with pytest.raises(ValueError): + msg = "dtype=object is not a valid dtype for get_dummies" + with pytest.raises(ValueError, match=msg): get_dummies(df, dtype="object") def test_get_dummies_basic(self, sparse, dtype): @@ -296,11 +299,19 @@ def test_dataframe_dummies_prefix_sep(self, df, sparse): tm.assert_frame_equal(result, expected) def test_dataframe_dummies_prefix_bad_length(self, df, sparse): - with pytest.raises(ValueError): + msg = re.escape( + "Length of 'prefix' (1) did not match the length of the columns being " + "encoded (2)" + ) + with pytest.raises(ValueError, match=msg): get_dummies(df, prefix=["too few"], sparse=sparse) def test_dataframe_dummies_prefix_sep_bad_length(self, df, sparse): - with pytest.raises(ValueError): + msg = re.escape( + "Length of 'prefix_sep' (1) did not match the length of the columns being " + "encoded (2)" + ) + with pytest.raises(ValueError, match=msg): get_dummies(df, prefix_sep=["bad"], sparse=sparse) def test_dataframe_dummies_prefix_dict(self, sparse): diff --git a/pandas/tests/reshape/test_union_categoricals.py b/pandas/tests/reshape/test_union_categoricals.py index b44f4844b8e2d..3cd99f13d36a0 100644 --- a/pandas/tests/reshape/test_union_categoricals.py +++ b/pandas/tests/reshape/test_union_categoricals.py @@ -344,5 +344,6 @@ def test_union_categorical_unwrap(self): result = union_categoricals([c1, c2]) tm.assert_categorical_equal(result, expected) - with pytest.raises(TypeError): + msg = "all components to combine must be Categorical" + with pytest.raises(TypeError, match=msg): union_categoricals([c1, ["a", "b", "c"]]) From 3e70e523feeab5d05d2a4517abd0ea96ac55f082 Mon Sep 17 00:00:00 2001 From: moink Date: Wed, 30 Dec 2020 11:17:57 +0100 Subject: [PATCH 2/2] TST: GH30999 add msg to pytest.raises in one test I missed --- pandas/tests/reshape/test_union_categoricals.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/reshape/test_union_categoricals.py b/pandas/tests/reshape/test_union_categoricals.py index 3cd99f13d36a0..8c0c0a1f22760 100644 --- a/pandas/tests/reshape/test_union_categoricals.py +++ b/pandas/tests/reshape/test_union_categoricals.py @@ -275,7 +275,8 @@ def test_union_categoricals_sort(self): c1 = Categorical(["b", "a"], categories=["b", "a", "c"], ordered=True) c2 = Categorical(["a", "c"], categories=["b", "a", "c"], ordered=True) - with pytest.raises(TypeError): + msg = "Cannot use sort_categories=True with ordered Categoricals" + with pytest.raises(TypeError, match=msg): union_categoricals([c1, c2], sort_categories=True) def test_union_categoricals_sort_false(self):