Skip to content

TST: disallow bare pytest raises #34940

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 5 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 4 additions & 2 deletions pandas/tests/frame/methods/test_assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ def test_assign_bad(self):
df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})

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

def test_assign_dependent(self):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/frame/methods/test_at_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def test_at_time_tz(self):
def test_at_time_raises(self):
# GH#20725
df = DataFrame([[1, 2, 3], [4, 5, 6]])
with pytest.raises(TypeError): # index is not a DatetimeIndex
msg = "Index must be DatetimeIndex"
with pytest.raises(TypeError, match=msg): # index is not a DatetimeIndex
df.at_time("00:00")

@pytest.mark.parametrize("axis", ["index", "columns", 0, 1])
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/frame/methods/test_between_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def test_between_time(self, close_open_fixture):
def test_between_time_raises(self):
# GH#20725
df = DataFrame([[1, 2, 3], [4, 5, 6]])
with pytest.raises(TypeError): # index is not a DatetimeIndex
msg = "Index must be DatetimeIndex"
with pytest.raises(TypeError, match=msg): # index is not a DatetimeIndex
df.between_time(start_time="00:00", end_time="12:00")

def test_between_time_axis(self, axis):
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/frame/methods/test_first_and_last.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def test_first_subset(self):
def test_first_raises(self):
# GH#20725
df = DataFrame([[1, 2, 3], [4, 5, 6]])
with pytest.raises(TypeError): # index is not a DatetimeIndex
msg = "'first' only supports a DatetimeIndex index"
with pytest.raises(TypeError, match=msg): # index is not a DatetimeIndex
df.first("1D")

def test_last_subset(self):
Expand All @@ -57,5 +58,6 @@ def test_last_subset(self):
def test_last_raises(self):
# GH20725
df = DataFrame([[1, 2, 3], [4, 5, 6]])
with pytest.raises(TypeError): # index is not a DatetimeIndex
msg = "'last' only supports a DatetimeIndex index"
with pytest.raises(TypeError, match=msg): # index is not a DatetimeIndex
df.last("1D")
15 changes: 13 additions & 2 deletions pandas/tests/frame/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ def test_interp_bad_method(self):
"D": list("abcd"),
}
)
with pytest.raises(ValueError):
msg = (
r"method must be one of \['linear', 'time', 'index', 'values', "
r"'nearest', 'zero', 'slinear', 'quadratic', 'cubic', "
r"'barycentric', 'krogh', 'spline', 'polynomial', "
r"'from_derivatives', 'piecewise_polynomial', 'pchip', 'akima', "
r"'cubicspline'\]. Got 'not_a_method' instead."
)
with pytest.raises(ValueError, match=msg):
df.interpolate(method="not_a_method")

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

@td.skip_if_no_scipy
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/frame/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,11 @@ def test_categorical_replace_with_dict(self, replace_dict, final_data):
expected = DataFrame({"a": a, "b": b})
result = df.replace(replace_dict, 3)
tm.assert_frame_equal(result, expected)
with pytest.raises(AssertionError):
msg = (
r"Attributes of DataFrame.iloc\[:, 0\] \(column name=\"a\"\) are "
"different"
)
with pytest.raises(AssertionError, match=msg):
# ensure non-inplace call does not affect original
tm.assert_frame_equal(df, expected)
df.replace(replace_dict, 3, inplace=True)
Expand Down
42 changes: 29 additions & 13 deletions pandas/tests/frame/test_query_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,14 @@ def test_query_numexpr(self):
result = df.eval("A+1", engine="numexpr")
tm.assert_series_equal(result, self.expected2, check_names=False)
else:
with pytest.raises(ImportError):
msg = (
r"'numexpr' is not installed or an unsupported version. "
r"Cannot use engine='numexpr' for query/eval if 'numexpr' is "
r"not installed"
)
with pytest.raises(ImportError, match=msg):
df.query("A>0", engine="numexpr")
with pytest.raises(ImportError):
with pytest.raises(ImportError, match=msg):
df.eval("A+1", engine="numexpr")


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

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

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

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

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

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

with pytest.raises(UndefinedVariableError, match="name 'df' is not defined"):
Expand Down Expand Up @@ -857,6 +866,7 @@ def test_str_query_method(self, parser, engine):
df = DataFrame(np.random.randn(10, 1), columns=["b"])
df["strings"] = Series(list("aabbccddee"))
expect = df[df.strings == "a"]
msg = r"'(Not)?In' nodes are not implemented"

if parser != "pandas":
col = "strings"
Expand All @@ -870,7 +880,7 @@ def test_str_query_method(self, parser, engine):

for lhs, op, rhs in zip(lhs, ops, rhs):
ex = f"{lhs} {op} {rhs}"
msg = r"'(Not)?In' nodes are not implemented"
# msg = r"'(Not)?In' nodes are not implemented"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't leave this commented out. out of curiosity why move outside if block if message repeated on L911?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left that in there by mistake. Also, I overlooked the if block. I'll put it there. (I had originally put it inside of the for loop)

Copy link
Member

@simonjayhawkins simonjayhawkins Jun 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove this line entirely. we don't want commented out code left behind.

Copy link
Contributor Author

@boweyism boweyism Jun 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I removed it:
336c96d

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still showing in diff. we don't want commented out code left behind.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops.. my bad. test_str_query_method and test_str_LIST_query_method got me confused. I've updated it

with pytest.raises(NotImplementedError, match=msg):
df.query(
ex,
Expand Down Expand Up @@ -898,6 +908,7 @@ def test_str_list_query_method(self, parser, engine):
df = DataFrame(np.random.randn(10, 1), columns=["b"])
df["strings"] = Series(list("aabbccddee"))
expect = df[df.strings.isin(["a", "b"])]
msg = r"'(Not)?In' nodes are not implemented"

if parser != "pandas":
col = "strings"
Expand All @@ -911,7 +922,7 @@ def test_str_list_query_method(self, parser, engine):

for lhs, op, rhs in zip(lhs, ops, rhs):
ex = f"{lhs} {op} {rhs}"
with pytest.raises(NotImplementedError):
with pytest.raises(NotImplementedError, match=msg):
df.query(ex, engine=engine, parser=parser)
else:
res = df.query('strings == ["a", "b"]', engine=engine, parser=parser)
Expand Down Expand Up @@ -946,10 +957,12 @@ def test_query_with_string_columns(self, parser, engine):
expec = df[df.a.isin(df.b) & (df.c < df.d)]
tm.assert_frame_equal(res, expec)
else:
with pytest.raises(NotImplementedError):
msg = r"'(Not)?In' nodes are not implemented"
with pytest.raises(NotImplementedError, match=msg):
df.query("a in b", parser=parser, engine=engine)

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

def test_object_array_eq_ne(self, parser, engine):
Expand Down Expand Up @@ -1186,15 +1199,18 @@ def test_missing_attribute(self, df):
df.eval("@pd.thing")

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

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

def test_failing_hashtag(self, df):
with pytest.raises(SyntaxError):
msg = "Failed to parse backticks"
with pytest.raises(SyntaxError, match=msg):
df.query("`foo#bar` > 4")

def test_call_non_named_expression(self, df):
Expand Down