Skip to content

Commit 9919623

Browse files
TST: Bare pytest raises (pandas-dev#38576)
* test_assert_series_equal.py * test_setitem_slice_mismatch_length_raises * test_setitem_scalar_key_sequence_raise * test_sub * test_arithmetic * black pandas on test_arithmetic.py * black pandas on setitem.py * BaseComparisonOpsTests * extension/base/ops.py * Break too long lines * The numpy error was an external one * Update RegExs to match more tests * Added new error messages * Removed redundant parenthesis in regexps
1 parent 03e1c89 commit 9919623

File tree

4 files changed

+84
-12
lines changed

4 files changed

+84
-12
lines changed

pandas/tests/arrays/boolean/test_arithmetic.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_add_mul(left_array, right_array, opname, exp):
4646

4747

4848
def test_sub(left_array, right_array):
49-
with pytest.raises(TypeError):
49+
with tm.external_error_raised(TypeError):
5050
# numpy points to ^ operator or logical_xor function instead
5151
left_array - right_array
5252

@@ -92,13 +92,29 @@ def test_error_invalid_values(data, all_arithmetic_operators):
9292
ops = getattr(s, op)
9393

9494
# invalid scalars
95-
with pytest.raises(TypeError):
95+
msg = (
96+
"ufunc '\\w+' did not contain a loop with signature matching types|"
97+
"ufunc '\\w+' not supported for the input types, and the inputs could "
98+
"not be safely coerced to any supported types|"
99+
"\\w+ cannot perform the operation \\w+"
100+
)
101+
with pytest.raises(TypeError, match=msg):
96102
ops("foo")
97-
with pytest.raises(TypeError):
103+
104+
msg = (
105+
"unsupported operand type\\(s\\) for|"
106+
"Concatenation operation is not implemented for NumPy arrays"
107+
)
108+
with pytest.raises(TypeError, match=msg):
98109
ops(pd.Timestamp("20180101"))
99110

100111
# invalid array-likes
101112
if op not in ("__mul__", "__rmul__"):
102113
# TODO(extension) numpy's mul with object array sees booleans as numbers
103-
with pytest.raises(TypeError):
114+
msg = (
115+
"unsupported operand type\\(s\\) for|"
116+
'can only concatenate str \\(not "bool"\\) to str|'
117+
"not all arguments converted during string formatting"
118+
)
119+
with pytest.raises(TypeError, match=msg):
104120
ops(pd.Series("foo", index=s.index))

pandas/tests/extension/base/ops.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,17 @@ def _check_op(self, s, op, other, op_name, exc=NotImplementedError):
3030
expected = s.combine(other, op)
3131
self.assert_series_equal(result, expected)
3232
else:
33-
with pytest.raises(exc):
33+
msg = (
34+
"unsupported operand type\\(s\\) for|"
35+
"cannot perform [\\w_]+ with this index type: [\\w_]+|"
36+
"Object with dtype category cannot perform the numpy op [\\w_]+|"
37+
"cannot add [\\w_]+ and [\\w_]+|"
38+
"can't multiply sequence by non-int of type '[\\w_]+'|"
39+
'can only concatenate str \\(not "[\\w_]+"\\) to str|'
40+
"Object with dtype category cannot perform the numpy op [\\w_]+|"
41+
"Concatenation operation is not implemented for NumPy arrays"
42+
)
43+
with pytest.raises(exc, match=msg):
3444
op(s, other)
3545

3646
def _check_divmod_op(self, s, op, other, exc=Exception):
@@ -44,7 +54,12 @@ def _check_divmod_op(self, s, op, other, exc=Exception):
4454
self.assert_series_equal(result_div, expected_div)
4555
self.assert_series_equal(result_mod, expected_mod)
4656
else:
47-
with pytest.raises(exc):
57+
msg = (
58+
"'tuple' object has no attribute 'dtype'|"
59+
"cannot perform __r?divmod__ with this index type|"
60+
"unsupported operand type\\(s\\) for divmod\\(\\)"
61+
)
62+
with pytest.raises(exc, match=msg):
4863
divmod(s, other)
4964

5065

@@ -111,7 +126,8 @@ def test_add_series_with_extension_array(self, data):
111126
def test_error(self, data, all_arithmetic_operators):
112127
# invalid ops
113128
op_name = all_arithmetic_operators
114-
with pytest.raises(AttributeError):
129+
msg = "'[\\w_]+' object has no attribute '[\\w_]+'"
130+
with pytest.raises(AttributeError, match=msg):
115131
getattr(data, op_name)
116132

117133
@pytest.mark.parametrize("box", [pd.Series, pd.DataFrame])
@@ -145,7 +161,8 @@ def _compare_other(self, s, data, op_name, other):
145161

146162
# series
147163
s = pd.Series(data)
148-
with pytest.raises(TypeError):
164+
msg = "not supported between instances of '[\\w._]+' and '[\\w._]+'"
165+
with pytest.raises(TypeError, match=msg):
149166
op(s, other)
150167

151168
def test_compare_scalar(self, data, all_compare_operators):

pandas/tests/extension/base/setitem.py

+28-2
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,25 @@ def test_setitem_loc_iloc_slice(self, data):
282282
self.assert_equal(result, expected)
283283

284284
def test_setitem_slice_mismatch_length_raises(self, data):
285+
# This class is a test mixin class, based on which test class it's mixed
286+
# with the expected error messages can vary. This regular expression
287+
# catches all the variants of those messages. It's formatted as a big OR
288+
# statement: /m1|m2|m3|m4/
289+
290+
msg = (
291+
# pandas.core.arrays.period.PeriodArray
292+
# pandas.core.arrays.datetimes.DatetimeArray
293+
"cannot set using a slice indexer with a different length than the value|"
294+
# string_arrow.ArrowStringArray
295+
"Length of indexer and values mismatch|"
296+
# pandas.tests.extension.decimal.array.DecimalArray
297+
"cannot copy sequence with size \\d to array axis with dimension \\d|"
298+
# All the rest
299+
"could not broadcast input array from "
300+
"shape \\(\\d,?\\) into shape \\(\\d,?\\)"
301+
)
285302
arr = data[:5]
286-
with pytest.raises(ValueError):
303+
with pytest.raises(ValueError, match=msg):
287304
arr[:1] = arr[:2]
288305

289306
def test_setitem_slice_array(self, data):
@@ -292,8 +309,17 @@ def test_setitem_slice_array(self, data):
292309
self.assert_extension_array_equal(arr, data[-5:])
293310

294311
def test_setitem_scalar_key_sequence_raise(self, data):
312+
# Check the comment on test_setitem_slice_mismatch_length_raises for more info.
313+
msg = (
314+
# pandas.core.arrays.string_arrow.ArrowStringArray
315+
"Must pass scalars with scalar indexer|"
316+
# pandas.core.arrays.datetimes.DatetimeArray
317+
"Could not convert object to NumPy datetime|"
318+
# All the rest
319+
"setting an array element with a sequence"
320+
)
295321
arr = data[:5].copy()
296-
with pytest.raises(ValueError):
322+
with pytest.raises(ValueError, match=msg):
297323
arr[0] = arr[[0, 1]]
298324

299325
def test_setitem_preserves_views(self, data):

pandas/tests/util/test_assert_series_equal.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,22 @@ def test_series_equal_exact_for_nonnumeric():
292292
tm.assert_series_equal(s1, s2, check_exact=True)
293293
tm.assert_series_equal(s2, s1, check_exact=True)
294294

295-
with pytest.raises(AssertionError):
295+
msg = """Series are different
296+
297+
Series values are different \\(100\\.0 %\\)
298+
\\[index\\]: \\[0, 1\\]
299+
\\[left\\]: \\[a, b\\]
300+
\\[right\\]: \\[b, a\\]"""
301+
with pytest.raises(AssertionError, match=msg):
296302
tm.assert_series_equal(s1, s3, check_exact=True)
297-
with pytest.raises(AssertionError):
303+
304+
msg = """Series are different
305+
306+
Series values are different \\(100\\.0 %\\)
307+
\\[index\\]: \\[0, 1\\]
308+
\\[left\\]: \\[b, a\\]
309+
\\[right\\]: \\[a, b\\]"""
310+
with pytest.raises(AssertionError, match=msg):
298311
tm.assert_series_equal(s3, s1, check_exact=True)
299312

300313

0 commit comments

Comments
 (0)