diff --git a/pandas/tests/base/test_constructors.py b/pandas/tests/base/test_constructors.py index 0b7274399aafc..e27b5c307cd99 100644 --- a/pandas/tests/base/test_constructors.py +++ b/pandas/tests/base/test_constructors.py @@ -53,13 +53,16 @@ def test_invalid_delegation(self): delegate = self.Delegate(self.Delegator()) - with pytest.raises(TypeError): + msg = "You cannot access the property foo" + with pytest.raises(TypeError, match=msg): delegate.foo - with pytest.raises(TypeError): + msg = "The property foo cannot be set" + with pytest.raises(TypeError, match=msg): delegate.foo = 5 - with pytest.raises(TypeError): + msg = "You cannot access the property foo" + with pytest.raises(TypeError, match=msg): delegate.foo() @pytest.mark.skipif(PYPY, reason="not relevant for PyPy") @@ -85,8 +88,8 @@ class T(NoNewAttributesMixin): t._freeze() assert "__frozen" in dir(t) assert getattr(t, "__frozen") - - with pytest.raises(AttributeError): + msg = "You cannot add any new attribute" + with pytest.raises(AttributeError, match=msg): t.b = "test" assert not hasattr(t, "b") @@ -129,7 +132,8 @@ def test_constructor_datetime_outofbound(self, a, klass): # datetime64[non-ns] raise error, other cases result in object dtype # and preserve original data if a.dtype.kind == "M": - with pytest.raises(pd.errors.OutOfBoundsDatetime): + msg = "Out of bounds" + with pytest.raises(pd.errors.OutOfBoundsDatetime, match=msg): klass(a) else: result = klass(a) @@ -138,5 +142,6 @@ def test_constructor_datetime_outofbound(self, a, klass): # Explicit dtype specified # Forced conversion fails for all -> all cases raise error - with pytest.raises(pd.errors.OutOfBoundsDatetime): + msg = "Out of bounds" + with pytest.raises(pd.errors.OutOfBoundsDatetime, match=msg): klass(a, dtype="datetime64[ns]") diff --git a/pandas/tests/base/test_conversion.py b/pandas/tests/base/test_conversion.py index 07a15d0619bb6..46fd1551e6170 100644 --- a/pandas/tests/base/test_conversion.py +++ b/pandas/tests/base/test_conversion.py @@ -303,7 +303,8 @@ def test_array(array, attr, index_or_series): def test_array_multiindex_raises(): idx = pd.MultiIndex.from_product([["A"], ["a", "b"]]) - with pytest.raises(ValueError, match="MultiIndex"): + msg = "MultiIndex has no single backing array" + with pytest.raises(ValueError, match=msg): idx.array @@ -429,11 +430,11 @@ def test_to_numpy_na_value_numpy_dtype(container, values, dtype, na_value, expec def test_to_numpy_kwargs_raises(): # numpy s = pd.Series([1, 2, 3]) - match = r"to_numpy\(\) got an unexpected keyword argument 'foo'" - with pytest.raises(TypeError, match=match): + msg = r"to_numpy\(\) got an unexpected keyword argument 'foo'" + with pytest.raises(TypeError, match=msg): s.to_numpy(foo=True) # extension s = pd.Series([1, 2, 3], dtype="Int64") - with pytest.raises(TypeError, match=match): + with pytest.raises(TypeError, match=msg): s.to_numpy(foo=True) diff --git a/pandas/tests/base/test_ops.py b/pandas/tests/base/test_ops.py index 2693eb12dda71..08ec57bd69ad4 100644 --- a/pandas/tests/base/test_ops.py +++ b/pandas/tests/base/test_ops.py @@ -123,11 +123,11 @@ def check_ops_properties(self, props, filter=None, ignore_failures=False): # an object that is datetimelike will raise a TypeError, # otherwise an AttributeError + msg = "no attribute" err = AttributeError if issubclass(type(o), DatetimeIndexOpsMixin): err = TypeError - - with pytest.raises(err): + with pytest.raises(err, match=msg): getattr(o, op) @pytest.mark.parametrize("klass", [Series, DataFrame]) @@ -211,9 +211,10 @@ def test_none_comparison(self): if is_datetime64_dtype(o) or is_datetime64tz_dtype(o): # Following DatetimeIndex (and Timestamp) convention, # inequality comparisons with Series[datetime64] raise - with pytest.raises(TypeError): + msg = "Invalid comparison" + with pytest.raises(TypeError, match=msg): None > o - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): o > None else: result = None > o @@ -235,7 +236,8 @@ def test_ndarray_compat_properties(self): for p in ["flags", "strides", "itemsize", "base", "data"]: assert not hasattr(o, p) - with pytest.raises(ValueError): + msg = "can only convert an array of size 1 to a Python scalar" + with pytest.raises(ValueError, match=msg): o.item() # len > 1 assert o.ndim == 1 @@ -438,7 +440,8 @@ def test_value_counts_bins(self, index_or_series): s = klass(s_values) # bins - with pytest.raises(TypeError): + msg = "bins argument only works with numeric data" + with pytest.raises(TypeError, match=msg): s.value_counts(bins=1) s1 = Series([1, 1, 2, 3]) @@ -857,7 +860,8 @@ def test_validate_bool_args(self): invalid_values = [1, "True", [1, 2, 3], 5.0] for value in invalid_values: - with pytest.raises(ValueError): + msg = "expected type bool" + with pytest.raises(ValueError, match=msg): self.int_series.drop_duplicates(inplace=value) def test_getitem(self): @@ -870,9 +874,11 @@ def test_getitem(self): assert i[-1] == i[9] - with pytest.raises(IndexError): + msg = "index 20 is out of bounds for axis 0 with size 10" + with pytest.raises(IndexError, match=msg): i[20] - with pytest.raises(IndexError): + msg = "single positional indexer is out-of-bounds" + with pytest.raises(IndexError, match=msg): s.iloc[20] @pytest.mark.parametrize("indexer_klass", [list, pd.Index])