| 1 | diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py |
| 2 | --- a/src/_pytest/assertion/rewrite.py |
| 3 | +++ b/src/_pytest/assertion/rewrite.py |
| 4 | @@ -964,6 +964,8 @@ def visit_Call_35(self, call): |
| 5 | """ |
| 6 | visit `ast.Call` nodes on Python3.5 and after |
| 7 | """ |
| 8 | + if isinstance(call.func, ast.Name) and call.func.id == "all": |
| 9 | + return self._visit_all(call) |
| 10 | new_func, func_expl = self.visit(call.func) |
| 11 | arg_expls = [] |
| 12 | new_args = [] |
| 13 | @@ -987,6 +989,27 @@ def visit_Call_35(self, call): |
| 14 | outer_expl = "%s\n{%s = %s\n}" % (res_expl, res_expl, expl) |
| 15 | return res, outer_expl |
| 16 | |
| 17 | + def _visit_all(self, call): |
| 18 | + """Special rewrite for the builtin all function, see #5062""" |
| 19 | + if not isinstance(call.args[0], (ast.GeneratorExp, ast.ListComp)): |
| 20 | + return |
| 21 | + gen_exp = call.args[0] |
| 22 | + assertion_module = ast.Module( |
| 23 | + body=[ast.Assert(test=gen_exp.elt, lineno=1, msg="", col_offset=1)] |
| 24 | + ) |
| 25 | + AssertionRewriter(module_path=None, config=None).run(assertion_module) |
| 26 | + for_loop = ast.For( |
| 27 | + iter=gen_exp.generators[0].iter, |
| 28 | + target=gen_exp.generators[0].target, |
| 29 | + body=assertion_module.body, |
| 30 | + orelse=[], |
| 31 | + ) |
| 32 | + self.statements.append(for_loop) |
| 33 | + return ( |
| 34 | + ast.Num(n=1), |
| 35 | + "", |
| 36 | + ) # Return an empty expression, all the asserts are in the for_loop |
| 37 | + |
| 38 | def visit_Starred(self, starred): |
| 39 | # From Python 3.5, a Starred node can appear in a function call |
| 40 | res, expl = self.visit(starred.value) |
| 41 | @@ -997,6 +1020,8 @@ def visit_Call_legacy(self, call): |
| 42 | """ |
| 43 | visit `ast.Call nodes on 3.4 and below` |
| 44 | """ |
| 45 | + if isinstance(call.func, ast.Name) and call.func.id == "all": |
| 46 | + return self._visit_all(call) |
| 47 | new_func, func_expl = self.visit(call.func) |
| 48 | arg_expls = [] |
| 49 | new_args = [] |