Skip to content

Commit 1951c8e

Browse files
authored
CLN: Avoid bare pytest.raises in computation/test_eval.py (#32507)
1 parent 09a46a4 commit 1951c8e

File tree

1 file changed

+65
-37
lines changed

1 file changed

+65
-37
lines changed

pandas/tests/computation/test_eval.py

+65-37
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@ def check_pow(self, lhs, arith1, rhs):
375375
and is_scalar(rhs)
376376
and _is_py3_complex_incompat(result, expected)
377377
):
378-
with pytest.raises(AssertionError):
378+
msg = "(DataFrame.columns|numpy array) are different"
379+
with pytest.raises(AssertionError, match=msg):
379380
tm.assert_numpy_array_equal(result, expected)
380381
else:
381382
tm.assert_almost_equal(result, expected)
@@ -449,16 +450,19 @@ def test_frame_invert(self):
449450
# float always raises
450451
lhs = DataFrame(randn(5, 2))
451452
if self.engine == "numexpr":
452-
with pytest.raises(NotImplementedError):
453+
msg = "couldn't find matching opcode for 'invert_dd'"
454+
with pytest.raises(NotImplementedError, match=msg):
453455
result = pd.eval(expr, engine=self.engine, parser=self.parser)
454456
else:
455-
with pytest.raises(TypeError):
457+
msg = "ufunc 'invert' not supported for the input types"
458+
with pytest.raises(TypeError, match=msg):
456459
result = pd.eval(expr, engine=self.engine, parser=self.parser)
457460

458461
# int raises on numexpr
459462
lhs = DataFrame(randint(5, size=(5, 2)))
460463
if self.engine == "numexpr":
461-
with pytest.raises(NotImplementedError):
464+
msg = "couldn't find matching opcode for 'invert"
465+
with pytest.raises(NotImplementedError, match=msg):
462466
result = pd.eval(expr, engine=self.engine, parser=self.parser)
463467
else:
464468
expect = ~lhs
@@ -474,10 +478,11 @@ def test_frame_invert(self):
474478
# object raises
475479
lhs = DataFrame({"b": ["a", 1, 2.0], "c": rand(3) > 0.5})
476480
if self.engine == "numexpr":
477-
with pytest.raises(ValueError):
481+
with pytest.raises(ValueError, match="unknown type object"):
478482
result = pd.eval(expr, engine=self.engine, parser=self.parser)
479483
else:
480-
with pytest.raises(TypeError):
484+
msg = "bad operand type for unary ~: 'str'"
485+
with pytest.raises(TypeError, match=msg):
481486
result = pd.eval(expr, engine=self.engine, parser=self.parser)
482487

483488
def test_series_invert(self):
@@ -488,16 +493,19 @@ def test_series_invert(self):
488493
# float raises
489494
lhs = Series(randn(5))
490495
if self.engine == "numexpr":
491-
with pytest.raises(NotImplementedError):
496+
msg = "couldn't find matching opcode for 'invert_dd'"
497+
with pytest.raises(NotImplementedError, match=msg):
492498
result = pd.eval(expr, engine=self.engine, parser=self.parser)
493499
else:
494-
with pytest.raises(TypeError):
500+
msg = "ufunc 'invert' not supported for the input types"
501+
with pytest.raises(TypeError, match=msg):
495502
result = pd.eval(expr, engine=self.engine, parser=self.parser)
496503

497504
# int raises on numexpr
498505
lhs = Series(randint(5, size=5))
499506
if self.engine == "numexpr":
500-
with pytest.raises(NotImplementedError):
507+
msg = "couldn't find matching opcode for 'invert"
508+
with pytest.raises(NotImplementedError, match=msg):
501509
result = pd.eval(expr, engine=self.engine, parser=self.parser)
502510
else:
503511
expect = ~lhs
@@ -517,10 +525,11 @@ def test_series_invert(self):
517525
# object
518526
lhs = Series(["a", 1, 2.0])
519527
if self.engine == "numexpr":
520-
with pytest.raises(ValueError):
528+
with pytest.raises(ValueError, match="unknown type object"):
521529
result = pd.eval(expr, engine=self.engine, parser=self.parser)
522530
else:
523-
with pytest.raises(TypeError):
531+
msg = "bad operand type for unary ~: 'str'"
532+
with pytest.raises(TypeError, match=msg):
524533
result = pd.eval(expr, engine=self.engine, parser=self.parser)
525534

526535
def test_frame_negate(self):
@@ -541,7 +550,8 @@ def test_frame_negate(self):
541550
# bool doesn't work with numexpr but works elsewhere
542551
lhs = DataFrame(rand(5, 2) > 0.5)
543552
if self.engine == "numexpr":
544-
with pytest.raises(NotImplementedError):
553+
msg = "couldn't find matching opcode for 'neg_bb'"
554+
with pytest.raises(NotImplementedError, match=msg):
545555
result = pd.eval(expr, engine=self.engine, parser=self.parser)
546556
else:
547557
expect = -lhs
@@ -566,7 +576,8 @@ def test_series_negate(self):
566576
# bool doesn't work with numexpr but works elsewhere
567577
lhs = Series(rand(5) > 0.5)
568578
if self.engine == "numexpr":
569-
with pytest.raises(NotImplementedError):
579+
msg = "couldn't find matching opcode for 'neg_bb'"
580+
with pytest.raises(NotImplementedError, match=msg):
570581
result = pd.eval(expr, engine=self.engine, parser=self.parser)
571582
else:
572583
expect = -lhs
@@ -610,7 +621,8 @@ def test_series_pos(self, lhs):
610621
tm.assert_series_equal(expect, result)
611622

612623
def test_scalar_unary(self):
613-
with pytest.raises(TypeError):
624+
msg = "bad operand type for unary ~: 'float'"
625+
with pytest.raises(TypeError, match=msg):
614626
pd.eval("~1.0", engine=self.engine, parser=self.parser)
615627

616628
assert pd.eval("-1.0", parser=self.parser, engine=self.engine) == -1.0
@@ -671,7 +683,8 @@ def test_disallow_scalar_bool_ops(self):
671683

672684
x, a, b, df = np.random.randn(3), 1, 2, DataFrame(randn(3, 2)) # noqa
673685
for ex in exprs:
674-
with pytest.raises(NotImplementedError):
686+
msg = "cannot evaluate scalar only bool ops|'BoolOp' nodes are not"
687+
with pytest.raises(NotImplementedError, match=msg):
675688
pd.eval(ex, engine=self.engine, parser=self.parser)
676689

677690
def test_identical(self):
@@ -772,7 +785,8 @@ def setup_ops(self):
772785

773786
def check_chained_cmp_op(self, lhs, cmp1, mid, cmp2, rhs):
774787
ex1 = f"lhs {cmp1} mid {cmp2} rhs"
775-
with pytest.raises(NotImplementedError):
788+
msg = "'BoolOp' nodes are not implemented"
789+
with pytest.raises(NotImplementedError, match=msg):
776790
pd.eval(ex1, engine=self.engine, parser=self.parser)
777791

778792

@@ -1183,7 +1197,8 @@ def test_bool_ops_with_constants(self):
11831197
def test_4d_ndarray_fails(self):
11841198
x = randn(3, 4, 5, 6)
11851199
y = Series(randn(10))
1186-
with pytest.raises(NotImplementedError):
1200+
msg = "N-dimensional objects, where N > 2, are not supported with eval"
1201+
with pytest.raises(NotImplementedError, match=msg):
11871202
self.eval("x + y", local_dict={"x": x, "y": y})
11881203

11891204
def test_constant(self):
@@ -1232,7 +1247,7 @@ def test_truediv(self):
12321247

12331248
def test_failing_subscript_with_name_error(self):
12341249
df = DataFrame(np.random.randn(5, 3)) # noqa
1235-
with pytest.raises(NameError):
1250+
with pytest.raises(NameError, match="name 'x' is not defined"):
12361251
self.eval("df[x > 2] > 2")
12371252

12381253
def test_lhs_expression_subscript(self):
@@ -1379,7 +1394,8 @@ def test_multi_line_expression(self):
13791394
assert ans is None
13801395

13811396
# multi-line not valid if not all assignments
1382-
with pytest.raises(ValueError):
1397+
msg = "Multi-line expressions are only valid if all expressions contain"
1398+
with pytest.raises(ValueError, match=msg):
13831399
df.eval(
13841400
"""
13851401
a = b + 2
@@ -1474,7 +1490,8 @@ def test_assignment_in_query(self):
14741490
# GH 8664
14751491
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
14761492
df_orig = df.copy()
1477-
with pytest.raises(ValueError):
1493+
msg = "cannot assign without a target object"
1494+
with pytest.raises(ValueError, match=msg):
14781495
df.query("a = 1")
14791496
tm.assert_frame_equal(df, df_orig)
14801497

@@ -1593,19 +1610,21 @@ def test_simple_in_ops(self):
15931610
)
15941611
assert res
15951612
else:
1596-
with pytest.raises(NotImplementedError):
1613+
msg = "'In' nodes are not implemented"
1614+
with pytest.raises(NotImplementedError, match=msg):
15971615
pd.eval("1 in [1, 2]", engine=self.engine, parser=self.parser)
1598-
with pytest.raises(NotImplementedError):
1616+
with pytest.raises(NotImplementedError, match=msg):
15991617
pd.eval("2 in (1, 2)", engine=self.engine, parser=self.parser)
1600-
with pytest.raises(NotImplementedError):
1618+
with pytest.raises(NotImplementedError, match=msg):
16011619
pd.eval("3 in (1, 2)", engine=self.engine, parser=self.parser)
1602-
with pytest.raises(NotImplementedError):
1603-
pd.eval("3 not in (1, 2)", engine=self.engine, parser=self.parser)
1604-
with pytest.raises(NotImplementedError):
1620+
with pytest.raises(NotImplementedError, match=msg):
16051621
pd.eval(
16061622
"[(3,)] in (1, 2, [(3,)])", engine=self.engine, parser=self.parser
16071623
)
1608-
with pytest.raises(NotImplementedError):
1624+
msg = "'NotIn' nodes are not implemented"
1625+
with pytest.raises(NotImplementedError, match=msg):
1626+
pd.eval("3 not in (1, 2)", engine=self.engine, parser=self.parser)
1627+
with pytest.raises(NotImplementedError, match=msg):
16091628
pd.eval(
16101629
"[3] not in (1, 2, [[3]])", engine=self.engine, parser=self.parser
16111630
)
@@ -1664,13 +1683,15 @@ def test_fails_not(self):
16641683
def test_fails_ampersand(self):
16651684
df = DataFrame(np.random.randn(5, 3)) # noqa
16661685
ex = "(df + 2)[df > 1] > 0 & (df > 0)"
1667-
with pytest.raises(NotImplementedError):
1686+
msg = "cannot evaluate scalar only bool ops"
1687+
with pytest.raises(NotImplementedError, match=msg):
16681688
pd.eval(ex, parser=self.parser, engine=self.engine)
16691689

16701690
def test_fails_pipe(self):
16711691
df = DataFrame(np.random.randn(5, 3)) # noqa
16721692
ex = "(df + 2)[df > 1] > 0 | (df > 0)"
1673-
with pytest.raises(NotImplementedError):
1693+
msg = "cannot evaluate scalar only bool ops"
1694+
with pytest.raises(NotImplementedError, match=msg):
16741695
pd.eval(ex, parser=self.parser, engine=self.engine)
16751696

16761697
def test_bool_ops_with_constants(self):
@@ -1679,7 +1700,8 @@ def test_bool_ops_with_constants(self):
16791700
):
16801701
ex = f"{lhs} {op} {rhs}"
16811702
if op in ("and", "or"):
1682-
with pytest.raises(NotImplementedError):
1703+
msg = "'BoolOp' nodes are not implemented"
1704+
with pytest.raises(NotImplementedError, match=msg):
16831705
self.eval(ex)
16841706
else:
16851707
res = self.eval(ex)
@@ -1690,7 +1712,8 @@ def test_simple_bool_ops(self):
16901712
for op, lhs, rhs in product(expr._bool_ops_syms, (True, False), (True, False)):
16911713
ex = f"lhs {op} rhs"
16921714
if op in ("and", "or"):
1693-
with pytest.raises(NotImplementedError):
1715+
msg = "'BoolOp' nodes are not implemented"
1716+
with pytest.raises(NotImplementedError, match=msg):
16941717
pd.eval(ex, engine=self.engine, parser=self.parser)
16951718
else:
16961719
res = pd.eval(ex, engine=self.engine, parser=self.parser)
@@ -1902,19 +1925,21 @@ def test_disallowed_nodes(engine, parser):
19021925
inst = VisitorClass("x + 1", engine, parser)
19031926

19041927
for ops in uns_ops:
1905-
with pytest.raises(NotImplementedError):
1928+
msg = "nodes are not implemented"
1929+
with pytest.raises(NotImplementedError, match=msg):
19061930
getattr(inst, ops)()
19071931

19081932

19091933
def test_syntax_error_exprs(engine, parser):
19101934
e = "s +"
1911-
with pytest.raises(SyntaxError):
1935+
with pytest.raises(SyntaxError, match="invalid syntax"):
19121936
pd.eval(e, engine=engine, parser=parser)
19131937

19141938

19151939
def test_name_error_exprs(engine, parser):
19161940
e = "s + t"
1917-
with pytest.raises(NameError):
1941+
msg = "name 's' is not defined"
1942+
with pytest.raises(NameError, match=msg):
19181943
pd.eval(e, engine=engine, parser=parser)
19191944

19201945

@@ -1973,7 +1998,8 @@ def test_bool_ops_fails_on_scalars(lhs, cmp, rhs, engine, parser):
19731998
ex2 = f"lhs {cmp} mid and mid {cmp} rhs"
19741999
ex3 = f"(lhs {cmp} mid) & (mid {cmp} rhs)"
19752000
for ex in (ex1, ex2, ex3):
1976-
with pytest.raises(NotImplementedError):
2001+
msg = "cannot evaluate scalar only bool ops|'BoolOp' nodes are not"
2002+
with pytest.raises(NotImplementedError, match=msg):
19772003
pd.eval(ex, engine=engine, parser=parser)
19782004

19792005

@@ -2029,7 +2055,8 @@ def test_negate_lt_eq_le(engine, parser):
20292055
tm.assert_frame_equal(result, expected)
20302056

20312057
if parser == "python":
2032-
with pytest.raises(NotImplementedError):
2058+
msg = "'Not' nodes are not implemented"
2059+
with pytest.raises(NotImplementedError, match=msg):
20332060
df.query("not (cat > 0)", engine=engine, parser=parser)
20342061
else:
20352062
result = df.query("not (cat > 0)", engine=engine, parser=parser)
@@ -2041,5 +2068,6 @@ def test_validate_bool_args(self):
20412068
invalid_values = [1, "True", [1, 2, 3], 5.0]
20422069

20432070
for value in invalid_values:
2044-
with pytest.raises(ValueError):
2071+
msg = 'For argument "inplace" expected type bool, received type'
2072+
with pytest.raises(ValueError, match=msg):
20452073
pd.eval("2+2", inplace=value)

0 commit comments

Comments
 (0)