Skip to content

Commit 3c5d8f4

Browse files
committed
Remove ValueError if compact=expand=True, compact takes precendence
1 parent 47d0a5d commit 3c5d8f4

3 files changed

Lines changed: 17 additions & 10 deletions

File tree

Doc/library/pprint.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ Functions
7070
each item of a sequence will be formatted on a separate line,
7171
otherwise as many items as will fit within the *width*
7272
will be formatted on each output line.
73-
Incompatible with *expand*.
73+
Takes precedence over *expand*.
7474

7575
:param bool expand:
76-
If ``True``,
76+
If ``True`` (the default),
7777
opening parentheses and brackets will be followed by a newline and the
7878
following content will be indented by one level, similar to
79-
pretty-printed JSON. Incompatible with *compact*.
79+
pretty-printed JSON. Has no effect when *compact* is also ``True``.
8080

8181
:param bool sort_dicts:
8282
If ``True``, dictionaries will be formatted with
@@ -222,7 +222,9 @@ PrettyPrinter objects
222222

223223
.. versionchanged:: next
224224
Changed default *indent* from 1 to 4,
225-
and default *width* from 80 to 88.
225+
default *width* from 80 to 88,
226+
and default *expand* from ``False`` to ``True``.
227+
*compact* takes precedence over *expand* when both are ``True``.
226228

227229

228230
:class:`PrettyPrinter` instances have the following methods:

Lib/pprint.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,12 @@ def __init__(
159159
160160
compact
161161
If true, several items will be combined in one line.
162-
Incompatible with expand mode.
162+
Takes precedence over expand mode.
163163
164164
expand
165165
If true, the output will be formatted similar to
166166
pretty-printed json.dumps() when ``indent`` is supplied.
167-
Incompatible with compact mode.
167+
Has no effect if compact mode is also enabled.
168168
169169
sort_dicts
170170
If true, dict keys are sorted.
@@ -181,8 +181,6 @@ def __init__(
181181
raise ValueError('depth must be > 0')
182182
if not width:
183183
raise ValueError('width must be != 0')
184-
if compact and expand:
185-
raise ValueError('compact and expand are incompatible')
186184
self._depth = depth
187185
self._indent_per_level = indent
188186
self._width = width
@@ -191,7 +189,7 @@ def __init__(
191189
else:
192190
self._stream = _sys.stdout
193191
self._compact = bool(compact)
194-
self._expand = bool(expand)
192+
self._expand = bool(expand) and not self._compact
195193
self._sort_dicts = sort_dicts
196194
self._underscore_numbers = underscore_numbers
197195

Lib/test/test_pprint.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ def test_init(self):
169169
self.assertRaises(ValueError, pprint.PrettyPrinter, depth=0)
170170
self.assertRaises(ValueError, pprint.PrettyPrinter, depth=-1)
171171
self.assertRaises(ValueError, pprint.PrettyPrinter, width=0)
172-
self.assertRaises(ValueError, pprint.PrettyPrinter, compact=True, expand=True)
173172

174173
def test_basic(self):
175174
# Verify .isrecursive() and .isreadable() w/o recursion
@@ -1164,6 +1163,14 @@ def test_compact(self):
11641163
[0, 1, 2, 3, 4]]"""
11651164
self.assertEqual(_pformat(o, width=47, compact=True), expected)
11661165

1166+
def test_compact_without_explicit_expand(self):
1167+
# Passing compact=True alone should not require also passing
1168+
# expand=False, even though expand defaults to True.
1169+
self.assertEqual(
1170+
pprint.pformat([1, 2, 3, 4, 5], width=10, compact=True),
1171+
"[ 1, 2,\n 3, 4,\n 5]",
1172+
)
1173+
11671174
def test_compact_width(self):
11681175
levels = 20
11691176
number = 10

0 commit comments

Comments
 (0)