From 2125d04501e00991f39460579f6415a239a43c4f Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 3 Jun 2019 08:34:25 -0700 Subject: [PATCH] Revert "Fix all() unroll for non-generators/non-list comprehensions (#5360)" This reverts commit 733f43b02eafe2934c2e86b7d0370e25dfe95a48, reversing changes made to e4fe41ebb754b3779a1535d43119ef3492ed6dcf. --- changelog/5358.bugfix.rst | 1 - src/_pytest/assertion/rewrite.py | 14 +++----------- testing/test_assertrewrite.py | 29 ++--------------------------- 3 files changed, 5 insertions(+), 39 deletions(-) delete mode 100644 changelog/5358.bugfix.rst diff --git a/changelog/5358.bugfix.rst b/changelog/5358.bugfix.rst deleted file mode 100644 index 181da1e0e..000000000 --- a/changelog/5358.bugfix.rst +++ /dev/null @@ -1 +0,0 @@ -Fix assertion rewriting of ``all()`` calls to deal with non-generators. diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 9b431b984..27dcb58ee 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -903,21 +903,11 @@ warn_explicit( res = self.assign(ast.BinOp(left_expr, binop.op, right_expr)) return res, explanation - @staticmethod - def _is_any_call_with_generator_or_list_comprehension(call): - """Return True if the Call node is an 'any' call with a generator or list comprehension""" - return ( - isinstance(call.func, ast.Name) - and call.func.id == "all" - and len(call.args) == 1 - and isinstance(call.args[0], (ast.GeneratorExp, ast.ListComp)) - ) - def visit_Call(self, call): """ visit `ast.Call` nodes """ - if self._is_any_call_with_generator_or_list_comprehension(call): + if isinstance(call.func, ast.Name) and call.func.id == "all": return self._visit_all(call) new_func, func_expl = self.visit(call.func) arg_expls = [] @@ -944,6 +934,8 @@ warn_explicit( 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 gen_exp = call.args[0] assertion_module = ast.Module( body=[ast.Assert(test=gen_exp.elt, lineno=1, msg="", col_offset=1)] diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 6720a790f..1c309f50c 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -656,7 +656,7 @@ class TestAssertionRewrite: assert "UnicodeDecodeError" not in msg assert "UnicodeEncodeError" not in msg - def test_unroll_all_generator(self, testdir): + def test_unroll_generator(self, testdir): testdir.makepyfile( """ def check_even(num): @@ -671,7 +671,7 @@ class TestAssertionRewrite: result = testdir.runpytest() result.stdout.fnmatch_lines(["*assert False*", "*where False = check_even(1)*"]) - def test_unroll_all_list_comprehension(self, testdir): + def test_unroll_list_comprehension(self, testdir): testdir.makepyfile( """ def check_even(num): @@ -686,31 +686,6 @@ class TestAssertionRewrite: result = testdir.runpytest() result.stdout.fnmatch_lines(["*assert False*", "*where False = check_even(1)*"]) - def test_unroll_all_object(self, testdir): - """all() for non generators/non list-comprehensions (#5358)""" - testdir.makepyfile( - """ - def test(): - assert all((1, 0)) - """ - ) - result = testdir.runpytest() - result.stdout.fnmatch_lines(["*assert False*", "*where False = all((1, 0))*"]) - - def test_unroll_all_starred(self, testdir): - """all() for non generators/non list-comprehensions (#5358)""" - testdir.makepyfile( - """ - def test(): - x = ((1, 0),) - assert all(*x) - """ - ) - result = testdir.runpytest() - result.stdout.fnmatch_lines( - ["*assert False*", "*where False = all(*((1, 0),))*"] - ) - def test_for_loop(self, testdir): testdir.makepyfile( """