Skip to content

Commit e4a553a

Browse files
authored
TST: disallow bare pytest raises (#34940)
1 parent 9b4fd9b commit e4a553a

File tree

7 files changed

+58
-22
lines changed

7 files changed

+58
-22
lines changed

pandas/tests/frame/methods/test_assign.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ def test_assign_bad(self):
6565
df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
6666

6767
# non-keyword argument
68-
with pytest.raises(TypeError):
68+
msg = r"assign\(\) takes 1 positional argument but 2 were given"
69+
with pytest.raises(TypeError, match=msg):
6970
df.assign(lambda x: x.A)
70-
with pytest.raises(AttributeError):
71+
msg = "'DataFrame' object has no attribute 'C'"
72+
with pytest.raises(AttributeError, match=msg):
7173
df.assign(C=df.A, D=df.A + df.C)
7274

7375
def test_assign_dependent(self):

pandas/tests/frame/methods/test_at_time.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def test_at_time_tz(self):
6565
def test_at_time_raises(self):
6666
# GH#20725
6767
df = DataFrame([[1, 2, 3], [4, 5, 6]])
68-
with pytest.raises(TypeError): # index is not a DatetimeIndex
68+
msg = "Index must be DatetimeIndex"
69+
with pytest.raises(TypeError, match=msg): # index is not a DatetimeIndex
6970
df.at_time("00:00")
7071

7172
@pytest.mark.parametrize("axis", ["index", "columns", 0, 1])

pandas/tests/frame/methods/test_between_time.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ def test_between_time(self, close_open_fixture):
6868
def test_between_time_raises(self):
6969
# GH#20725
7070
df = DataFrame([[1, 2, 3], [4, 5, 6]])
71-
with pytest.raises(TypeError): # index is not a DatetimeIndex
71+
msg = "Index must be DatetimeIndex"
72+
with pytest.raises(TypeError, match=msg): # index is not a DatetimeIndex
7273
df.between_time(start_time="00:00", end_time="12:00")
7374

7475
def test_between_time_axis(self, axis):

pandas/tests/frame/methods/test_first_and_last.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def test_first_subset(self):
3131
def test_first_raises(self):
3232
# GH#20725
3333
df = DataFrame([[1, 2, 3], [4, 5, 6]])
34-
with pytest.raises(TypeError): # index is not a DatetimeIndex
34+
msg = "'first' only supports a DatetimeIndex index"
35+
with pytest.raises(TypeError, match=msg): # index is not a DatetimeIndex
3536
df.first("1D")
3637

3738
def test_last_subset(self):
@@ -57,5 +58,6 @@ def test_last_subset(self):
5758
def test_last_raises(self):
5859
# GH20725
5960
df = DataFrame([[1, 2, 3], [4, 5, 6]])
60-
with pytest.raises(TypeError): # index is not a DatetimeIndex
61+
msg = "'last' only supports a DatetimeIndex index"
62+
with pytest.raises(TypeError, match=msg): # index is not a DatetimeIndex
6163
df.last("1D")

pandas/tests/frame/methods/test_interpolate.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ def test_interp_bad_method(self):
4343
"D": list("abcd"),
4444
}
4545
)
46-
with pytest.raises(ValueError):
46+
msg = (
47+
r"method must be one of \['linear', 'time', 'index', 'values', "
48+
r"'nearest', 'zero', 'slinear', 'quadratic', 'cubic', "
49+
r"'barycentric', 'krogh', 'spline', 'polynomial', "
50+
r"'from_derivatives', 'piecewise_polynomial', 'pchip', 'akima', "
51+
r"'cubicspline'\]. Got 'not_a_method' instead."
52+
)
53+
with pytest.raises(ValueError, match=msg):
4754
df.interpolate(method="not_a_method")
4855

4956
def test_interp_combo(self):
@@ -67,7 +74,11 @@ def test_interp_combo(self):
6774
def test_interp_nan_idx(self):
6875
df = DataFrame({"A": [1, 2, np.nan, 4], "B": [np.nan, 2, 3, 4]})
6976
df = df.set_index("A")
70-
with pytest.raises(NotImplementedError):
77+
msg = (
78+
"Interpolation with NaNs in the index has not been implemented. "
79+
"Try filling those NaNs before interpolating."
80+
)
81+
with pytest.raises(NotImplementedError, match=msg):
7182
df.interpolate(method="values")
7283

7384
@td.skip_if_no_scipy

pandas/tests/frame/methods/test_replace.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,11 @@ def test_categorical_replace_with_dict(self, replace_dict, final_data):
13141314
expected = DataFrame({"a": a, "b": b})
13151315
result = df.replace(replace_dict, 3)
13161316
tm.assert_frame_equal(result, expected)
1317-
with pytest.raises(AssertionError):
1317+
msg = (
1318+
r"Attributes of DataFrame.iloc\[:, 0\] \(column name=\"a\"\) are "
1319+
"different"
1320+
)
1321+
with pytest.raises(AssertionError, match=msg):
13181322
# ensure non-inplace call does not affect original
13191323
tm.assert_frame_equal(df, expected)
13201324
df.replace(replace_dict, 3, inplace=True)

pandas/tests/frame/test_query_eval.py

+28-13
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,14 @@ def test_query_numexpr(self):
7171
result = df.eval("A+1", engine="numexpr")
7272
tm.assert_series_equal(result, self.expected2, check_names=False)
7373
else:
74-
with pytest.raises(ImportError):
74+
msg = (
75+
r"'numexpr' is not installed or an unsupported version. "
76+
r"Cannot use engine='numexpr' for query/eval if 'numexpr' is "
77+
r"not installed"
78+
)
79+
with pytest.raises(ImportError, match=msg):
7580
df.query("A>0", engine="numexpr")
76-
with pytest.raises(ImportError):
81+
with pytest.raises(ImportError, match=msg):
7782
df.eval("A+1", engine="numexpr")
7883

7984

@@ -452,14 +457,16 @@ def test_date_query_with_non_date(self):
452457
result = df.query("dates != nondate", parser=parser, engine=engine)
453458
tm.assert_frame_equal(result, df)
454459

460+
msg = r"Invalid comparison between dtype=datetime64\[ns\] and ndarray"
455461
for op in ["<", ">", "<=", ">="]:
456-
with pytest.raises(TypeError):
462+
with pytest.raises(TypeError, match=msg):
457463
df.query(f"dates {op} nondate", parser=parser, engine=engine)
458464

459465
def test_query_syntax_error(self):
460466
engine, parser = self.engine, self.parser
461467
df = DataFrame({"i": range(10), "+": range(3, 13), "r": range(4, 14)})
462-
with pytest.raises(SyntaxError):
468+
msg = "invalid syntax"
469+
with pytest.raises(SyntaxError, match=msg):
463470
df.query("i - +", engine=engine, parser=parser)
464471

465472
def test_query_scope(self):
@@ -781,7 +788,8 @@ def test_date_index_query_with_NaT_duplicates(self):
781788
df["dates3"] = date_range("1/1/2014", periods=n)
782789
df.loc[np.random.rand(n) > 0.5, "dates1"] = pd.NaT
783790
df.set_index("dates1", inplace=True, drop=True)
784-
with pytest.raises(NotImplementedError):
791+
msg = r"'BoolOp' nodes are not implemented"
792+
with pytest.raises(NotImplementedError, match=msg):
785793
df.query("index < 20130101 < dates3", engine=engine, parser=parser)
786794

787795
def test_nested_scope(self):
@@ -798,7 +806,8 @@ def test_nested_scope(self):
798806
df2 = DataFrame(np.random.randn(5, 3))
799807

800808
# don't have the pandas parser
801-
with pytest.raises(SyntaxError):
809+
msg = r"The '@' prefix is only supported by the pandas parser"
810+
with pytest.raises(SyntaxError, match=msg):
802811
df.query("(@df>0) & (@df2>0)", engine=engine, parser=parser)
803812

804813
with pytest.raises(UndefinedVariableError, match="name 'df' is not defined"):
@@ -867,10 +876,10 @@ def test_str_query_method(self, parser, engine):
867876

868877
eq, ne = "==", "!="
869878
ops = 2 * ([eq] + [ne])
879+
msg = r"'(Not)?In' nodes are not implemented"
870880

871881
for lhs, op, rhs in zip(lhs, ops, rhs):
872882
ex = f"{lhs} {op} {rhs}"
873-
msg = r"'(Not)?In' nodes are not implemented"
874883
with pytest.raises(NotImplementedError, match=msg):
875884
df.query(
876885
ex,
@@ -908,10 +917,11 @@ def test_str_list_query_method(self, parser, engine):
908917

909918
eq, ne = "==", "!="
910919
ops = 2 * ([eq] + [ne])
920+
msg = r"'(Not)?In' nodes are not implemented"
911921

912922
for lhs, op, rhs in zip(lhs, ops, rhs):
913923
ex = f"{lhs} {op} {rhs}"
914-
with pytest.raises(NotImplementedError):
924+
with pytest.raises(NotImplementedError, match=msg):
915925
df.query(ex, engine=engine, parser=parser)
916926
else:
917927
res = df.query('strings == ["a", "b"]', engine=engine, parser=parser)
@@ -946,10 +956,12 @@ def test_query_with_string_columns(self, parser, engine):
946956
expec = df[df.a.isin(df.b) & (df.c < df.d)]
947957
tm.assert_frame_equal(res, expec)
948958
else:
949-
with pytest.raises(NotImplementedError):
959+
msg = r"'(Not)?In' nodes are not implemented"
960+
with pytest.raises(NotImplementedError, match=msg):
950961
df.query("a in b", parser=parser, engine=engine)
951962

952-
with pytest.raises(NotImplementedError):
963+
msg = r"'BoolOp' nodes are not implemented"
964+
with pytest.raises(NotImplementedError, match=msg):
953965
df.query("a in b and c < d", parser=parser, engine=engine)
954966

955967
def test_object_array_eq_ne(self, parser, engine):
@@ -1186,15 +1198,18 @@ def test_missing_attribute(self, df):
11861198
df.eval("@pd.thing")
11871199

11881200
def test_failing_quote(self, df):
1189-
with pytest.raises(SyntaxError):
1201+
msg = r"(Could not convert ).*( to a valid Python identifier.)"
1202+
with pytest.raises(SyntaxError, match=msg):
11901203
df.query("`it's` > `that's`")
11911204

11921205
def test_failing_character_outside_range(self, df):
1193-
with pytest.raises(SyntaxError):
1206+
msg = r"(Could not convert ).*( to a valid Python identifier.)"
1207+
with pytest.raises(SyntaxError, match=msg):
11941208
df.query("`☺` > 4")
11951209

11961210
def test_failing_hashtag(self, df):
1197-
with pytest.raises(SyntaxError):
1211+
msg = "Failed to parse backticks"
1212+
with pytest.raises(SyntaxError, match=msg):
11981213
df.query("`foo#bar` > 4")
11991214

12001215
def test_call_non_named_expression(self, df):

0 commit comments

Comments
 (0)