Skip to content

Commit

Permalink
add version check for py<38
Browse files Browse the repository at this point in the history
  • Loading branch information
aless10 committed Feb 22, 2023
1 parent 0e6d616 commit 3e90bf5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion changelog/10743.bugfix.rst
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Fixed different behavior from std lib unittest of asserts with expression that contains the walrus operator in it that changes the value of a variable
Fixed different behavior from std lib unittest of asserts with expression that contains the walrus operator in it that changes the value of a variable.
9 changes: 7 additions & 2 deletions src/_pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
if TYPE_CHECKING:
from _pytest.assertion import AssertionState

if sys.version_info > (3, 8):
namedExpr = ast.NamedExpr
else:
namedExpr = ast.Expr


assertstate_key = StashKey["AssertionState"]()

Expand Down Expand Up @@ -937,7 +942,7 @@ def visit_Assert(self, assert_: ast.Assert) -> List[ast.stmt]:
ast.copy_location(node, assert_)
return self.statements

def visit_NamedExpr(self, name: ast.NamedExpr) -> Tuple[ast.NamedExpr, str]:
def visit_NamedExpr(self, name: namedExpr) -> Tuple[namedExpr, str]:
# Display the repr of the target name if it's a local variable or
# _should_repr_global_name() thinks it's acceptable.
locs = ast.Call(self.builtin("locals"), [], [])
Expand Down Expand Up @@ -1061,7 +1066,7 @@ def visit_Compare(self, comp: ast.Compare) -> Tuple[ast.expr, str]:
results = [left_res]
for i, op, next_operand in it:
next_res, next_expl = self.visit(next_operand)
if isinstance(next_operand, (ast.Compare, ast.BoolOp, ast.NamedExpr)):
if isinstance(next_operand, (ast.Compare, ast.BoolOp)):
next_expl = f"({next_expl})"
results.append(next_res)
sym = BINOP_MAP[op.__class__]
Expand Down
3 changes: 3 additions & 0 deletions testing/test_assertrewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,9 @@ def test_simple_failure():
result.stdout.fnmatch_lines(["*E*assert (1 + 1) == 3"])


@pytest.mark.skipif(
sys.version_info < (3, 8), reason="walrus operator not available in py<38"
)
class TestIssue10743:
def test_assertion_walrus_operator(self, pytester: Pytester) -> None:
pytester.makepyfile(
Expand Down

0 comments on commit 3e90bf5

Please sign in to comment.