Skip to content

Commit 7e78965

Browse files
committed
Merge branch 'logic_brackets'
2 parents c519b95 + 58e5581 commit 7e78965

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838

3939
* Renamed the pytest ``pdb`` module (plugin) into ``debugging``.
4040

41+
* Improve of the test output for logical expression with brackets.
42+
Fixes(`#925`_). Thanks `@DRMacIver`_ for reporting. Thanks to `@RedBeardCode`_
43+
for PR.
44+
4145
*
4246

4347
* ImportErrors in plugins now are a fatal error instead of issuing a
@@ -51,6 +55,7 @@
5155
.. _#1626: https://github.com/pytest-dev/pytest/pull/1626
5256
.. _#1503: https://github.com/pytest-dev/pytest/issues/1503
5357
.. _#1479: https://github.com/pytest-dev/pytest/issues/1479
58+
.. _#925: https://github.com/pytest-dev/pytest/issues/925
5459

5560
.. _@graingert: https://github.com/graingert
5661
.. _@taschini: https://github.com/taschini
@@ -59,6 +64,7 @@
5964
.. _@Vogtinator: https://github.com/Vogtinator
6065
.. _@bagerard: https://github.com/bagerard
6166
.. _@davehunt: https://github.com/davehunt
67+
.. _@DRMacIver: https://github.com/DRMacIver
6268

6369

6470
2.9.2

_pytest/assertion/rewrite.py

Lines changed: 5 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
@@ -855,6 +856,8 @@ def visit_Attribute(self, attr):
855856
def visit_Compare(self, comp):
856857
self.push_format_context()
857858
left_res, left_expl = self.visit(comp.left)
859+
if isinstance(comp.left, (_ast.Compare, _ast.BoolOp)):
860+
left_expl = "({0})".format(left_expl)
858861
res_variables = [self.variable() for i in range(len(comp.ops))]
859862
load_names = [ast.Name(v, ast.Load()) for v in res_variables]
860863
store_names = [ast.Name(v, ast.Store()) for v in res_variables]
@@ -864,6 +867,8 @@ def visit_Compare(self, comp):
864867
results = [left_res]
865868
for i, op, next_operand in it:
866869
next_res, next_expl = self.visit(next_operand)
870+
if isinstance(next_operand, (_ast.Compare, _ast.BoolOp)):
871+
next_expl = "({0})".format(next_expl)
867872
results.append(next_res)
868873
sym = binop_map[op.__class__]
869874
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)