-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
PEP 813: The Pretty Print Protocol #4690
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
base: main
Are you sure you want to change the base?
Conversation
hugovk
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks useful!
Co-authored-by: Hugo van Kemenade <[email protected]>
AA-Turner
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the slight delay in initial review!
Mostly editorial comments in line, a couple of questions where I didn't follow the text too.
A slightly more substantive point I think it'd be useful to address is what style/form of prettiness will be chosen -- I'm thinking here of the proposed(?) change to pprint to add block-style formatting, similar to how code is often formatted in source form, rather than the more traditional pprint approach (as illustrated in the examples here). Personally, I'd always want to use the block-style if possible, but my understanding of the current proposal is that options like that wouldn't be possible.
A
| This PEP describes the "pretty print protocol", a collection of changes proposed to make pretty printing more | ||
| customizable and convenient. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be useful to note that implementation is via the __pretty__ dunder (& the change to print?) here, even if just in a single sentence.
| ========== | ||
|
|
||
| "Pretty printing" is a feature which provides a capability to format object representations for better | ||
| readability. The core functionality is implemented by the standard library :mod:`pprint`. ``pprint`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| readability. The core functionality is implemented by the standard library :mod:`pprint`. ``pprint`` | |
| readability. The core functionality is implemented by the standard library :mod:`pprint` module. ``pprint`` |
|
|
||
| "Pretty printing" is a feature which provides a capability to format object representations for better | ||
| readability. The core functionality is implemented by the standard library :mod:`pprint`. ``pprint`` | ||
| includes a class and APIs which users can invoke to format and print more readable representations of objects. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is implicitly versus repr(), perhaps useful to say so explicitly?
| "Pretty printing" is a feature which provides a capability to format object representations for better | ||
| readability. The core functionality is implemented by the standard library :mod:`pprint`. ``pprint`` | ||
| includes a class and APIs which users can invoke to format and print more readable representations of objects. | ||
| Important use cases include pretty printing large dictionaries and other complicated objects. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say a key use case is interactive debugging, pretty-printing by default is a big reason I use IPython.
| Specification | ||
| ============= | ||
|
|
||
| There are two parts to this proposal. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| There are two parts to this proposal. | |
| There are two parts to this proposal; | |
| a new dunder method and a change to :func:`print`. |
| * ``maxlevels`` - the requested limit to recursion | ||
| * ``levels`` - the current recursion level | ||
|
|
||
| Similarly, ``__pretty__()`` returns three values, the string to be used as the pretty printed representation, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Similarly, ``__pretty__()`` returns three values, the string to be used as the pretty printed representation, | |
| Similarly, ``__pretty__()`` returns three values: the string to be used as the pretty printed representation, |
| Examples | ||
| ======== | ||
|
|
||
| A custom ``__pprint__()`` method can be used to customize the representation of the object: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| A custom ``__pprint__()`` method can be used to customize the representation of the object: | |
| A custom ``__pretty__()`` method can be used to customize the representation of the object: |
| ``__pretty__()`` is optional; if missing, the standard pretty printers fall back to ``__repr__()`` | ||
| for full backward compatibility (technically speaking, :py:func:`python:pprint.saferepr` is used). | ||
| However, if defined on a class, ``__pretty__()`` has the same argument signature as | ||
| :py:meth:`python:pprint.PrettyPrinter.format`, taking four arguments: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this four in addition to self? (i.e. def __pretty__(self, object, context, maxlevels, levels): ...)
| >>> class Custom: | ||
| ... def __str__(self): return 'my str' | ||
| ... def __repr__(self): return 'my repr' | ||
| ... def __pprint__(self, context, maxlevels, level): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ... def __pprint__(self, context, maxlevels, level): | |
| ... def __pretty__(self, context, maxlevels, level): |
| >>> pprint.pp(Custom()) | ||
| my pprint |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the intent for PrettyPrinter to change to respect __pretty__ methods? I don't think I saw that above in the spec, but I may have missed it, sorry!
Basic requirements (all PEP Types)
pep-NNNN.rst), PR title (PEP 123: <Title of PEP>) andPEPheaderAuthororSponsor, and formally confirmed their approvalAuthor,Status(Draft),TypeandCreatedheaders filled out correctlyPEP-Delegate,Topic,RequiresandReplacesheaders completed if appropriate.github/CODEOWNERSfor the PEPStandards Track requirements
Python-Versionset to valid (pre-beta) future Python version, if relevantDiscussions-ToandPost-History📚 Documentation preview 📚: https://pep-previews--4690.org.readthedocs.build/pep-0813/