Skip to content

Commit

Permalink
Rework the PrettyPrinter implementation for full diffs
Browse files Browse the repository at this point in the history
The normal default pretty printer is not great when objects are nested
and it can get hard to read the diff.

Instead, provide a pretty printer that behaves more like when json get
indented, which allows for smaller, more meaningful differences, at
the expense of a slightly longer diff.

This does not touch the other places where the pretty printer is used,
and only updated the full diff one.
  • Loading branch information
BenjaminSchubert committed Oct 30, 2023
1 parent 0e3bf7b commit 2ac6392
Show file tree
Hide file tree
Showing 8 changed files with 647 additions and 316 deletions.
7 changes: 7 additions & 0 deletions changelog/1531.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Improved the very verbose diff for every standard library container types. Previously,
this would use the default python pretty printer, which puts opening and closing
markers on the same line as the first/last entry, in addition to not having
consistent indentation.

The indentation is now consistent and the markers on their own separate lines
which should reduce the diffs shown to users.
250 changes: 94 additions & 156 deletions src/_pytest/_io/pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
# python 3.12.
# It only copies the required parts of the module to get the functionality
# required for pytest.
#
# This was then modified to offer a more stable output, which is easier for
# running comparisons, by having indentation that does not depend on the name
# of the classes, but mostly on the level, and ensuring we get opening and
# closing symbols on separate lines.
#
import collections as _collections
import dataclasses as _dataclasses
import re
Expand Down Expand Up @@ -45,7 +51,7 @@ def _safe_tuple(t):
class PrettyPrinter:
def __init__(
self,
indent=1,
indent=4,
width=80,
depth=None,
stream=None,
Expand Down Expand Up @@ -108,35 +114,31 @@ def _format(self, object, stream, indent, allowance, context, level):
self._recursive = True
self._readable = False
return

Check warning on line 116 in src/_pytest/_io/pprint.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/_io/pprint.py#L113-L116

Added lines #L113 - L116 were not covered by tests
rep = self._repr(object, context, level)
max_width = self._width - indent - allowance
if len(rep) > max_width:
p = self._dispatch.get(type(object).__repr__, None) # type: ignore[arg-type]
if p is not None:
context[objid] = 1
p(self, object, stream, indent, allowance, context, level + 1)
del context[objid]
return
elif (
_dataclasses.is_dataclass(object)
and not isinstance(object, type)
and object.__dataclass_params__.repr
and
# Check dataclass has generated repr method.
hasattr(object.__repr__, "__wrapped__")
and "__create_fn__" in object.__repr__.__wrapped__.__qualname__
):
context[objid] = 1
self._pprint_dataclass(
object, stream, indent, allowance, context, level + 1
)
del context[objid]
return
stream.write(rep)

p = self._dispatch.get(type(object).__repr__, None) # type: ignore[arg-type]
if p is not None:
context[objid] = 1
p(self, object, stream, indent, allowance, context, level + 1)
del context[objid]
elif (
_dataclasses.is_dataclass(object)
and not isinstance(object, type)
and object.__dataclass_params__.repr
and
# Check dataclass has generated repr method.
hasattr(object.__repr__, "__wrapped__")
and "__create_fn__" in object.__repr__.__wrapped__.__qualname__
):
context[objid] = 1
self._pprint_dataclass(
object, stream, indent, allowance, context, level + 1
)
del context[objid]
else:
stream.write(self._repr(object, context, level))

def _pprint_dataclass(self, object, stream, indent, allowance, context, level):
cls_name = object.__class__.__name__
indent += len(cls_name) + 1
items = [
(f.name, getattr(object, f.name))
for f in _dataclasses.fields(object)
Expand All @@ -151,17 +153,11 @@ def _pprint_dataclass(self, object, stream, indent, allowance, context, level):
def _pprint_dict(self, object, stream, indent, allowance, context, level):
write = stream.write
write("{")
if self._indent_per_level > 1:
write((self._indent_per_level - 1) * " ")
length = len(object)
if length:
if self._sort_dicts:
items = sorted(object.items(), key=_safe_tuple)
else:
items = object.items()
self._format_dict_items(
items, stream, indent, allowance + 1, context, level
)
if self._sort_dicts:
items = sorted(object.items(), key=_safe_tuple)
else:
items = object.items()

Check warning on line 159 in src/_pytest/_io/pprint.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/_io/pprint.py#L159

Added line #L159 was not covered by tests
self._format_dict_items(items, stream, indent, allowance, context, level)
write("}")

_dispatch[dict.__repr__] = _pprint_dict
Expand All @@ -172,32 +168,22 @@ def _pprint_ordered_dict(self, object, stream, indent, allowance, context, level
return
cls = object.__class__
stream.write(cls.__name__ + "(")
self._format(
list(object.items()),
stream,
indent + len(cls.__name__) + 1,
allowance + 1,
context,
level,
)
self._pprint_dict(object, stream, indent, allowance, context, level)
stream.write(")")

_dispatch[_collections.OrderedDict.__repr__] = _pprint_ordered_dict

def _pprint_list(self, object, stream, indent, allowance, context, level):
stream.write("[")
self._format_items(object, stream, indent, allowance + 1, context, level)
self._format_items(object, stream, indent, allowance, context, level)
stream.write("]")

_dispatch[list.__repr__] = _pprint_list

def _pprint_tuple(self, object, stream, indent, allowance, context, level):
stream.write("(")
endchar = ",)" if len(object) == 1 else ")"
self._format_items(
object, stream, indent, allowance + len(endchar), context, level
)
stream.write(endchar)
self._format_items(object, stream, indent, allowance, context, level)
stream.write(")")

_dispatch[tuple.__repr__] = _pprint_tuple

Expand All @@ -212,11 +198,8 @@ def _pprint_set(self, object, stream, indent, allowance, context, level):
else:
stream.write(typ.__name__ + "({")
endchar = "})"

Check warning on line 200 in src/_pytest/_io/pprint.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/_io/pprint.py#L199-L200

Added lines #L199 - L200 were not covered by tests
indent += len(typ.__name__) + 1
object = sorted(object, key=_safe_key)
self._format_items(
object, stream, indent, allowance + len(endchar), context, level
)
self._format_items(object, stream, indent, allowance, context, level)
stream.write(endchar)

_dispatch[set.__repr__] = _pprint_set
Expand Down Expand Up @@ -306,7 +289,7 @@ def _pprint_bytearray(self, object, stream, indent, allowance, context, level):

def _pprint_mappingproxy(self, object, stream, indent, allowance, context, level):
stream.write("mappingproxy(")
self._format(object.copy(), stream, indent + 13, allowance + 1, context, level)
self._format(object.copy(), stream, indent, allowance, context, level)
stream.write(")")

_dispatch[_types.MappingProxyType.__repr__] = _pprint_mappingproxy
Expand All @@ -320,7 +303,6 @@ def _pprint_simplenamespace(
cls_name = "namespace"
else:
cls_name = object.__class__.__name__

Check warning on line 305 in src/_pytest/_io/pprint.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/_io/pprint.py#L305

Added line #L305 was not covered by tests
indent += len(cls_name) + 1
items = object.__dict__.items()
stream.write(cls_name + "(")
self._format_namespace_items(items, stream, indent, allowance, context, level)
Expand All @@ -329,32 +311,30 @@ def _pprint_simplenamespace(
_dispatch[_types.SimpleNamespace.__repr__] = _pprint_simplenamespace

def _format_dict_items(self, items, stream, indent, allowance, context, level):
if not items:
return

write = stream.write
indent += self._indent_per_level
delimnl = ",\n" + " " * indent
last_index = len(items) - 1
for i, (key, ent) in enumerate(items):
last = i == last_index
rep = self._repr(key, context, level)
write(rep)
item_indent = indent + self._indent_per_level
delimnl = "\n" + " " * item_indent
for key, ent in items:
write(delimnl)
write(self._repr(key, context, level))
write(": ")
self._format(
ent,
stream,
indent + len(rep) + 2,
allowance if last else 1,
context,
level,
)
if not last:
write(delimnl)
self._format(ent, stream, item_indent, 1, context, level)
write(",")

write("\n" + " " * indent)

def _format_namespace_items(self, items, stream, indent, allowance, context, level):
if not items:
return

write = stream.write
delimnl = ",\n" + " " * indent
last_index = len(items) - 1
for i, (key, ent) in enumerate(items):
last = i == last_index
item_indent = indent + self._indent_per_level
delimnl = "\n" + " " * item_indent
for key, ent in items:
write(delimnl)
write(key)
write("=")
if id(ent) in context:
Expand All @@ -365,52 +345,36 @@ def _format_namespace_items(self, items, stream, indent, allowance, context, lev
self._format(
ent,
stream,
indent + len(key) + 1,
allowance if last else 1,
item_indent + len(key) + 1,
1,
context,
level,
)
if not last:
write(delimnl)

write(",")

write("\n" + " " * indent)

def _format_items(self, items, stream, indent, allowance, context, level):
if not items:
return

write = stream.write
indent += self._indent_per_level
if self._indent_per_level > 1:
write((self._indent_per_level - 1) * " ")
delimnl = ",\n" + " " * indent
delim = ""
width = max_width = self._width - indent + 1
item_indent = indent + self._indent_per_level
delimnl = "\n" + " " * item_indent

it = iter(items)
try:
next_ent = next(it)
except StopIteration:
return
last = False
while not last:
ent = next_ent
while True:
try:
next_ent = next(it)
except StopIteration:
last = True
max_width -= allowance
width -= allowance
if self._compact:
rep = self._repr(ent, context, level)
w = len(rep) + 2
if width < w:
width = max_width
if delim:
delim = delimnl
if width >= w:
width -= w
write(delim)
delim = ", "
write(rep)
continue
write(delim)
delim = delimnl
self._format(ent, stream, indent, allowance if last else 1, context, level)
break

write(delimnl)
self._format(next_ent, stream, item_indent, 1, context, level)
write(",")

write("\n" + " " * indent)

def _repr(self, object, context, level):
repr, readable, recursive = self.format(
Expand All @@ -430,66 +394,40 @@ def format(self, object, context, maxlevels, level):
return self._safe_repr(object, context, maxlevels, level)

def _pprint_default_dict(self, object, stream, indent, allowance, context, level):
if not len(object):
stream.write(repr(object))
return
rdf = self._repr(object.default_factory, context, level)
cls = object.__class__
indent += len(cls.__name__) + 1
stream.write("{}({},\n{}".format(cls.__name__, rdf, " " * indent))
self._pprint_dict(object, stream, indent, allowance + 1, context, level)
stream.write(f"{object.__class__.__name__}({rdf}, ")
self._pprint_dict(object, stream, indent, allowance, context, level)
stream.write(")")

_dispatch[_collections.defaultdict.__repr__] = _pprint_default_dict

def _pprint_counter(self, object, stream, indent, allowance, context, level):
if not len(object):
stream.write(repr(object))
return
cls = object.__class__
stream.write(cls.__name__ + "({")
if self._indent_per_level > 1:
stream.write((self._indent_per_level - 1) * " ")
stream.write(object.__class__.__name__ + "({")
items = object.most_common()
self._format_dict_items(
items, stream, indent + len(cls.__name__) + 1, allowance + 2, context, level
)
self._format_dict_items(items, stream, indent, allowance, context, level)
stream.write("})")

_dispatch[_collections.Counter.__repr__] = _pprint_counter

def _pprint_chain_map(self, object, stream, indent, allowance, context, level):
if not len(object.maps):
if not len(object.maps) or (len(object.maps) == 1 and not len(object.maps[0])):
stream.write(repr(object))
return
cls = object.__class__
stream.write(cls.__name__ + "(")
indent += len(cls.__name__) + 1
for i, m in enumerate(object.maps):
if i == len(object.maps) - 1:
self._format(m, stream, indent, allowance + 1, context, level)
stream.write(")")
else:
self._format(m, stream, indent, 1, context, level)
stream.write(",\n" + " " * indent)

stream.write(object.__class__.__name__ + "(")
self._format_items(object.maps, stream, indent, allowance, context, level)
stream.write(")")

_dispatch[_collections.ChainMap.__repr__] = _pprint_chain_map

def _pprint_deque(self, object, stream, indent, allowance, context, level):
if not len(object):
stream.write(repr(object))
return
cls = object.__class__
stream.write(cls.__name__ + "(")
indent += len(cls.__name__) + 1
stream.write(object.__class__.__name__ + "(")
if object.maxlen is not None:
stream.write("maxlen=%d, " % object.maxlen)
stream.write("[")
if object.maxlen is None:
self._format_items(object, stream, indent, allowance + 2, context, level)
stream.write("])")
else:
self._format_items(object, stream, indent, 2, context, level)
rml = self._repr(object.maxlen, context, level)
stream.write("],\n{}maxlen={})".format(" " * indent, rml))

self._format_items(object, stream, indent, allowance + 1, context, level)
stream.write("])")

_dispatch[_collections.deque.__repr__] = _pprint_deque

Expand Down
Loading

0 comments on commit 2ac6392

Please sign in to comment.