Skip to content

Commit f519d34

Browse files
committed
Address feedback so far in the DPO thread
1 parent 92f55f9 commit f519d34

File tree

1 file changed

+71
-9
lines changed

1 file changed

+71
-9
lines changed

peps/pep-0813.rst

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ keyword, and default arguments. The values can be any of the following formats:
7171

7272
* A single value, representing a positional argument. The value itself is used.
7373
* A 2-tuple of ``(name, value)`` representing a keyword argument. A representation of
74-
``name=value`` is used.
74+
``name=value`` is used. If ``name`` is ``None`` or the empty string, then ``value`` is treated as a
75+
positional argument. This is how you would print a positional argument with a tuple value. See
76+
:ref:`examples`.
7577
* A 3-tuple of ``(name, value, default_value)`` representing a keyword argument with a default
7678
value. If ``value`` equals ``default_value``, then this tuple is skipped, otherwise
7779
``name=value`` is used.
@@ -85,8 +87,8 @@ keyword, and default arguments. The values can be any of the following formats:
8587
A new argument to built-in ``print``
8688
------------------------------------
8789

88-
Built-in :func:`print` takes a new optional argument, appended to the end of the argument list, called
89-
``pretty``, which can take one of the following values:
90+
Built-in :func:`print` gains a new optional keyword-only argument called ``pretty``, which can take
91+
one of the following values:
9092

9193
* ``None`` - the default. No pretty printing is invoked. Fully backward compatible.
9294
* ``True`` - use a temporary instance of the :py:class:`python:pprint.PrettyPrinter` class to get a
@@ -106,6 +108,8 @@ will be added. The effect of this specifier with an expression ``value`` will b
106108
:py:func:`python:pprint.pformat`, passing ``value`` as the only argument. In this initial specification, it
107109
will be an error to provide any format specifier if ``!p`` is used.
108110

111+
.. _examples:
112+
109113
Examples
110114
========
111115

@@ -167,6 +171,21 @@ Here's an example of using the ``pretty`` argument to built-in ``print()``:
167171
'PC_SYNC_IO': 25,
168172
'PC_VDISABLE': 9}
169173
174+
Here's an example where a positional argument has a tuple value. In this case, you use the 2-tuple format,
175+
with the ``name`` being ``None`` or the empty string.
176+
177+
.. code-block:: pycon
178+
179+
>>> class Things:
180+
... def __pprint__(self):
181+
... yield (None, (1, 2))
182+
... yield ('', (3, 4))
183+
... yield ('arg', (5, 6))
184+
...
185+
>>> from rich.pretty import pprint
186+
>>> pprint(Things())
187+
Things((1, 2), (3, 4), arg=(5, 6))
188+
170189

171190
Backwards Compatibility
172191
=======================
@@ -205,13 +224,18 @@ None at this time.
205224
Open Issues
206225
===========
207226

227+
Rich compatibility
228+
------------------
229+
208230
The output format and APIs are heavily inspired by `Rich
209-
<rich-repr-protocol_>`_. The idea is that Rich could
210-
implement an API compatible with ``print(..., pretty=RichPrinter)`` fairly easily. Rich's API is designed to
211-
print constructor-like representations of instances, which means that it's not possible to control much of the
212-
"class chrome" around the arguments. Rich does support using angle brackets (i.e. ``<...>``) instead of
213-
parentheses by setting the attribute ``.angular=True`` on the rich repr method. This PEP does not support
214-
that feature, although it likely could in the future.
231+
<rich-repr-protocol_>`_. The idea is that Rich could implement an API
232+
compatible with ``print(..., pretty=RichPrinter)`` fairly easily. Rich's API
233+
is designed to print constructor-like representations of instances, which
234+
means that it's not possible to control much of the "class chrome" around the
235+
arguments. Rich does support using angle brackets (i.e. ``<...>``) instead of
236+
parentheses by setting the attribute ``.angular=True`` on the rich repr
237+
method. This PEP does not support that feature, although it likely could in
238+
the future.
215239

216240
This also means that there's no way to control the pretty printed format of built-in types like strings,
217241
dicts, lists, etc. This seems fine as ``pprint`` is not intended to be as feature-rich (pun intended!) as
@@ -232,6 +256,32 @@ multiple objects with, say a newline between the object representations. Compar
232256
It's likely you'll want the second output, but more complicated multi-object displays could get even less
233257
convenient and/or more verbose.
234258

259+
print's pretty argument could just take a callable
260+
--------------------------------------------------
261+
262+
There was [a suggestion](https://discuss.python.org/t/pep-813-the-pretty-print-protocol/106242/2) in the DPO
263+
thread that the third form of ``print(..., pretty=not_none_or_true)`` could generalize to any
264+
callable taking a single argument, rather than requiring it to be an instance with a
265+
``pformat()``-compatible method. We don't need both, but the suggested generalization could be useful. I.e.
266+
267+
.. code-block:: python
268+
269+
pprint(myobj, pretty=MyPrinter())
270+
271+
vs.
272+
273+
.. code-block:: python
274+
275+
pprint(myobj, pretty=MyPrinter().pformat)
276+
277+
278+
Deferred Ideas
279+
==============
280+
281+
Possibly add support for ``!p`` conversions to t-strings.
282+
283+
``!p`` conversions could include format specifiers.
284+
235285

236286
Acknowledgments
237287
===============
@@ -245,6 +295,18 @@ Footnotes
245295
TBD
246296

247297

298+
Change History
299+
==============
300+
301+
* `TBD <TBD>`__
302+
303+
* Specify that to pretty print tuples as positional arguments, use the 2-tuple value format, passing
304+
``None`` or the empty string as the argument name.
305+
* Specify that the ``!p`` conversion in f-strings and ``str.format()`` implicitly perform an
306+
import of the ``pprint`` module.
307+
* Clarify the language around the new optional keyword-only argument ``pretty`` to ``print()``.
308+
309+
248310
Copyright
249311
=========
250312

0 commit comments

Comments
 (0)