Skip to content

Commit 2a8b463

Browse files
authored
Merge pull request #5376 from asottile/backport_5373
[4.6] Merge pull request #5373 from asottile/revert_all_handling
2 parents 114dba5 + 12bf458 commit 2a8b463

File tree

5 files changed

+3
-111
lines changed

5 files changed

+3
-111
lines changed

changelog/5370.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Revert unrolling of ``all()`` to fix ``NameError`` on nested comprehensions.

changelog/5371.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Revert unrolling of ``all()`` to fix incorrect handling of generators with ``if``.

changelog/5372.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Revert unrolling of ``all()`` to fix incorrect assertion when using ``all()`` in an expression.

src/_pytest/assertion/rewrite.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -949,22 +949,10 @@ def visit_BinOp(self, binop):
949949
res = self.assign(ast.BinOp(left_expr, binop.op, right_expr))
950950
return res, explanation
951951

952-
@staticmethod
953-
def _is_any_call_with_generator_or_list_comprehension(call):
954-
"""Return True if the Call node is an 'any' call with a generator or list comprehension"""
955-
return (
956-
isinstance(call.func, ast.Name)
957-
and call.func.id == "all"
958-
and len(call.args) == 1
959-
and isinstance(call.args[0], (ast.GeneratorExp, ast.ListComp))
960-
)
961-
962952
def visit_Call_35(self, call):
963953
"""
964954
visit `ast.Call` nodes on Python3.5 and after
965955
"""
966-
if self._is_any_call_with_generator_or_list_comprehension(call):
967-
return self._visit_all(call)
968956
new_func, func_expl = self.visit(call.func)
969957
arg_expls = []
970958
new_args = []
@@ -988,25 +976,6 @@ def visit_Call_35(self, call):
988976
outer_expl = "%s\n{%s = %s\n}" % (res_expl, res_expl, expl)
989977
return res, outer_expl
990978

991-
def _visit_all(self, call):
992-
"""Special rewrite for the builtin all function, see #5062"""
993-
gen_exp = call.args[0]
994-
assertion_module = ast.Module(
995-
body=[ast.Assert(test=gen_exp.elt, lineno=1, msg="", col_offset=1)]
996-
)
997-
AssertionRewriter(module_path=None, config=None).run(assertion_module)
998-
for_loop = ast.For(
999-
iter=gen_exp.generators[0].iter,
1000-
target=gen_exp.generators[0].target,
1001-
body=assertion_module.body,
1002-
orelse=[],
1003-
)
1004-
self.statements.append(for_loop)
1005-
return (
1006-
ast.Num(n=1),
1007-
"",
1008-
) # Return an empty expression, all the asserts are in the for_loop
1009-
1010979
def visit_Starred(self, starred):
1011980
# From Python 3.5, a Starred node can appear in a function call
1012981
res, expl = self.visit(starred.value)
@@ -1017,8 +986,6 @@ def visit_Call_legacy(self, call):
1017986
"""
1018987
visit `ast.Call nodes on 3.4 and below`
1019988
"""
1020-
if self._is_any_call_with_generator_or_list_comprehension(call):
1021-
return self._visit_all(call)
1022989
new_func, func_expl = self.visit(call.func)
1023990
arg_expls = []
1024991
new_args = []

testing/test_assertrewrite.py

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -656,12 +656,6 @@ def __repr__(self):
656656
else:
657657
assert lines == ["assert 0 == 1\n + where 1 = \\n{ \\n~ \\n}.a"]
658658

659-
def test_unroll_expression(self):
660-
def f():
661-
assert all(x == 1 for x in range(10))
662-
663-
assert "0 == 1" in getmsg(f)
664-
665659
def test_custom_repr_non_ascii(self):
666660
def f():
667661
class A(object):
@@ -677,78 +671,6 @@ def __repr__(self):
677671
assert "UnicodeDecodeError" not in msg
678672
assert "UnicodeEncodeError" not in msg
679673

680-
def test_unroll_all_generator(self, testdir):
681-
testdir.makepyfile(
682-
"""
683-
def check_even(num):
684-
if num % 2 == 0:
685-
return True
686-
return False
687-
688-
def test_generator():
689-
odd_list = list(range(1,9,2))
690-
assert all(check_even(num) for num in odd_list)"""
691-
)
692-
result = testdir.runpytest()
693-
result.stdout.fnmatch_lines(["*assert False*", "*where False = check_even(1)*"])
694-
695-
def test_unroll_all_list_comprehension(self, testdir):
696-
testdir.makepyfile(
697-
"""
698-
def check_even(num):
699-
if num % 2 == 0:
700-
return True
701-
return False
702-
703-
def test_list_comprehension():
704-
odd_list = list(range(1,9,2))
705-
assert all([check_even(num) for num in odd_list])"""
706-
)
707-
result = testdir.runpytest()
708-
result.stdout.fnmatch_lines(["*assert False*", "*where False = check_even(1)*"])
709-
710-
def test_unroll_all_object(self, testdir):
711-
"""all() for non generators/non list-comprehensions (#5358)"""
712-
testdir.makepyfile(
713-
"""
714-
def test():
715-
assert all((1, 0))
716-
"""
717-
)
718-
result = testdir.runpytest()
719-
result.stdout.fnmatch_lines(["*assert False*", "*where False = all((1, 0))*"])
720-
721-
def test_unroll_all_starred(self, testdir):
722-
"""all() for non generators/non list-comprehensions (#5358)"""
723-
testdir.makepyfile(
724-
"""
725-
def test():
726-
x = ((1, 0),)
727-
assert all(*x)
728-
"""
729-
)
730-
result = testdir.runpytest()
731-
result.stdout.fnmatch_lines(
732-
["*assert False*", "*where False = all(*((1, 0),))*"]
733-
)
734-
735-
def test_for_loop(self, testdir):
736-
testdir.makepyfile(
737-
"""
738-
def check_even(num):
739-
if num % 2 == 0:
740-
return True
741-
return False
742-
743-
def test_for_loop():
744-
odd_list = list(range(1,9,2))
745-
for num in odd_list:
746-
assert check_even(num)
747-
"""
748-
)
749-
result = testdir.runpytest()
750-
result.stdout.fnmatch_lines(["*assert False*", "*where False = check_even(1)*"])
751-
752674

753675
class TestRewriteOnImport(object):
754676
def test_pycache_is_a_file(self, testdir):

0 commit comments

Comments
 (0)