Skip to content

Commit 8d093fe

Browse files
ShaharNavehsimonjayhawkins
authored andcommitted
TST: Insert 'match' to bare pytest raises (#30997)
1 parent ee83988 commit 8d093fe

File tree

5 files changed

+42
-19
lines changed

5 files changed

+42
-19
lines changed

pandas/tests/test_common.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ def test_random_state():
6060
assert com.random_state() is np.random
6161

6262
# Error for floats or strings
63-
with pytest.raises(ValueError):
63+
msg = "random_state must be an integer, a numpy RandomState, or None"
64+
with pytest.raises(ValueError, match=msg):
6465
com.random_state("test")
6566

66-
with pytest.raises(ValueError):
67+
with pytest.raises(ValueError, match=msg):
6768
com.random_state(5.5)
6869

6970

@@ -93,15 +94,17 @@ def test_dict_compat():
9394

9495
def test_standardize_mapping():
9596
# No uninitialized defaultdicts
96-
with pytest.raises(TypeError):
97+
msg = r"to_dict\(\) only accepts initialized defaultdicts"
98+
with pytest.raises(TypeError, match=msg):
9799
com.standardize_mapping(collections.defaultdict)
98100

99101
# No non-mapping subtypes, instance
100-
with pytest.raises(TypeError):
102+
msg = "unsupported type: <class 'list'>"
103+
with pytest.raises(TypeError, match=msg):
101104
com.standardize_mapping([])
102105

103106
# No non-mapping subtypes, class
104-
with pytest.raises(TypeError):
107+
with pytest.raises(TypeError, match=msg):
105108
com.standardize_mapping(list)
106109

107110
fill = {"bad": "data"}

pandas/tests/test_downstream.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,12 @@ def test_missing_required_dependency():
136136
# https://github.com/MacPython/pandas-wheels/pull/50
137137
call = ["python", "-sSE", "-c", "import pandas"]
138138

139-
with pytest.raises(subprocess.CalledProcessError) as exc:
139+
msg = (
140+
r"Command '\['python', '-sSE', '-c', 'import pandas'\]' "
141+
"returned non-zero exit status 1."
142+
)
143+
144+
with pytest.raises(subprocess.CalledProcessError, match=msg) as exc:
140145
subprocess.check_output(call, stderr=subprocess.STDOUT)
141146

142147
output = exc.value.stdout.decode()

pandas/tests/test_errors.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
def test_exception_importable(exc):
2323
from pandas import errors
2424

25-
e = getattr(errors, exc)
26-
assert e is not None
25+
err = getattr(errors, exc)
26+
assert err is not None
2727

2828
# check that we can raise on them
29-
with pytest.raises(e):
30-
raise e()
29+
30+
msg = "^$"
31+
32+
with pytest.raises(err, match=msg):
33+
raise err()
3134

3235

3336
def test_catch_oob():

pandas/tests/test_lib.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ def test_max_len_string_array(self):
2222
assert libwriters.max_len_string_array(arr) == 3
2323

2424
# raises
25-
with pytest.raises(TypeError):
25+
msg = "No matching signature found"
26+
with pytest.raises(TypeError, match=msg):
2627
libwriters.max_len_string_array(arr.astype("U"))
2728

2829
def test_fast_unique_multiple_list_gen_sort(self):
@@ -100,9 +101,11 @@ def test_maybe_indices_to_slice_right_edge(self):
100101
assert not isinstance(maybe_slice, slice)
101102
tm.assert_numpy_array_equal(maybe_slice, indices)
102103

103-
with pytest.raises(IndexError):
104+
msg = "index 100 is out of bounds for axis (0|1) with size 100"
105+
106+
with pytest.raises(IndexError, match=msg):
104107
target[indices]
105-
with pytest.raises(IndexError):
108+
with pytest.raises(IndexError, match=msg):
106109
target[maybe_slice]
107110

108111
indices = np.array([100, 99, 98, 97], dtype=np.int64)
@@ -111,9 +114,9 @@ def test_maybe_indices_to_slice_right_edge(self):
111114
assert not isinstance(maybe_slice, slice)
112115
tm.assert_numpy_array_equal(maybe_slice, indices)
113116

114-
with pytest.raises(IndexError):
117+
with pytest.raises(IndexError, match=msg):
115118
target[indices]
116-
with pytest.raises(IndexError):
119+
with pytest.raises(IndexError, match=msg):
117120
target[maybe_slice]
118121

119122
for case in [[99, 97, 99, 96], [99, 99, 98, 97], [98, 98, 97, 96]]:

pandas/tests/test_take.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -423,16 +423,21 @@ class TestExtensionTake:
423423

424424
def test_bounds_check_large(self):
425425
arr = np.array([1, 2])
426-
with pytest.raises(IndexError):
426+
427+
msg = "indices are out-of-bounds"
428+
with pytest.raises(IndexError, match=msg):
427429
algos.take(arr, [2, 3], allow_fill=True)
428430

429-
with pytest.raises(IndexError):
431+
msg = "index 2 is out of bounds for size 2"
432+
with pytest.raises(IndexError, match=msg):
430433
algos.take(arr, [2, 3], allow_fill=False)
431434

432435
def test_bounds_check_small(self):
433436
arr = np.array([1, 2, 3], dtype=np.int64)
434437
indexer = [0, -1, -2]
435-
with pytest.raises(ValueError):
438+
439+
msg = r"'indices' contains values less than allowed \(-2 < -1\)"
440+
with pytest.raises(ValueError, match=msg):
436441
algos.take(arr, indexer, allow_fill=True)
437442

438443
result = algos.take(arr, indexer)
@@ -446,7 +451,11 @@ def test_take_empty(self, allow_fill):
446451
result = algos.take(arr, [], allow_fill=allow_fill)
447452
tm.assert_numpy_array_equal(arr, result)
448453

449-
with pytest.raises(IndexError):
454+
msg = (
455+
r"cannot do a non-empty take from an empty axes.|"
456+
"indices are out-of-bounds"
457+
)
458+
with pytest.raises(IndexError, match=msg):
450459
algos.take(arr, [0], allow_fill=allow_fill)
451460

452461
def test_take_na_empty(self):

0 commit comments

Comments
 (0)