Skip to content

Commit

Permalink
Fix all() unroll for non-generators/non-list comprehensions
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Jun 2, 2019
1 parent e4fe41e commit 4d01c18
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog/5358.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix assertion rewriting of ``all()`` calls to deal with non-generators.
10 changes: 7 additions & 3 deletions src/_pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,9 @@ def visit_Call_35(self, call):
visit `ast.Call` nodes on Python3.5 and after
"""
if isinstance(call.func, ast.Name) and call.func.id == "all":
return self._visit_all(call)
result = self._visit_all(call)
if result is not None:
return result
new_func, func_expl = self.visit(call.func)
arg_expls = []
new_args = []
Expand All @@ -981,7 +983,7 @@ def visit_Call_35(self, call):
def _visit_all(self, call):
"""Special rewrite for the builtin all function, see #5062"""
if not isinstance(call.args[0], (ast.GeneratorExp, ast.ListComp)):
return
return None
gen_exp = call.args[0]
assertion_module = ast.Module(
body=[ast.Assert(test=gen_exp.elt, lineno=1, msg="", col_offset=1)]
Expand Down Expand Up @@ -1010,7 +1012,9 @@ def visit_Call_legacy(self, call):
visit `ast.Call nodes on 3.4 and below`
"""
if isinstance(call.func, ast.Name) and call.func.id == "all":
return self._visit_all(call)
result = self._visit_all(call)
if result is not None:
return result
new_func, func_expl = self.visit(call.func)
arg_expls = []
new_args = []
Expand Down
14 changes: 12 additions & 2 deletions testing/test_assertrewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ def __repr__(self):
assert "UnicodeDecodeError" not in msg
assert "UnicodeEncodeError" not in msg

def test_unroll_generator(self, testdir):
def test_unroll_all_generator(self, testdir):
testdir.makepyfile(
"""
def check_even(num):
Expand All @@ -692,7 +692,7 @@ def test_generator():
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*assert False*", "*where False = check_even(1)*"])

def test_unroll_list_comprehension(self, testdir):
def test_unroll_all_list_comprehension(self, testdir):
testdir.makepyfile(
"""
def check_even(num):
Expand All @@ -707,6 +707,16 @@ def test_list_comprehension():
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*assert False*", "*where False = check_even(1)*"])

def test_unroll_all_object(self, testdir):
testdir.makepyfile(
"""
def test():
assert all((1, 0))
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*assert False*", "*where False = all((1, 0))*"])

def test_for_loop(self, testdir):
testdir.makepyfile(
"""
Expand Down

0 comments on commit 4d01c18

Please sign in to comment.