Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ Oliver Bestwalter
Omar Kohl
Omer Hadari
Ondřej Súkup
Oscar Benjamin
Patrick Hayes
Paweł Adamczak
Pedro Algarvio
Expand Down
1 change: 1 addition & 0 deletions changelog/2256.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Show full repr with ``assert a==b`` and ``-vv``.
14 changes: 14 additions & 0 deletions src/_pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def isiterable(obj):
elif type(left) == type(right) and (isdatacls(left) or isattrs(left)):
type_fn = (isdatacls, isattrs)
explanation = _compare_eq_cls(left, right, verbose, type_fn)
elif verbose:
explanation = _compare_eq_verbose(left, right)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This uses it with -v already, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That's also what happens in the case of e.g. strings: https://github.com/pytest-dev/pytest/blob/master/src/_pytest/assertion/util.py#L176

The filtering based on -vv happens down the line somewhere else (in all cases).

So with -v this function will generate full output but pytest won't ultimately show the whole of it unless -vv is given.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's confusing - especially when looking at the test.
Can this be fixed here (in this PR) also?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is showing the output of call_equal which calls these functions directly. Shortening (without -vv) takes place here I think: https://github.com/pytest-dev/pytest/blob/master/src/_pytest/assertion/truncate.py

That looks like a deliberate design and seems reasonable to me so I'm not sure what needs fixing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense that truncation happens later down the line, otherwise every implementation would need to implement or call truncation (IIUC).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But yeah, I was also confused for this same reason, I had to look at the rest of the code to understand what was going on.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicoddemus
I think we should create a follow-up issue/PR for this.
Have you looked closer at it already?

if isiterable(left) and isiterable(right):
expl = _compare_eq_iterable(left, right, verbose)
if explanation is not None:
Expand Down Expand Up @@ -236,6 +238,18 @@ def escape_for_readable_diff(binary_text):
return explanation


def _compare_eq_verbose(left, right):
keepends = True
left_lines = repr(left).splitlines(keepends)
right_lines = repr(right).splitlines(keepends)

explanation = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unified_diff imported but not used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I've removed that now

explanation += [u"-" + line for line in left_lines]
explanation += [u"+" + line for line in right_lines]

return explanation


def _compare_eq_iterable(left, right, verbose=False):
if not verbose:
return [u"Use -v to get the full diff"]
Expand Down
24 changes: 24 additions & 0 deletions testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,30 @@ def test_list_tuples(self):
expl = callequal([(1, 2)], [])
assert len(expl) > 1

def test_repr_verbose(self):
class Nums:
def __init__(self, nums):
self.nums = nums

def __repr__(self):
return str(self.nums)

list_x = list(range(5000))
list_y = list(range(5000))
list_y[len(list_y) // 2] = 3
nums_x = Nums(list_x)
nums_y = Nums(list_y)

assert callequal(nums_x, nums_y) is None

expl = callequal(nums_x, nums_y, verbose=1)
assert "-" + repr(nums_x) in expl
assert "+" + repr(nums_y) in expl

expl = callequal(nums_x, nums_y, verbose=2)
assert "-" + repr(nums_x) in expl
assert "+" + repr(nums_y) in expl

def test_list_bad_repr(self):
class A(object):
def __repr__(self):
Expand Down