Skip to content

Commit 0c63762

Browse files
authored
Merge pull request #1654 from tomviner/issue1503/remove_collapse_false
Fixes #1503 no longer collapse false explanations
2 parents 3d263c6 + 77689eb commit 0c63762

File tree

3 files changed

+30
-47
lines changed

3 files changed

+30
-47
lines changed

CHANGELOG.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
deprecated but still present. Thanks to `@RedBeardCode`_ and `@tomviner`_
3232
for PR (`#1626`_).
3333

34+
* Always include full assertion explanation. The previous behaviour was hiding
35+
sub-expressions that happened to be False, assuming this was redundant information.
36+
Thanks `@bagerard`_ for reporting (`#1503`_). Thanks to `@davehunt`_ and
37+
`@tomviner`_ for PR.
38+
39+
*
40+
3441
*
3542

3643
.. _#1580: https://github.com/pytest-dev/pytest/pull/1580
@@ -39,12 +46,15 @@
3946
.. _#460: https://github.com/pytest-dev/pytest/pull/460
4047
.. _#1553: https://github.com/pytest-dev/pytest/issues/1553
4148
.. _#1626: https://github.com/pytest-dev/pytest/pull/1626
49+
.. _#1503: https://github.com/pytest-dev/pytest/issues/1503
4250

4351
.. _@graingert: https://github.com/graingert
4452
.. _@taschini: https://github.com/taschini
4553
.. _@nikratio: https://github.com/nikratio
4654
.. _@RedBeardCode: https://github.com/RedBeardCode
4755
.. _@Vogtinator: https://github.com/Vogtinator
56+
.. _@bagerard: https://github.com/bagerard
57+
.. _@davehunt: https://github.com/davehunt
4858

4959

5060
2.9.2

_pytest/assertion/util.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,44 +38,11 @@ def format_explanation(explanation):
3838
displaying diffs.
3939
"""
4040
explanation = ecu(explanation)
41-
explanation = _collapse_false(explanation)
4241
lines = _split_explanation(explanation)
4342
result = _format_lines(lines)
4443
return u('\n').join(result)
4544

4645

47-
def _collapse_false(explanation):
48-
"""Collapse expansions of False
49-
50-
So this strips out any "assert False\n{where False = ...\n}"
51-
blocks.
52-
"""
53-
where = 0
54-
while True:
55-
start = where = explanation.find("False\n{False = ", where)
56-
if where == -1:
57-
break
58-
level = 0
59-
prev_c = explanation[start]
60-
for i, c in enumerate(explanation[start:]):
61-
if prev_c + c == "\n{":
62-
level += 1
63-
elif prev_c + c == "\n}":
64-
level -= 1
65-
if not level:
66-
break
67-
prev_c = c
68-
else:
69-
raise AssertionError("unbalanced braces: %r" % (explanation,))
70-
end = start + i
71-
where = end
72-
if explanation[end - 1] == '\n':
73-
explanation = (explanation[:start] + explanation[start+15:end-1] +
74-
explanation[end+1:])
75-
where -= 17
76-
return explanation
77-
78-
7946
def _split_explanation(explanation):
8047
"""Return a list of individual lines in the explanation
8148

testing/test_assertrewrite.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,12 @@ def x():
213213
return False
214214
def f():
215215
assert x() and x()
216-
assert getmsg(f, {"x" : x}) == "assert (x())"
216+
assert getmsg(f, {"x" : x}) == """assert (False)
217+
+ where False = x()"""
217218
def f():
218219
assert False or x()
219-
assert getmsg(f, {"x" : x}) == "assert (False or x())"
220+
assert getmsg(f, {"x" : x}) == """assert (False or False)
221+
+ where False = x()"""
220222
def f():
221223
assert 1 in {} and 2 in {}
222224
assert getmsg(f) == "assert (1 in {})"
@@ -299,27 +301,34 @@ def g(a=42, *args, **kwargs):
299301
ns = {"g" : g}
300302
def f():
301303
assert g()
302-
assert getmsg(f, ns) == """assert g()"""
304+
assert getmsg(f, ns) == """assert False
305+
+ where False = g()"""
303306
def f():
304307
assert g(1)
305-
assert getmsg(f, ns) == """assert g(1)"""
308+
assert getmsg(f, ns) == """assert False
309+
+ where False = g(1)"""
306310
def f():
307311
assert g(1, 2)
308-
assert getmsg(f, ns) == """assert g(1, 2)"""
312+
assert getmsg(f, ns) == """assert False
313+
+ where False = g(1, 2)"""
309314
def f():
310315
assert g(1, g=42)
311-
assert getmsg(f, ns) == """assert g(1, g=42)"""
316+
assert getmsg(f, ns) == """assert False
317+
+ where False = g(1, g=42)"""
312318
def f():
313319
assert g(1, 3, g=23)
314-
assert getmsg(f, ns) == """assert g(1, 3, g=23)"""
320+
assert getmsg(f, ns) == """assert False
321+
+ where False = g(1, 3, g=23)"""
315322
def f():
316323
seq = [1, 2, 3]
317324
assert g(*seq)
318-
assert getmsg(f, ns) == """assert g(*[1, 2, 3])"""
325+
assert getmsg(f, ns) == """assert False
326+
+ where False = g(*[1, 2, 3])"""
319327
def f():
320328
x = "a"
321329
assert g(**{x : 2})
322-
assert getmsg(f, ns) == """assert g(**{'a': 2})"""
330+
assert getmsg(f, ns) == """assert False
331+
+ where False = g(**{'a': 2})"""
323332

324333
def test_attribute(self):
325334
class X(object):
@@ -332,7 +341,8 @@ def f():
332341
def f():
333342
x.a = False # noqa
334343
assert x.a # noqa
335-
assert getmsg(f, ns) == """assert x.a"""
344+
assert getmsg(f, ns) == """assert False
345+
+ where False = x.a"""
336346

337347
def test_comparisons(self):
338348
def f():
@@ -710,7 +720,3 @@ def test_long_repr():
710720
""")
711721
result = testdir.runpytest()
712722
assert 'unbalanced braces' not in result.stdout.str()
713-
714-
715-
def test_collapse_false_unbalanced_braces():
716-
util._collapse_false('some text{ False\n{False = some more text\n}')

0 commit comments

Comments
 (0)