Fix assertion rewriting involving Starred + side-effects

This commit is contained in:
Anthony Sottile 2018-11-17 10:41:44 -08:00
parent 4c9cde74ab
commit 690a63b921
3 changed files with 16 additions and 1 deletions

View File

@ -0,0 +1 @@
Fix assertion rewriting involving ``Starred`` + side-effects.

View File

@ -946,7 +946,8 @@ class AssertionRewriter(ast.NodeVisitor):
def visit_Starred(self, starred):
# From Python 3.5, a Starred node can appear in a function call
res, expl = self.visit(starred.value)
return starred, "*" + expl
new_starred = ast.Starred(res, starred.ctx)
return new_starred, "*" + expl
def visit_Call_legacy(self, call):
"""

View File

@ -413,6 +413,19 @@ class TestAssertionRewrite(object):
)
testdir.runpytest().assert_outcomes(passed=1)
@pytest.mark.skipif("sys.version_info < (3,5)")
def test_starred_with_side_effect(self, testdir):
"""See #4412"""
testdir.makepyfile(
"""\
def test():
f = lambda x: x
x = iter([1, 2, 3])
assert 2 * next(x) == f(*[next(x)])
"""
)
testdir.runpytest().assert_outcomes(passed=1)
def test_call(self):
def g(a=42, *args, **kwargs):
return False