From 55a78b6973e33b43954b1a0f854bc79ef6f01b56 Mon Sep 17 00:00:00 2001 From: Benjamin Schubert Date: Mon, 27 Nov 2023 12:52:24 +0000 Subject: [PATCH] Address PR comments --- src/_pytest/_io/pprint.py | 25 ++++++++++++------------- testing/io/test_pprint.py | 39 +++++++++++++++++++++++++++------------ testing/test_assertion.py | 13 +++++++++++++ 3 files changed, 52 insertions(+), 25 deletions(-) diff --git a/src/_pytest/_io/pprint.py b/src/_pytest/_io/pprint.py index af0b87a30..e922d7156 100644 --- a/src/_pytest/_io/pprint.py +++ b/src/_pytest/_io/pprint.py @@ -178,7 +178,7 @@ class PrettyPrinter: return cls = object.__class__ stream.write(cls.__name__ + "(") - self._pprint_dict(object, stream, indent, allowance, context, level) + self._format(list(object.items()), stream, indent, allowance, context, level) stream.write(")") _dispatch[_collections.OrderedDict.__repr__] = _pprint_ordered_dict @@ -373,15 +373,9 @@ class PrettyPrinter: item_indent = indent + self._indent_per_level delimnl = "\n" + " " * item_indent - it = iter(items) - while True: - try: - next_ent = next(it) - except StopIteration: - break - + for item in items: write(delimnl) - self._format(next_ent, stream, item_indent, 1, context, level) + self._format(item, stream, item_indent, 1, context, level) write(",") write("\n" + " " * indent) @@ -412,10 +406,15 @@ class PrettyPrinter: _dispatch[_collections.defaultdict.__repr__] = _pprint_default_dict def _pprint_counter(self, object, stream, indent, allowance, context, level): - stream.write(object.__class__.__name__ + "({") - items = object.most_common() - self._format_dict_items(items, stream, indent, allowance, context, level) - stream.write("})") + stream.write(object.__class__.__name__ + "(") + + if object: + stream.write("{") + items = object.most_common() + self._format_dict_items(items, stream, indent, allowance, context, level) + stream.write("}") + + stream.write(")") _dispatch[_collections.Counter.__repr__] = _pprint_counter diff --git a/testing/io/test_pprint.py b/testing/io/test_pprint.py index fd828cc38..3ed601972 100644 --- a/testing/io/test_pprint.py +++ b/testing/io/test_pprint.py @@ -84,19 +84,28 @@ class DataclassWithTwoItems: pytest.param( OrderedDict({"one": 1}), """ - OrderedDict({ - 'one': 1, - }) + OrderedDict([ + ( + 'one', + 1, + ), + ]) """, id="ordereddict-one-item", ), pytest.param( OrderedDict({"one": 1, "two": 2}), """ - OrderedDict({ - 'one': 1, - 'two': 2, - }) + OrderedDict([ + ( + 'one', + 1, + ), + ( + 'two', + 2, + ), + ]) """, id="ordereddict-two-items", ), @@ -244,7 +253,7 @@ class DataclassWithTwoItems: ), pytest.param( Counter(), - "Counter({})", + "Counter()", id="counter-empty", ), pytest.param( @@ -380,10 +389,16 @@ class DataclassWithTwoItems: 'one': 1, 'two': 2, }), - 'ordereddict': OrderedDict({ - 'one': 1, - 'two': 2, - }), + 'ordereddict': OrderedDict([ + ( + 'one', + 1, + ), + ( + 'two', + 2, + ), + ]), 'set': { 1, 2, diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 114a67f6b..ce10ed8c4 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -754,6 +754,19 @@ class TestAssert_reprcompare: "+ 3,", " )", ] + lines = callequal((1, 2, 3), (1, 20, 3), verbose=2) + assert lines == [ + "(1, 2, 3) == (1, 20, 3)", + "At index 1 diff: 2 != 20", + "Full diff:", + " (", + " 1,", + "- 20,", + "? -", + "+ 2,", + " 3,", + " )", + ] def test_set(self) -> None: expl = callequal({0, 1}, {0, 2})