Skip to content

gh-112632 : Added an option for block formatting to pprint #129274

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

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
b9baf94
Added an option for block formatting to pprint
stodoran Jan 25, 2025
f5fcbfc
Small bug fix for indent=1, added to unit test
StefanTodoran Feb 6, 2025
31a4d1e
Merge branch 'main' into fix-issue-112632
StefanTodoran Feb 6, 2025
a92ef3d
Remove set from block style unit test (since order kept changing)
StefanTodoran Feb 7, 2025
d9a147f
📜🤖 Added by blurb_it.
blurb-it[bot] Feb 7, 2025
ffef3b0
Merge branch 'main' into fix-issue-112632
StefanTodoran Feb 7, 2025
19e4e08
Fix params in news entry
StefanTodoran Feb 7, 2025
8ef085f
Fix line length for news entry
StefanTodoran Feb 7, 2025
c446aa1
Shorten news entry
StefanTodoran Feb 7, 2025
f9bd6ef
Merge branch 'main' into fix-issue-112632
StefanTodoran Feb 9, 2025
49b1108
Address documentation related PR comments
StefanTodoran Feb 19, 2025
62dd5e5
Merge branch 'main' into fix-issue-112632
StefanTodoran Feb 19, 2025
0b8fd2b
Fix length of title underline :/
StefanTodoran Feb 19, 2025
5f08960
Fix test case, remove unnecessary recursion on dataclass (we aren't t…
StefanTodoran Feb 19, 2025
ac71fc4
Merge branch 'main' into fix-issue-112632
StefanTodoran Feb 25, 2025
59ed5fd
Update Doc/library/pprint.rst
StefanTodoran Feb 25, 2025
cbc392d
Update Doc/library/pprint.rst
StefanTodoran Feb 25, 2025
d1af62c
Update Doc/library/pprint.rst
StefanTodoran Feb 25, 2025
283ed38
Update Doc/library/pprint.rst
StefanTodoran Feb 25, 2025
142b92b
Update Doc/library/pprint.rst
StefanTodoran Feb 25, 2025
2273be2
Update Doc/whatsnew/3.14.rst
StefanTodoran Feb 25, 2025
d8b5942
Update Lib/pprint.py
StefanTodoran Feb 25, 2025
8c3ed5c
Update Lib/pprint.py
StefanTodoran Feb 25, 2025
9c7afd5
Update Lib/pprint.py
StefanTodoran Feb 25, 2025
4e7fbff
Update Lib/pprint.py
StefanTodoran Feb 25, 2025
5abab85
Update Lib/pprint.py
StefanTodoran Feb 25, 2025
5e8a624
Update Lib/pprint.py
StefanTodoran Feb 25, 2025
5ffbd0d
Update Lib/test/test_pprint.py
StefanTodoran Feb 25, 2025
fa4cdac
Update Lib/test/test_pprint.py
StefanTodoran Feb 25, 2025
6bffc7e
Update Lib/test/test_pprint.py
StefanTodoran Feb 25, 2025
c8ce4ff
Update Lib/test/test_pprint.py
StefanTodoran Feb 25, 2025
879f1da
Update Lib/test/test_pprint.py
StefanTodoran Feb 25, 2025
21c9fa6
Update Lib/test/test_pprint.py
StefanTodoran Feb 25, 2025
815e1da
Update Lib/test/test_pprint.py
StefanTodoran Feb 25, 2025
9d12a8a
Merge branch 'main' into fix-issue-112632
StefanTodoran Feb 25, 2025
a7c26dd
Apply suggestions from code review
StefanTodoran Feb 25, 2025
37312d9
Split large test case into multiple smaller ones, update news entry
StefanTodoran Mar 28, 2025
e3b09fa
Merge branch 'main' into fix-issue-112632
StefanTodoran Mar 28, 2025
712b644
Merge changes
StefanTodoran Mar 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 103 additions & 5 deletions Doc/library/pprint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ Functions
---------

.. function:: pp(object, stream=None, indent=1, width=80, depth=None, *, \
compact=False, sort_dicts=False, underscore_numbers=False)
compact=False, sort_dicts=False, underscore_numbers=False, \
block_style=False)
Comment on lines +39 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are keyword-only (after *), so we have the luxury of alphabetical sorting:

Suggested change
compact=False, sort_dicts=False, underscore_numbers=False, \
block_style=False)
block_style=False, compact=False, sort_dicts=False, \
underscore_numbers=False)


Prints the formatted representation of *object*, followed by a newline.
This function may be used in the interactive interpreter
Expand Down Expand Up @@ -85,6 +86,12 @@ Functions
integers will be formatted with the ``_`` character for a thousands separator,
otherwise underscores are not displayed (the default).

:param bool block_style:
If ``True``,
opening parentheses and brackets will be followed by a newline and the
following content will be indented by one level, similar to block style
JSON formatting. This option is not compatible with *compact*.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This option is not compatible with compact.

A passing thought: Perhaps introduce a mode (str) argument, and deprecate compact in favour of it? It's not great to have two conflicting Booleans, though it may be the best option for now.


>>> import pprint
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> stuff.insert(0, stuff)
Expand All @@ -100,18 +107,20 @@ Functions


.. function:: pprint(object, stream=None, indent=1, width=80, depth=None, *, \
compact=False, sort_dicts=True, underscore_numbers=False)
compact=False, sort_dicts=True, \
underscore_numbers=False, block_style=False)
Comment on lines +110 to +111
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise

Suggested change
compact=False, sort_dicts=True, \
underscore_numbers=False, block_style=False)
block_style=False, compact=False, \
sort_dicts=True, underscore_numbers=False)


Alias for :func:`~pprint.pp` with *sort_dicts* set to ``True`` by default,
which would automatically sort the dictionaries' keys,
you might want to use :func:`~pprint.pp` instead where it is ``False`` by default.


.. function:: pformat(object, indent=1, width=80, depth=None, *, \
compact=False, sort_dicts=True, underscore_numbers=False)
compact=False, sort_dicts=True, \
underscore_numbers=False, block_style=False)
Comment on lines +119 to +120
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise

Suggested change
compact=False, sort_dicts=True, \
underscore_numbers=False, block_style=False)
block_style=False, compact=False, \
sort_dicts=True, underscore_numbers=False)


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

Expand Down Expand Up @@ -155,7 +164,8 @@ PrettyPrinter Objects
.. index:: single: ...; placeholder

.. class:: PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, \
compact=False, sort_dicts=True, underscore_numbers=False)
compact=False, sort_dicts=True, \
underscore_numbers=False, block_style=False)
Comment on lines +167 to +168
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise

Suggested change
compact=False, sort_dicts=True, \
underscore_numbers=False, block_style=False)
block_style=False, compact=False, \
sort_dicts=True, underscore_numbers=False)


Construct a :class:`PrettyPrinter` instance.

Expand All @@ -179,6 +189,22 @@ PrettyPrinter Objects
'knights', 'ni'],
'spam', 'eggs', 'lumberjack', 'knights',
'ni']
>>> pp = pprint.PrettyPrinter(width=41, block_style=True, indent=3)
>>> pp.pprint(stuff)
[
[
'spam',
'eggs',
'lumberjack',
'knights',
'ni'
],
'spam',
'eggs',
'lumberjack',
'knights',
'ni'
]
>>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
... ('parrot', ('fresh fruit',))))))))
>>> pp = pprint.PrettyPrinter(depth=6)
Expand All @@ -198,6 +224,9 @@ PrettyPrinter Objects
.. versionchanged:: 3.11
No longer attempts to write to :data:`!sys.stdout` if it is ``None``.

.. versionchanged:: next
Added the *block_style* parameter.


:class:`PrettyPrinter` instances have the following methods:

Expand Down Expand Up @@ -420,3 +449,72 @@ cannot be split, the specified width will be exceeded::
'requires_python': None,
'summary': 'A sample Python project',
'version': '1.2.0'}

Lastly, we can achieve block style formatting with the *block_style* parameter.
Best results are achieved with a higher *indent* value::

>>> pprint.pp(project_info, indent=4, block_style=True)
{
'author': 'The Python Packaging Authority',
'author_email': 'pypa-dev@googlegroups.com',
'bugtrack_url': None,
'classifiers': [
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Topic :: Software Development :: Build Tools'
],
'description': 'A sample Python project\n'
'=======================\n'
'\n'
'This is the description file for the project.\n'
'\n'
'The file should use UTF-8 encoding and be written using ReStructured '
'Text. It\n'
'will be used to generate the project webpage on PyPI, and should be '
'written for\n'
'that purpose.\n'
'\n'
'Typical contents for this file would include an overview of the project, '
'basic\n'
'usage examples, etc. Generally, including the project changelog in here '
'is not\n'
'a good idea, although a simple "What\'s New" section for the most recent '
'version\n'
'may be appropriate.',
'description_content_type': None,
'docs_url': None,
'download_url': 'UNKNOWN',
'downloads': {'last_day': -1, 'last_month': -1, 'last_week': -1},
'dynamic': None,
'home_page': 'https://github.com/pypa/sampleproject',
'keywords': 'sample setuptools development',
'license': 'MIT',
'license_expression': None,
'license_files': None,
'maintainer': None,
'maintainer_email': None,
'name': 'sampleproject',
'package_url': 'https://pypi.org/project/sampleproject/',
'platform': 'UNKNOWN',
'project_url': 'https://pypi.org/project/sampleproject/',
'project_urls': {
'Download': 'UNKNOWN',
'Homepage': 'https://github.com/pypa/sampleproject'
},
'provides_extra': None,
'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
'requires_dist': None,
'requires_python': None,
'summary': 'A sample Python project',
'version': '1.2.0',
'yanked': False,
'yanked_reason': None
}
12 changes: 11 additions & 1 deletion Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,16 @@ platform
(Contributed by Bénédikt Tran in :gh:`122549`.)


pprint
------

* Add a *block_style* keyword argument for :func:`pprint.pprint`,
:func:`pprint.pformat`, :func:`pprint.pp`. If true, the output will be
formatted in a block style similar to pretty-printed :func:`json.dumps` when
*indent* is supplied.
(Contributed by Stefan Todoran in :gh:`129274`.)


pydoc
-----

Expand Down Expand Up @@ -1200,7 +1210,7 @@ Deprecated
.. include:: ../deprecations/pending-removal-in-future.rst

Removed
=======
========
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
========
=======


argparse
--------
Expand Down
Loading
Loading