add version check for py<38
This commit is contained in:
parent
0e6d6161f6
commit
3e90bf573f
|
@ -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.
|
||||||
|
|
|
@ -44,6 +44,11 @@ from _pytest.stash import StashKey
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from _pytest.assertion import AssertionState
|
from _pytest.assertion import AssertionState
|
||||||
|
|
||||||
|
if sys.version_info > (3, 8):
|
||||||
|
namedExpr = ast.NamedExpr
|
||||||
|
else:
|
||||||
|
namedExpr = ast.Expr
|
||||||
|
|
||||||
|
|
||||||
assertstate_key = StashKey["AssertionState"]()
|
assertstate_key = StashKey["AssertionState"]()
|
||||||
|
|
||||||
|
@ -937,7 +942,7 @@ class AssertionRewriter(ast.NodeVisitor):
|
||||||
ast.copy_location(node, assert_)
|
ast.copy_location(node, assert_)
|
||||||
return self.statements
|
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
|
# Display the repr of the target name if it's a local variable or
|
||||||
# _should_repr_global_name() thinks it's acceptable.
|
# _should_repr_global_name() thinks it's acceptable.
|
||||||
locs = ast.Call(self.builtin("locals"), [], [])
|
locs = ast.Call(self.builtin("locals"), [], [])
|
||||||
|
@ -1061,7 +1066,7 @@ class AssertionRewriter(ast.NodeVisitor):
|
||||||
results = [left_res]
|
results = [left_res]
|
||||||
for i, op, next_operand in it:
|
for i, op, next_operand in it:
|
||||||
next_res, next_expl = self.visit(next_operand)
|
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})"
|
next_expl = f"({next_expl})"
|
||||||
results.append(next_res)
|
results.append(next_res)
|
||||||
sym = BINOP_MAP[op.__class__]
|
sym = BINOP_MAP[op.__class__]
|
||||||
|
|
|
@ -1265,6 +1265,9 @@ class TestIssue2121:
|
||||||
result.stdout.fnmatch_lines(["*E*assert (1 + 1) == 3"])
|
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:
|
class TestIssue10743:
|
||||||
def test_assertion_walrus_operator(self, pytester: Pytester) -> None:
|
def test_assertion_walrus_operator(self, pytester: Pytester) -> None:
|
||||||
pytester.makepyfile(
|
pytester.makepyfile(
|
||||||
|
|
Loading…
Reference in New Issue