Skip to content

Commit af663a8

Browse files
authored
Rename block_style to expand (#1)
1 parent ed2fcda commit af663a8

File tree

5 files changed

+92
-91
lines changed

5 files changed

+92
-91
lines changed

Doc/library/pprint.rst

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Functions
3636
---------
3737

3838
.. function:: pp(object, stream=None, indent=1, width=80, depth=None, *, \
39-
block_style=False, compact=False, sort_dicts=False, \
39+
compact=False, expand=False, sort_dicts=False, \
4040
underscore_numbers=False)
4141

4242
Prints the formatted representation of *object*, followed by a newline.
@@ -75,6 +75,13 @@ Functions
7575
each item of a sequence will be formatted on a separate line,
7676
otherwise as many items as will fit within the *width*
7777
will be formatted on each output line.
78+
Incompatible with *expand*.
79+
80+
:param bool expand:
81+
If ``True``,
82+
opening parentheses and brackets will be followed by a newline and the
83+
following content will be indented by one level, similar to block style
84+
JSON formatting. Incompatible with *compact*.
7885

7986
:param bool sort_dicts:
8087
If ``True``, dictionaries will be formatted with
@@ -86,12 +93,6 @@ Functions
8693
integers will be formatted with the ``_`` character for a thousands separator,
8794
otherwise underscores are not displayed (the default).
8895

89-
:param bool block_style:
90-
If ``True``,
91-
opening parentheses and brackets will be followed by a newline and the
92-
following content will be indented by one level, similar to block style
93-
JSON formatting. This option is not compatible with *compact*.
94-
9596
>>> import pprint
9697
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
9798
>>> stuff.insert(0, stuff)
@@ -107,7 +108,7 @@ Functions
107108

108109

109110
.. function:: pprint(object, stream=None, indent=1, width=80, depth=None, *, \
110-
block_style=False, compact=False, sort_dicts=True, \
111+
compact=False, expand=False, sort_dicts=True, \
111112
underscore_numbers=False)
112113

113114
Alias for :func:`~pprint.pp` with *sort_dicts* set to ``True`` by default,
@@ -116,11 +117,11 @@ Functions
116117

117118

118119
.. function:: pformat(object, indent=1, width=80, depth=None, *, \
119-
block_style=False, compact=False, sort_dicts=True, \
120+
compact=False, expand=False, sort_dicts=True, \
120121
underscore_numbers=False)
121122

122123
Return the formatted representation of *object* as a string. *indent*,
123-
*width*, *depth*, *compact*, *sort_dicts*, *underscore_numbers* and *block_style* are
124+
*width*, *depth*, *compact*, *expand*, *sort_dicts* and *underscore_numbers* are
124125
passed to the :class:`PrettyPrinter` constructor as formatting parameters
125126
and their meanings are as described in the documentation above.
126127

@@ -164,7 +165,7 @@ PrettyPrinter Objects
164165
.. index:: single: ...; placeholder
165166

166167
.. class:: PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, \
167-
block_style=False, compact=False, sort_dicts=True, \
168+
compact=False, expand=False, sort_dicts=True, \
168169
underscore_numbers=False)
169170

170171
Construct a :class:`PrettyPrinter` instance.
@@ -189,7 +190,7 @@ PrettyPrinter Objects
189190
'knights', 'ni'],
190191
'spam', 'eggs', 'lumberjack', 'knights',
191192
'ni']
192-
>>> pp = pprint.PrettyPrinter(width=41, block_style=True, indent=3)
193+
>>> pp = pprint.PrettyPrinter(width=41, expand=True, indent=3)
193194
>>> pp.pprint(stuff)
194195
[
195196
[
@@ -225,7 +226,7 @@ PrettyPrinter Objects
225226
No longer attempts to write to :data:`!sys.stdout` if it is ``None``.
226227

227228
.. versionchanged:: next
228-
Added the *block_style* parameter.
229+
Added the *expand* parameter.
229230

230231

231232
:class:`PrettyPrinter` instances have the following methods:
@@ -450,10 +451,10 @@ cannot be split, the specified width will be exceeded::
450451
'summary': 'A sample Python project',
451452
'version': '1.2.0'}
452453

453-
Lastly, we can achieve block style formatting with the *block_style* parameter.
454+
Lastly, we can achieve block style formatting with the *expand* parameter.
454455
Best results are achieved with a higher *indent* value::
455456

456-
>>> pprint.pp(project_info, indent=4, block_style=True)
457+
>>> pprint.pp(project_info, indent=4, expand=True)
457458
{
458459
'author': 'The Python Packaging Authority',
459460
'author_email': 'pypa-dev@googlegroups.com',

Doc/whatsnew/3.15.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ platform
411411
pprint
412412
------
413413

414-
* Add a *block_style* keyword argument for :func:`pprint.pprint`,
414+
* Add an *expand* keyword argument for :func:`pprint.pprint`,
415415
:func:`pprint.pformat`, :func:`pprint.pp`. If true, the output will be
416416
formatted in a block style similar to pretty-printed :func:`json.dumps` when
417417
*indent* is supplied.

Lib/pprint.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,23 @@
4444

4545

4646
def pprint(object, stream=None, indent=1, width=80, depth=None, *,
47-
block_style=False, compact=False, sort_dicts=True,
47+
compact=False, expand=False, sort_dicts=True,
4848
underscore_numbers=False):
4949
"""Pretty-print a Python object to a stream [default is sys.stdout]."""
5050
printer = PrettyPrinter(
5151
stream=stream, indent=indent, width=width, depth=depth,
52-
compact=compact, sort_dicts=sort_dicts,
53-
underscore_numbers=underscore_numbers, block_style=block_style)
52+
compact=compact, expand=expand, sort_dicts=sort_dicts,
53+
underscore_numbers=underscore_numbers)
5454
printer.pprint(object)
5555

5656

5757
def pformat(object, indent=1, width=80, depth=None, *,
58-
block_style=False, compact=False, sort_dicts=True,
58+
compact=False, expand=False, sort_dicts=True,
5959
underscore_numbers=False):
6060
"""Format a Python object into a pretty-printed representation."""
6161
return PrettyPrinter(indent=indent, width=width, depth=depth,
62-
compact=compact, sort_dicts=sort_dicts,
63-
underscore_numbers=underscore_numbers,
64-
block_style=block_style).pformat(object)
62+
compact=compact, expand=expand, sort_dicts=sort_dicts,
63+
underscore_numbers=underscore_numbers).pformat(object)
6564

6665

6766
def pp(object, *args, sort_dicts=False, **kwargs):
@@ -114,7 +113,7 @@ def _safe_tuple(t):
114113

115114
class PrettyPrinter:
116115
def __init__(self, indent=1, width=80, depth=None, stream=None, *,
117-
block_style=False, compact=False, sort_dicts=True,
116+
compact=False, expand=False, sort_dicts=True,
118117
underscore_numbers=False):
119118
"""Handle pretty printing operations onto a stream using a set of
120119
configured parameters.
@@ -134,18 +133,19 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
134133
135134
compact
136135
If true, several items will be combined in one line.
136+
Incompatible with expand mode.
137+
138+
expand
139+
If true, the output will be formatted in a block style similar to
140+
pretty-printed json.dumps() when ``indent`` is supplied.
141+
Incompatible with compact mode.
137142
138143
sort_dicts
139144
If true, dict keys are sorted.
140145
141146
underscore_numbers
142147
If true, digit groups are separated with underscores.
143148
144-
block_style
145-
If true, the output will be formatted in a block style similar to
146-
pretty-printed json.dumps() when ``indent`` is supplied.
147-
Incompatible with compact mode.
148-
149149
"""
150150
indent = int(indent)
151151
width = int(width)
@@ -155,8 +155,8 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
155155
raise ValueError('depth must be > 0')
156156
if not width:
157157
raise ValueError('width must be != 0')
158-
if compact and block_style:
159-
raise ValueError('compact and block_style are incompatible')
158+
if compact and expand:
159+
raise ValueError('compact and expand are incompatible')
160160
self._depth = depth
161161
self._indent_per_level = indent
162162
self._width = width
@@ -165,9 +165,9 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
165165
else:
166166
self._stream = _sys.stdout
167167
self._compact = bool(compact)
168+
self._expand = bool(expand)
168169
self._sort_dicts = sort_dicts
169170
self._underscore_numbers = underscore_numbers
170-
self._block_style = bool(block_style)
171171

172172
def pprint(self, object):
173173
if self._stream is not None:
@@ -218,12 +218,12 @@ def _format(self, object, stream, indent, allowance, context, level):
218218
stream.write(rep)
219219

220220
def _format_block_start(self, start_str, indent):
221-
if self._block_style:
221+
if self._expand:
222222
return f"{start_str}\n{' ' * indent}"
223223
return start_str
224224

225225
def _format_block_end(self, end_str, indent):
226-
if self._block_style:
226+
if self._expand:
227227
return f"\n{' ' * indent}{end_str}"
228228
return end_str
229229

@@ -232,7 +232,7 @@ def _pprint_dataclass(self, object, stream, indent, allowance, context, level):
232232
from dataclasses import fields as dataclass_fields
233233

234234
cls_name = object.__class__.__name__
235-
if self._block_style:
235+
if self._expand:
236236
indent += self._indent_per_level
237237
else:
238238
indent += len(cls_name) + 1
@@ -246,9 +246,9 @@ def _pprint_dataclass(self, object, stream, indent, allowance, context, level):
246246
def _pprint_dict(self, object, stream, indent, allowance, context, level):
247247
write = stream.write
248248
write(self._format_block_start('{', indent))
249-
if self._indent_per_level > 1 and not self._block_style:
249+
if self._indent_per_level > 1 and not self._expand:
250250
write((self._indent_per_level - 1) * ' ')
251-
if self._indent_per_level > 0 and self._block_style:
251+
if self._indent_per_level > 0 and self._expand:
252252
write(self._indent_per_level * ' ')
253253
length = len(object)
254254
if length:
@@ -268,7 +268,7 @@ def _pprint_ordered_dict(self, object, stream, indent, allowance, context, level
268268
return
269269
cls = object.__class__
270270
stream.write(cls.__name__ + '(')
271-
if self._block_style:
271+
if self._expand:
272272
recursive_indent = indent
273273
else:
274274
recursive_indent = indent + len(cls.__name__) + 1
@@ -349,7 +349,7 @@ def _pprint_set(self, object, stream, indent, allowance, context, level):
349349
else:
350350
stream.write(self._format_block_start(typ.__name__ + '({', indent))
351351
endchar = '})'
352-
if not self._block_style:
352+
if not self._expand:
353353
indent += len(typ.__name__) + 1
354354
object = sorted(object, key=_safe_key)
355355
self._format_items(object, stream, indent, allowance + len(endchar),
@@ -420,7 +420,7 @@ def _pprint_bytes(self, object, stream, indent, allowance, context, level):
420420
return
421421
parens = level == 1
422422
if parens:
423-
if self._block_style:
423+
if self._expand:
424424
indent += self._indent_per_level
425425
else:
426426
indent += 1
@@ -440,7 +440,7 @@ def _pprint_bytes(self, object, stream, indent, allowance, context, level):
440440
def _pprint_bytearray(self, object, stream, indent, allowance, context, level):
441441
write = stream.write
442442
write(self._format_block_start('bytearray(', indent))
443-
if self._block_style:
443+
if self._expand:
444444
write(' ' * self._indent_per_level)
445445
recursive_indent = indent + self._indent_per_level
446446
else:
@@ -453,7 +453,7 @@ def _pprint_bytearray(self, object, stream, indent, allowance, context, level):
453453

454454
def _pprint_mappingproxy(self, object, stream, indent, allowance, context, level):
455455
stream.write('mappingproxy(')
456-
if self._block_style:
456+
if self._expand:
457457
recursive_indent = indent
458458
else:
459459
recursive_indent = indent + 13
@@ -470,7 +470,7 @@ def _pprint_simplenamespace(self, object, stream, indent, allowance, context, le
470470
cls_name = 'namespace'
471471
else:
472472
cls_name = object.__class__.__name__
473-
if self._block_style:
473+
if self._expand:
474474
indent += self._indent_per_level
475475
else:
476476
indent += len(cls_name) + 1
@@ -493,7 +493,7 @@ def _format_dict_items(self, items, stream, indent, allowance, context,
493493
rep = self._repr(key, context, level)
494494
write(rep)
495495
write(': ')
496-
if self._block_style:
496+
if self._expand:
497497
recursive_indent = indent
498498
else:
499499
recursive_indent = indent + len(rep) + 2
@@ -516,7 +516,7 @@ def _format_namespace_items(self, items, stream, indent, allowance, context, lev
516516
# recursive dataclass repr.
517517
write("...")
518518
else:
519-
if self._block_style:
519+
if self._expand:
520520
recursive_indent = indent
521521
else:
522522
recursive_indent = indent + len(key) + 1
@@ -529,9 +529,9 @@ def _format_namespace_items(self, items, stream, indent, allowance, context, lev
529529
def _format_items(self, items, stream, indent, allowance, context, level):
530530
write = stream.write
531531
indent += self._indent_per_level
532-
if self._indent_per_level > 1 and not self._block_style:
532+
if self._indent_per_level > 1 and not self._expand:
533533
write((self._indent_per_level - 1) * ' ')
534-
if self._indent_per_level > 0 and self._block_style:
534+
if self._indent_per_level > 0 and self._expand:
535535
write(self._indent_per_level * ' ')
536536
delimnl = ',\n' + ' ' * indent
537537
delim = ''
@@ -591,7 +591,7 @@ def _pprint_default_dict(self, object, stream, indent, allowance, context, level
591591
return
592592
rdf = self._repr(object.default_factory, context, level)
593593
cls = object.__class__
594-
if self._block_style:
594+
if self._expand:
595595
stream.write('%s(%s, ' % (cls.__name__, rdf))
596596
else:
597597
indent += len(cls.__name__) + 1
@@ -608,12 +608,12 @@ def _pprint_counter(self, object, stream, indent, allowance, context, level):
608608
return
609609
cls = object.__class__
610610
stream.write(self._format_block_start(cls.__name__ + '({', indent))
611-
if self._indent_per_level > 1 and not self._block_style:
611+
if self._indent_per_level > 1 and not self._expand:
612612
stream.write((self._indent_per_level - 1) * ' ')
613-
if self._indent_per_level > 0 and self._block_style:
613+
if self._indent_per_level > 0 and self._expand:
614614
stream.write(self._indent_per_level * ' ')
615615
items = object.most_common()
616-
if self._block_style:
616+
if self._expand:
617617
recursive_indent = indent
618618
else:
619619
recursive_indent = indent + len(cls.__name__) + 1
@@ -630,7 +630,7 @@ def _pprint_chain_map(self, object, stream, indent, allowance, context, level):
630630
cls = object.__class__
631631
stream.write(self._format_block_start(cls.__name__ + '(',
632632
indent + self._indent_per_level))
633-
if self._block_style:
633+
if self._expand:
634634
indent += self._indent_per_level
635635
else:
636636
indent += len(cls.__name__) + 1
@@ -650,7 +650,7 @@ def _pprint_deque(self, object, stream, indent, allowance, context, level):
650650
return
651651
cls = object.__class__
652652
stream.write(self._format_block_start(cls.__name__ + '([', indent))
653-
if not self._block_style:
653+
if not self._expand:
654654
indent += len(cls.__name__) + 1
655655
if object.maxlen is None:
656656
self._format_items(object, stream, indent, allowance + 2,
@@ -660,7 +660,7 @@ def _pprint_deque(self, object, stream, indent, allowance, context, level):
660660
self._format_items(object, stream, indent, 2,
661661
context, level)
662662
rml = self._repr(object.maxlen, context, level)
663-
if self._block_style:
663+
if self._expand:
664664
stream.write('%s], maxlen=%s)' % ('\n' + ' ' * indent, rml))
665665
else:
666666
stream.write('],\n%smaxlen=%s)' % (' ' * indent, rml))

0 commit comments

Comments
 (0)