Skip to content

Commit cb481a3

Browse files
committed
assertrepr_compare: prefer same maxsize
Previously it would say: > assert '123456789012...901234567890A' == '1234567890123...901234567890B'" This makes it look like the "3" might be different already. This is clearer, and it is OK to have potentially one less char in the right one: > assert '123456789012...901234567890A' == '123456789012...901234567890B'"
1 parent 300f785 commit cb481a3

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/_pytest/assertion/util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ def isiterable(obj):
119119

120120
def assertrepr_compare(config, op, left, right):
121121
"""Return specialised explanations for some operators/operands"""
122-
width = 80 - 15 - len(op) - 2 # 15 chars indentation, 1 space around op
123-
left_repr = saferepr(left, maxsize=int(width // 2))
124-
right_repr = saferepr(right, maxsize=width - len(left_repr))
122+
maxsize = (80 - 15 - len(op) - 2) // 2 # 15 chars indentation, 1 space around op
123+
left_repr = saferepr(left, maxsize=maxsize)
124+
right_repr = saferepr(right, maxsize=maxsize)
125125

126126
summary = "{} {} {}".format(left_repr, op, right_repr)
127127

testing/test_assertrewrite.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,16 @@ class X:
200200
else:
201201
assert msg == ["assert cls == 42"]
202202

203+
def test_assertrepr_compare_same_width(self, request):
204+
"""Should use same width/truncation with same initial width."""
205+
206+
def f():
207+
assert "1234567890" * 5 + "A" == "1234567890" * 5 + "B"
208+
209+
assert getmsg(f).splitlines()[0] == (
210+
"assert '123456789012...901234567890A' == '123456789012...901234567890B'"
211+
)
212+
203213
def test_dont_rewrite_if_hasattr_fails(self, request):
204214
class Y:
205215
""" A class whos getattr fails, but not with `AttributeError` """

0 commit comments

Comments
 (0)