Skip to content

Commit 5b6ce45

Browse files
committed
__pretty__() is now exactly signatured as PrettyPrinter.format()
1 parent 955459e commit 5b6ce45

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

Lib/pprint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ def _safe_repr(self, object, context, maxlevels, level):
616616
return repr(object), True, False
617617

618618
if (p := getattr(typ, "__pprint__", None)):
619-
return p(object, context, maxlevels, level), True, False
619+
return p(object, context, maxlevels, level)
620620

621621
r = getattr(typ, "__repr__", None)
622622

Lib/test/test_pprint.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ def __repr__(self):
143143
return "my str"
144144

145145
def __pprint__(self, context, maxlevels, level):
146-
return "my pprint"
146+
# The custom pretty repr, not-readable bool, no recursion detected.
147+
return "my pprint", False, False
147148

148149

149150
class QueryTestCase(unittest.TestCase):

Lib/test/test_print.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def test_string_in_loop_on_same_line(self):
203203

204204
class PPrintable:
205205
def __pprint__(self, context, maxlevels, level):
206-
return 'I feel pretty'
206+
return 'I feel pretty', False, False
207207

208208

209209
class PrettySmart(PrettyPrinter):

pep-9999.rst

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,26 @@ There are two parts to this proposal.
5454

5555
Classes can implement a new dunder method, ``__pretty__()`` which if present, generates the pretty printed
5656
representation of their instances. This augments ``__repr__()`` which, prior to this proposal, was the only
57-
method used to generate a pretty representation of the object. Since the built-in :py:func:`repr` function
58-
provides functionality potentially separate from pretty printing, some classes may want more control over
59-
object display between those two use cases.
57+
method used to generate a pretty representation of the object. Since object reprs provide functionality
58+
distinct from pretty printing, some classes may want more control over their pretty display.
6059

6160
``__pretty__()`` is optional; if missing, the standard pretty printers fall back to ``__repr__()`` for full
62-
backward compatibility. However, if defined on a class, ``__pretty__()`` has the same argument signature as
63-
:py:func:`PrettyPrinter.format`, taking four arguments:
61+
backward compatibility (technically speaking, :meth:`pprint.saferepr` is used). However, if defined on a
62+
class, ``__pretty__()`` has the same argument signature as :py:func:`PrettyPrinter.format`, taking four
63+
arguments:
6464

6565
* ``object`` - the object to print, which is effectively always ``self``
6666
* ``context`` - a dictionary mapping the ``id()`` of objects which are part of the current presentation
6767
context
6868
* ``maxlevels`` - the requested limit to recursion
6969
* ``levels`` - the current recursion level
7070

71-
See :py:func:`PrettyPrinter.format` for details.
71+
Similarly, ``__pretty__()`` returns three values, the string to be used as the pretty printed representation,
72+
a boolean indicating whether the returned value is "readable", and a boolean indicating whether recursion has
73+
been detected. In this context, "readable" means the same as :meth:`PrettyPrinter.isreadable`, i.e. that the
74+
returned value can be used to reconstruct the original object using ``eval()``.
7275

73-
Unlike that function, ``__pretty__()`` returns a single value, the string to be used as the pretty printed
74-
representation.
76+
See :py:func:`PrettyPrinter.format` for details.
7577

7678

7779
A new argument to built-in ``print``
@@ -172,13 +174,7 @@ None at this time.
172174
Open Issues
173175
===========
174176

175-
As currently defined, the ``__pretty__()`` method is defined as taking four arguments and returning
176-
a single string. This is close to -- but not quite -- the full signature for
177-
``PrettyPrinter.format()``, and was chosen for reference implementation convenience. It's not
178-
clear that custom pretty representations need ``context``, ``maxlevels``, and ``levels``, so perhaps
179-
the argument list should be simplified? If not, then perhaps ``__pretty__()`` should *exactly*
180-
match ``PrettyPrinter.format()``? That does, however complicate the protocol for users.
181-
177+
TBD
182178

183179
Acknowledgements
184180
================

0 commit comments

Comments
 (0)