From 690a63b9218f72662cd3a67c6c200b758c88ce12 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 17 Nov 2018 10:41:44 -0800 Subject: [PATCH] Fix assertion rewriting involving Starred + side-effects --- changelog/4412.bugfix.rst | 1 + src/_pytest/assertion/rewrite.py | 3 ++- testing/test_assertrewrite.py | 13 +++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 changelog/4412.bugfix.rst diff --git a/changelog/4412.bugfix.rst b/changelog/4412.bugfix.rst new file mode 100644 index 000000000..7a28b6108 --- /dev/null +++ b/changelog/4412.bugfix.rst @@ -0,0 +1 @@ +Fix assertion rewriting involving ``Starred`` + side-effects. diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index ecb24ff7c..7b9aa5006 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -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): """ diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index acbabb68b..a02433cd6 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -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