diff --git a/pandas/tests/arrays/boolean/test_arithmetic.py b/pandas/tests/arrays/boolean/test_arithmetic.py index 01de64568a011..72b378f75c936 100644 --- a/pandas/tests/arrays/boolean/test_arithmetic.py +++ b/pandas/tests/arrays/boolean/test_arithmetic.py @@ -46,7 +46,7 @@ def test_add_mul(left_array, right_array, opname, exp): def test_sub(left_array, right_array): - with pytest.raises(TypeError): + with tm.external_error_raised(TypeError): # numpy points to ^ operator or logical_xor function instead left_array - right_array @@ -92,13 +92,29 @@ def test_error_invalid_values(data, all_arithmetic_operators): ops = getattr(s, op) # invalid scalars - with pytest.raises(TypeError): + msg = ( + "ufunc '\\w+' did not contain a loop with signature matching types|" + "ufunc '\\w+' not supported for the input types, and the inputs could " + "not be safely coerced to any supported types|" + "\\w+ cannot perform the operation \\w+" + ) + with pytest.raises(TypeError, match=msg): ops("foo") - with pytest.raises(TypeError): + + msg = ( + "unsupported operand type\\(s\\) for|" + "Concatenation operation is not implemented for NumPy arrays" + ) + with pytest.raises(TypeError, match=msg): ops(pd.Timestamp("20180101")) # invalid array-likes if op not in ("__mul__", "__rmul__"): # TODO(extension) numpy's mul with object array sees booleans as numbers - with pytest.raises(TypeError): + msg = ( + "unsupported operand type\\(s\\) for|" + 'can only concatenate str \\(not "bool"\\) to str|' + "not all arguments converted during string formatting" + ) + with pytest.raises(TypeError, match=msg): ops(pd.Series("foo", index=s.index)) diff --git a/pandas/tests/extension/base/ops.py b/pandas/tests/extension/base/ops.py index c93603398977e..6e65418bde58d 100644 --- a/pandas/tests/extension/base/ops.py +++ b/pandas/tests/extension/base/ops.py @@ -30,7 +30,17 @@ def _check_op(self, s, op, other, op_name, exc=NotImplementedError): expected = s.combine(other, op) self.assert_series_equal(result, expected) else: - with pytest.raises(exc): + msg = ( + "unsupported operand type\\(s\\) for|" + "cannot perform [\\w_]+ with this index type: [\\w_]+|" + "Object with dtype category cannot perform the numpy op [\\w_]+|" + "cannot add [\\w_]+ and [\\w_]+|" + "can't multiply sequence by non-int of type '[\\w_]+'|" + 'can only concatenate str \\(not "[\\w_]+"\\) to str|' + "Object with dtype category cannot perform the numpy op [\\w_]+|" + "Concatenation operation is not implemented for NumPy arrays" + ) + with pytest.raises(exc, match=msg): op(s, other) def _check_divmod_op(self, s, op, other, exc=Exception): @@ -44,7 +54,12 @@ def _check_divmod_op(self, s, op, other, exc=Exception): self.assert_series_equal(result_div, expected_div) self.assert_series_equal(result_mod, expected_mod) else: - with pytest.raises(exc): + msg = ( + "'tuple' object has no attribute 'dtype'|" + "cannot perform __r?divmod__ with this index type|" + "unsupported operand type\\(s\\) for divmod\\(\\)" + ) + with pytest.raises(exc, match=msg): divmod(s, other) @@ -111,7 +126,8 @@ def test_add_series_with_extension_array(self, data): def test_error(self, data, all_arithmetic_operators): # invalid ops op_name = all_arithmetic_operators - with pytest.raises(AttributeError): + msg = "'[\\w_]+' object has no attribute '[\\w_]+'" + with pytest.raises(AttributeError, match=msg): getattr(data, op_name) @pytest.mark.parametrize("box", [pd.Series, pd.DataFrame]) @@ -145,7 +161,8 @@ def _compare_other(self, s, data, op_name, other): # series s = pd.Series(data) - with pytest.raises(TypeError): + msg = "not supported between instances of '[\\w._]+' and '[\\w._]+'" + with pytest.raises(TypeError, match=msg): op(s, other) def test_compare_scalar(self, data, all_compare_operators): diff --git a/pandas/tests/extension/base/setitem.py b/pandas/tests/extension/base/setitem.py index a4e6fc0f78cbb..e77d2b2633e88 100644 --- a/pandas/tests/extension/base/setitem.py +++ b/pandas/tests/extension/base/setitem.py @@ -282,8 +282,25 @@ def test_setitem_loc_iloc_slice(self, data): self.assert_equal(result, expected) def test_setitem_slice_mismatch_length_raises(self, data): + # This class is a test mixin class, based on which test class it's mixed + # with the expected error messages can vary. This regular expression + # catches all the variants of those messages. It's formatted as a big OR + # statement: /m1|m2|m3|m4/ + + msg = ( + # pandas.core.arrays.period.PeriodArray + # pandas.core.arrays.datetimes.DatetimeArray + "cannot set using a slice indexer with a different length than the value|" + # string_arrow.ArrowStringArray + "Length of indexer and values mismatch|" + # pandas.tests.extension.decimal.array.DecimalArray + "cannot copy sequence with size \\d to array axis with dimension \\d|" + # All the rest + "could not broadcast input array from " + "shape \\(\\d,?\\) into shape \\(\\d,?\\)" + ) arr = data[:5] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): arr[:1] = arr[:2] def test_setitem_slice_array(self, data): @@ -292,8 +309,17 @@ def test_setitem_slice_array(self, data): self.assert_extension_array_equal(arr, data[-5:]) def test_setitem_scalar_key_sequence_raise(self, data): + # Check the comment on test_setitem_slice_mismatch_length_raises for more info. + msg = ( + # pandas.core.arrays.string_arrow.ArrowStringArray + "Must pass scalars with scalar indexer|" + # pandas.core.arrays.datetimes.DatetimeArray + "Could not convert object to NumPy datetime|" + # All the rest + "setting an array element with a sequence" + ) arr = data[:5].copy() - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): arr[0] = arr[[0, 1]] def test_setitem_preserves_views(self, data): diff --git a/pandas/tests/util/test_assert_series_equal.py b/pandas/tests/util/test_assert_series_equal.py index ae4523014b01d..6bf461d849b5e 100644 --- a/pandas/tests/util/test_assert_series_equal.py +++ b/pandas/tests/util/test_assert_series_equal.py @@ -292,9 +292,22 @@ def test_series_equal_exact_for_nonnumeric(): tm.assert_series_equal(s1, s2, check_exact=True) tm.assert_series_equal(s2, s1, check_exact=True) - with pytest.raises(AssertionError): + msg = """Series are different + +Series values are different \\(100\\.0 %\\) +\\[index\\]: \\[0, 1\\] +\\[left\\]: \\[a, b\\] +\\[right\\]: \\[b, a\\]""" + with pytest.raises(AssertionError, match=msg): tm.assert_series_equal(s1, s3, check_exact=True) - with pytest.raises(AssertionError): + + msg = """Series are different + +Series values are different \\(100\\.0 %\\) +\\[index\\]: \\[0, 1\\] +\\[left\\]: \\[b, a\\] +\\[right\\]: \\[a, b\\]""" + with pytest.raises(AssertionError, match=msg): tm.assert_series_equal(s3, s1, check_exact=True)