Skip to content

Commit b4e8769

Browse files
[refactor to use match] AssertionRewriter.visit_BoolOp()
1 parent ef7a432 commit b4e8769

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

src/_pytest/assertion/rewrite.py

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

testing/test_assertrewrite.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,9 @@ def test_simple_failure():
15521552
result.stdout.fnmatch_lines(["*E*assert (1 + 1) == 3"])
15531553

15541554

1555-
class TestIssue10743:
1555+
class TestAssertionRewriteWalrusOperator:
1556+
"""See #10743"""
1557+
15561558
def test_assertion_walrus_operator(self, pytester: Pytester) -> None:
15571559
pytester.makepyfile(
15581560
"""
@@ -1719,6 +1721,22 @@ def test_walrus_operator_not_override_value():
17191721
result = pytester.runpytest()
17201722
assert result.ret == 0
17211723

1724+
def test_assertion_namedexpr_compare_left_overwrite(
1725+
self, pytester: Pytester
1726+
) -> None:
1727+
pytester.makepyfile(
1728+
"""
1729+
def test_namedexpr_compare_left_overwrite():
1730+
a = "Hello"
1731+
b = "World"
1732+
c = "Test"
1733+
assert (a := b) == c and (a := "Test") == "Test"
1734+
"""
1735+
)
1736+
result = pytester.runpytest()
1737+
assert result.ret == 1
1738+
result.stdout.fnmatch_lines(["*assert ('World' == 'Test'*"])
1739+
17221740

17231741
class TestIssue11028:
17241742
def test_assertion_walrus_operator_in_operand(self, pytester: Pytester) -> None:

0 commit comments

Comments
 (0)