Skip to content

TST: Bare pytest raises #38576

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

Merged
merged 14 commits into from
Dec 20, 2020
24 changes: 20 additions & 4 deletions pandas/tests/arrays/boolean/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
25 changes: 21 additions & 4 deletions pandas/tests/extension/base/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)


Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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):
Expand Down
30 changes: 28 additions & 2 deletions pandas/tests/extension/base/setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
17 changes: 15 additions & 2 deletions pandas/tests/util/test_assert_series_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down