Skip to content

Revert "TST: Bare pytest raises" in base extension tests #38748

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 3 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 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 tm.external_error_raised(TypeError):
with pytest.raises(TypeError):
# numpy points to ^ operator or logical_xor function instead
left_array - right_array

Expand Down Expand Up @@ -92,29 +92,13 @@ def test_error_invalid_values(data, all_arithmetic_operators):
ops = getattr(s, op)

# invalid scalars
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):
with pytest.raises(TypeError):
ops("foo")

msg = (
"unsupported operand type\\(s\\) for|"
"Concatenation operation is not implemented for NumPy arrays"
)
with pytest.raises(TypeError, match=msg):
with pytest.raises(TypeError):
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
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):
with pytest.raises(TypeError):
ops(pd.Series("foo", index=s.index))
25 changes: 4 additions & 21 deletions pandas/tests/extension/base/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,7 @@ def _check_op(self, s, op, other, op_name, exc=NotImplementedError):
expected = s.combine(other, op)
self.assert_series_equal(result, expected)
else:
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):
with pytest.raises(exc):
op(s, other)

def _check_divmod_op(self, s, op, other, exc=Exception):
Expand All @@ -54,12 +44,7 @@ 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:
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):
with pytest.raises(exc):
divmod(s, other)


Expand Down Expand Up @@ -126,8 +111,7 @@ def test_add_series_with_extension_array(self, data):
def test_error(self, data, all_arithmetic_operators):
# invalid ops
op_name = all_arithmetic_operators
msg = "'[\\w_]+' object has no attribute '[\\w_]+'"
with pytest.raises(AttributeError, match=msg):
with pytest.raises(AttributeError):
getattr(data, op_name)

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

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

def test_compare_scalar(self, data, all_compare_operators):
Expand Down
30 changes: 2 additions & 28 deletions pandas/tests/extension/base/setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,25 +282,8 @@ 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, match=msg):
with pytest.raises(ValueError):
arr[:1] = arr[:2]

def test_setitem_slice_array(self, data):
Expand All @@ -309,17 +292,8 @@ 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, match=msg):
with pytest.raises(ValueError):
arr[0] = arr[[0, 1]]

def test_setitem_preserves_views(self, data):
Expand Down