Skip to content

Commit 5046151

Browse files
[refactor to use match] AssertionRewriter.visit_BoolOp()
1 parent 3cb1208 commit 5046151

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/_pytest/assertion/rewrite.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,20 +1008,16 @@ def visit_BoolOp(self, boolop: ast.BoolOp) -> tuple[ast.Name, str]:
10081008
# cond is set in a prior loop iteration below
10091009
self.expl_stmts.append(ast.If(cond, fail_inner, [])) # noqa: F821
10101010
self.expl_stmts = fail_inner
1011-
# Check if the left operand is a ast.NamedExpr and the value has already been visited
1012-
if (
1013-
isinstance(v, ast.Compare)
1014-
and isinstance(v.left, ast.NamedExpr)
1015-
and v.left.target.id
1016-
in [
1017-
ast_expr.id
1018-
for ast_expr in boolop.values[:i]
1019-
if hasattr(ast_expr, "id")
1020-
]
1021-
):
1022-
pytest_temp = self.variable()
1023-
self.variables_overwrite[self.scope][v.left.target.id] = v.left # type:ignore[assignment]
1024-
v.left.target.id = pytest_temp
1011+
match v:
1012+
# Check if the left operand is an ast.NamedExpr and the value has already been visited
1013+
case ast.Compare(left=ast.NamedExpr(target=ast.Name(id=target_id))):
1014+
if target_id in [
1015+
e.id for e in boolop.values[:i] if isinstance(e, ast.Name)
1016+
]:
1017+
pytest_temp = self.variable()
1018+
self.variables_overwrite[self.scope][target_id] = v.left # type:ignore[assignment]
1019+
# mypy's false positive, we're checking that the 'target' attribute exists.
1020+
v.left.target.id = pytest_temp # type:ignore[attr-defined]
10251021
self.push_format_context()
10261022
res, expl = self.visit(v)
10271023
body.append(ast.Assign([ast.Name(res_var, ast.Store())], res))

0 commit comments

Comments
 (0)