Skip to content

Commit 965715b

Browse files
committed
Improve of the test output for logical expression with brackets.
Fixes #925
1 parent 0c63762 commit 965715b

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
Thanks `@bagerard`_ for reporting (`#1503`_). Thanks to `@davehunt`_ and
3737
`@tomviner`_ for PR.
3838

39-
*
39+
* Improve of the test output for logical expression with brackets.
40+
Fixes(`#925`_). Thanks `@DRMacIver`_ for reporting. Thanks to `@RedBeardCode`_
41+
for PR.
4042

4143
*
4244

@@ -47,6 +49,7 @@
4749
.. _#1553: https://github.com/pytest-dev/pytest/issues/1553
4850
.. _#1626: https://github.com/pytest-dev/pytest/pull/1626
4951
.. _#1503: https://github.com/pytest-dev/pytest/issues/1503
52+
.. _#925: https://github.com/pytest-dev/pytest/issues/925
5053

5154
.. _@graingert: https://github.com/graingert
5255
.. _@taschini: https://github.com/taschini
@@ -55,6 +58,7 @@
5558
.. _@Vogtinator: https://github.com/Vogtinator
5659
.. _@bagerard: https://github.com/bagerard
5760
.. _@davehunt: https://github.com/davehunt
61+
.. _@DRMacIver: https://github.com/DRMacIver
5862

5963

6064
2.9.2

_pytest/assertion/rewrite.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Rewrite assertion AST to produce nice error messages"""
22

33
import ast
4+
import _ast
45
import errno
56
import itertools
67
import imp
@@ -753,6 +754,8 @@ def visit_BoolOp(self, boolop):
753754
self.statements = save
754755
self.on_failure = fail_save
755756
expl_template = self.helper("format_boolop", expl_list, ast.Num(is_or))
757+
#if isinstance(boolop, (_ast.Compare, _ast.BoolOp)):
758+
# expl_template = "({0})".format(expl_template)
756759
expl = self.pop_format_context(expl_template)
757760
return ast.Name(res_var, ast.Load()), self.explanation_param(expl)
758761

@@ -855,6 +858,8 @@ def visit_Attribute(self, attr):
855858
def visit_Compare(self, comp):
856859
self.push_format_context()
857860
left_res, left_expl = self.visit(comp.left)
861+
if isinstance(comp.left, (_ast.Compare, _ast.BoolOp)):
862+
left_expl = "({0})".format(left_expl)
858863
res_variables = [self.variable() for i in range(len(comp.ops))]
859864
load_names = [ast.Name(v, ast.Load()) for v in res_variables]
860865
store_names = [ast.Name(v, ast.Store()) for v in res_variables]
@@ -864,6 +869,8 @@ def visit_Compare(self, comp):
864869
results = [left_res]
865870
for i, op, next_operand in it:
866871
next_res, next_expl = self.visit(next_operand)
872+
if isinstance(next_operand, (_ast.Compare, _ast.BoolOp)):
873+
next_expl = "({0})".format(next_expl)
867874
results.append(next_res)
868875
sym = binop_map[op.__class__]
869876
syms.append(ast.Str(sym))

testing/test_assertrewrite.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,3 +720,30 @@ def test_long_repr():
720720
""")
721721
result = testdir.runpytest()
722722
assert 'unbalanced braces' not in result.stdout.str()
723+
724+
725+
class TestIssue925():
726+
def test_simple_case(self, testdir):
727+
testdir.makepyfile("""
728+
def test_ternary_display():
729+
assert (False == False) == False
730+
""")
731+
result = testdir.runpytest()
732+
result.stdout.fnmatch_lines('*E*assert (False == False) == False')
733+
734+
def test_long_case(self, testdir):
735+
testdir.makepyfile("""
736+
def test_ternary_display():
737+
assert False == (False == True) == True
738+
""")
739+
result = testdir.runpytest()
740+
result.stdout.fnmatch_lines('*E*assert (False == True) == True')
741+
742+
def test_many_brackets(self, testdir):
743+
testdir.makepyfile("""
744+
def test_ternary_display():
745+
assert True == ((False == True) == True)
746+
""")
747+
result = testdir.runpytest()
748+
result.stdout.fnmatch_lines('*E*assert True == ((False == True) == True)')
749+

0 commit comments

Comments
 (0)