Skip to content

gh-89722: Add tests for pprint.pprint #95281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
88 changes: 86 additions & 2 deletions Lib/test/test_pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ def test_nested_indentations(self):
'third': 3}]"""
self.assertEqual(pprint.pformat(o, indent=4, width=41), expected)

expected = "[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n" \
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 you can write a helper function, something like assert_pprint(self, obj, expected, indent=1, width=80, depth=None) (mirroring the defaults of pprint.pprint), that then tests both pprint.pformat to a string and pprint,pprint to a stream. Then use it everywhere instead of duplicating the expected values everywhere. You'll probably need to ignore the trailing newline on the stream version.

" { 'first': 1,\n 'second': 2,\n 'third': 3}]\n"
stream = io.StringIO()
pprint.pprint(o, stream=stream, indent=4, width=41)
self.assertEqual(stream.getvalue(), expected)

def test_width(self):
expected = """\
[[[[[[1, 2, 3],
Expand Down Expand Up @@ -364,10 +370,38 @@ def test_width(self):
'1 '
'2']]]]]""")

expected = "[[[[[[1, 2, 3],\n '1 2']]]],\n {1: [1, 2, 3],\n 2: [12, 34]},\n "\
"'abc def ghi',\n ('ab cd ef',),\n set2({1, 23}),\n [[[[[1, 2, 3],\n " \
"'1 2']]]]]\n"
stream = io.StringIO()
pprint.pprint(o, stream=stream, width=15)
self.assertEqual(stream.getvalue(), expected)
stream = io.StringIO()
pprint.pprint(o, stream=stream, width=16)
stream = io.StringIO()
pprint.pprint(o, stream=stream, width=25)
self.assertEqual(stream.getvalue(), expected)
stream = io.StringIO()
pprint.pprint(o, stream=stream, width=14)
expected = "[[[[[[1,\n 2,\n 3],\n '1 '\n '2']]]],\n {1: [1,\n " \
"2,\n 3],\n 2: [12,\n 34]},\n 'abc def '\n 'ghi',\n ('ab cd '\n " \
"'ef',),\n set2({1,\n 23}),\n [[[[[1,\n 2,\n 3],\n " \
"'1 '\n '2']]]]]\n"

self.assertEqual(stream.getvalue(), expected)

def test_integer(self):
self.assertEqual(pprint.pformat(1234567), '1234567')
self.assertEqual(pprint.pformat(1234567, underscore_numbers=True), '1_234_567')

stream = io.StringIO()
pprint.pprint(1234567, stream=stream)
self.assertEqual(stream.getvalue(), '1234567\n')

stream = io.StringIO()
pprint.pprint(1234567, stream=stream, underscore_numbers=True)
self.assertEqual(stream.getvalue(), '1_234_567\n')

class Temperature(int):
def __new__(cls, celsius_degrees):
return super().__new__(Temperature, celsius_degrees)
Expand All @@ -376,6 +410,10 @@ def __repr__(self):
return f"{kelvin_degrees}°K"
self.assertEqual(pprint.pformat(Temperature(1000)), '1273.15°K')

stream = io.StringIO()
pprint.pprint(Temperature(1000), stream=stream)
self.assertEqual(stream.getvalue(), '1273.15°K\n')

def test_sorted_dict(self):
# Starting in Python 2.5, pprint sorts dict displays by key regardless
# of how small the dictionary may be.
Expand All @@ -396,9 +434,20 @@ def test_sorted_dict(self):

def test_sort_dict(self):
d = dict.fromkeys('cba')
self.assertEqual(pprint.pformat(d, sort_dicts=False), "{'c': None, 'b': None, 'a': None}")

expected_unsorted = "{'c': None, 'b': None, 'a': None}"
expected_unsorted_list = "[{'c': None, 'b': None, 'a': None}, {'c': None, 'b': None, 'a': None}]"
self.assertEqual(pprint.pformat(d, sort_dicts=False), expected_unsorted)
self.assertEqual(pprint.pformat([d, d], sort_dicts=False),
"[{'c': None, 'b': None, 'a': None}, {'c': None, 'b': None, 'a': None}]")
expected_unsorted_list)

stream = io.StringIO()
pprint.pprint(d, stream=stream, sort_dicts=False)
self.assertEqual(stream.getvalue(), expected_unsorted + "\n")

stream = io.StringIO()
pprint.pprint([d, d], stream=stream, sort_dicts=False)
self.assertEqual(stream.getvalue(), expected_unsorted_list + "\n")

def test_ordered_dict(self):
d = collections.OrderedDict()
Expand Down Expand Up @@ -740,13 +789,43 @@ def test_depth(self):
self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict))
self.assertEqual(pprint.pformat(nested_list), repr(nested_list))

stream = io.StringIO()
pprint.pprint(nested_tuple, stream=stream)
expected = f"{repr(nested_tuple)}\n"
self.assertEqual(stream.getvalue(), expected)

stream = io.StringIO()
pprint.pprint(nested_dict, stream=stream)
expected = f"{repr(nested_dict)}\n"
self.assertEqual(stream.getvalue(), expected)

stream = io.StringIO()
pprint.pprint(nested_list, stream=stream)
expected = f"{repr(nested_list)}\n"
self.assertEqual(stream.getvalue(), expected)

lv1_tuple = '(1, (...))'
lv1_dict = '{1: {...}}'
lv1_list = '[1, [...]]'
self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple)
self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict)
self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list)

stream = io.StringIO()
pprint.pprint(nested_tuple, stream=stream, depth=1)
expected = lv1_tuple + "\n"
self.assertEqual(stream.getvalue(), expected)

stream = io.StringIO()
pprint.pprint(nested_dict, stream=stream, depth=1)
expected = lv1_dict + "\n"
self.assertEqual(stream.getvalue(), expected)

stream = io.StringIO()
pprint.pprint(nested_list, stream=stream, depth=1)
expected = lv1_list + "\n"
self.assertEqual(stream.getvalue(), expected)

def test_sort_unorderable_values(self):
# Issue 3976: sorted pprints fail for unorderable values.
n = 20
Expand Down Expand Up @@ -863,6 +942,11 @@ def test_compact(self):
[0, 1, 2, 3, 4]]"""
self.assertEqual(pprint.pformat(o, width=47, compact=True), expected)

stream = io.StringIO()
pprint.pprint(o, stream=stream, width=47, compact=True)
expected_pprint = expected + "\n"
self.assertEqual(stream.getvalue(), expected_pprint)

def test_compact_width(self):
levels = 20
number = 10
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add tests for pprint.pprint arguments.
Loading