add version check for py<38

This commit is contained in:
Alessio Izzo 2023-02-22 22:28:18 +01:00
parent 0e6d6161f6
commit 3e90bf573f
No known key found for this signature in database
GPG Key ID: 2B5983EE2D924936
3 changed files with 11 additions and 3 deletions

View File

@ -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.

View File

@ -44,6 +44,11 @@ from _pytest.stash import StashKey
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"]()
@ -937,7 +942,7 @@ class AssertionRewriter(ast.NodeVisitor):
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"), [], [])
@ -1061,7 +1066,7 @@ class AssertionRewriter(ast.NodeVisitor):
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__]

View File

@ -1265,6 +1265,9 @@ class TestIssue2121:
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(