From 765f75a8f1d38413fa85ae51f61e6be969aac9d5 Mon Sep 17 00:00:00 2001 From: Tomer Keren Date: Sat, 13 Apr 2019 19:13:29 +0300 Subject: [PATCH] Replace asserts of `any` with an assert in a for --- src/_pytest/assertion/rewrite.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 18506d2e1..68ff2b3ef 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -964,6 +964,8 @@ warn_explicit( """ visit `ast.Call` nodes on Python3.5 and after """ + if call.func.id == "all": + return self.visit_all(call) new_func, func_expl = self.visit(call.func) arg_expls = [] new_args = [] @@ -987,6 +989,22 @@ warn_explicit( outer_expl = "%s\n{%s = %s\n}" % (res_expl, res_expl, expl) return res, outer_expl + def visit_all(self, call): + """Special rewrite for the builtin all function, see #5602""" + if not isinstance(call.args[0], ast.GeneratorExp): + return + gen_exp = call.args[0] + for_loop = ast.For( + iter=gen_exp.generators[0].iter, + target=gen_exp.generators[0].target, + body=[ + self.visit(ast.Assert(test=gen_exp.elt, lineno=1, msg="", col_offset=1)) + ], + ) + ast.fix_missing_locations(for_loop) + for_loop = ast.copy_location(for_loop, call) + return for_loop, "" + def visit_Starred(self, starred): # From Python 3.5, a Starred node can appear in a function call res, expl = self.visit(starred.value)