Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assertion rewriting involving Starred + side-effects #4414

Merged
merged 1 commit into from
Nov 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/4412.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix assertion rewriting involving ``Starred`` + side-effects.
3 changes: 2 additions & 1 deletion src/_pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,8 @@ def visit_Call_35(self, call):
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):
"""
Expand Down
13 changes: 13 additions & 0 deletions testing/test_assertrewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,19 @@ def test_multmat_operator():
)
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
Expand Down