-
-
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 <1324225+hugovk@users.noreply.github.com>
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
willingc
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.
Nice. A few clarifications in addition to Adam's.
| 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. | ||
|
|
||
| The ``pprint`` module is great as far as it goes. This PEP builds on the features of this module to provide |
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.
| The ``pprint`` module is great as far as it goes. This PEP builds on the features of this module to provide | |
| The ``pprint`` module provides fundamentals for user-readable information display. This PEP builds on the features of this module to provide |
| Important use cases include pretty printing large dictionaries and other complicated objects. | ||
|
|
||
| The ``pprint`` module is great as far as it goes. This PEP builds on the features of this module to provide | ||
| more customization and convenience. |
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.
| more customization and convenience. | |
| more customization, display structure, and user convenience. |
| Pretty printing is very useful for displaying complex data structures, like dictionaries read from JSON | ||
| content. By providing a way for classes to customize how their instances participate in pretty printing, | ||
| users have more options for visually improving the display and debugging of their complex data. |
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.
| Pretty printing is very useful for displaying complex data structures, like dictionaries read from JSON | |
| content. By providing a way for classes to customize how their instances participate in pretty printing, | |
| users have more options for visually improving the display and debugging of their complex data. | |
| Pretty printing is very useful for displaying complex data structures, like dictionaries read from JSON | |
| content. Through class customization options for pretty printing, | |
| users have more options for visually improving the display and debugging of their complex data. |
| By extending the built-in :func:`print` function to automatically pretty print its output, this feature is | ||
| made even more convenient, since no extra imports are required, and users can easily just piggyback on | ||
| well-worn "print debugging" patterns, at least for the most common use cases. |
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.
| By extending the built-in :func:`print` function to automatically pretty print its output, this feature is | |
| made even more convenient, since no extra imports are required, and users can easily just piggyback on | |
| well-worn "print debugging" patterns, at least for the most common use cases. | |
| By extending the built-in :func:`print` function to automatically pretty print its output, user-readable display is | |
| made even more convenient. Since no extra imports are required, users can easily piggyback on | |
| well-worn "print debugging" patterns for most common use cases. |
| made even more convenient, since no extra imports are required, and users can easily just piggyback on | ||
| well-worn "print debugging" patterns, at least for the most common use cases. | ||
|
|
||
| These two extensions work independently, but hand-in-hand can provide a powerful and convenient new feature. |
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.
| These two extensions work independently, but hand-in-hand can provide a powerful and convenient new feature. | |
| These two extensions, class customization for pretty printing and default print behavior, work independently but can be used in conjunction to provide a powerful and convenient user display of information. |
peps/pep-0813.rst
Outdated
| Classes can implement a new dunder method, ``__pretty__()`` which if present, generates the pretty printed | ||
| representation of their instances. This augments ``__repr__()`` which, prior to this proposal, was the only | ||
| method used to generate a pretty representation of the object. Since object reprs provide functionality | ||
| distinct from pretty printing, some classes may want more control over their pretty display. |
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.
| distinct from pretty printing, some classes may want more control over their pretty display. | |
| structured representation distinct from pretty printing, some classes may benefit from more control over pretty visual display of their instances. |
No worries! I was on vacation so I'm just getting back to this now.
Very much appreciated!
While I prefer to defer substantive discussions to the inevitable mega-DPO thread, I think a possible way to handle this would be to use a different I'll have to discuss with @ericvsmith as we move forward with the proposal. |
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
|
Hi foks, Can I be bold (and possibly also italic and underline) and suggest that this should become something like Rich's rich repr protocol. If not adopt it entirely. In its current form, it looks like most of the work in formatting something as "pretty" is left to the object implementing Because Rich's The rich repr approach does place some limitations on the output. The author couldn't generate absolutely anything in pretty output, but I consider this to be a plus. The pretty output will always look like formatted code in your editor, as you couldn't generate anything too crazy with this approach. Other features like color and indent guides are optional, but would remain possible. Example Rich output for context:
Will (Rich guy) |
That's really interesting and powerful @willmcgugan, thanks for the reference. @ericvsmith and I have been thinking about how to improve the proposal to make things more flexible, and this is a really good real-world example of what it might look like. Have you had any problems or suggestions for improving rich's pretty printing? I wonder about adopting it directly. I'm not sure how well that will play with the stdlib Even if it isn't feasible to adopt directly, I'd want to think about ways to make them interoperate seamlessly. |
|
The implementation is a little gnarly. Much of the complexity came from introspection. I'd also like to echo @AA-Turner 's comments about the formatting. I don't consider pprint's output to be pretty any more. It was pre-Python3 and pre-black. I think most people will have an expectation that "pretty" is how your editor would auto-format a data-structure. Feel free to mention me any time. I'm interested in following along on this one. |

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/