This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Teach pprint about dict views
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: AlexWaygood, ajaksu2, david-peled, eric.araujo, joshinsel, rhettinger
Priority: normal Keywords: easy, patch

Created on 2021-12-02 07:06 by rhettinger, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 30135 open ajaksu2, 2021-12-16 09:54
Messages (5)
msg407513 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-12-02 07:06
The pprint() code has a number of type or protocol specific handlers but doesn't have one for dict views.  So, we don't get pretty printing for the key(), values(), and items():

d = {i:i for i in range(100)}
pprint(d)             # This is handled correctly
pprint(d.keys())      # Printed on one line
pprint(d.values())    # Printed on one line
pprint(d.items())     # Printed on one line
msg408582 - (view) Author: Joshua Insel (joshinsel) Date: 2021-12-15 03:07
According to the documentation for pprint, it is supposed to print objects on a single line if possible. See the second paragraph here: https://docs.python.org/3/library/pprint.html
msg408648 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-12-15 21:07
More accurate to say that it aspires to print in a single line ONLY if the content fits in the specified width.  Otherwise, it prints vertically with appropriate indentation.  Indeed, that is the entire purpose of the module; otherwise, we would just use print() which always writes one line.
msg408682 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2021-12-16 10:00
I have tried to add this. The PR adds:
- PrettyPrinter._pprint_dict_view to handle dict_keys and dict_values (sorted with _safe_key).
= PrettyPrinter._pprint_dict_items_view to handle dict_items (sorted using _safe_tuple).
- Tests.

Would a NEWS entry or other form of documentation be necessary?
msg410839 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2022-01-18 01:57
The PR looks good!  When the last comments are resolved, unless there is activity from other core devs, I will merge it.
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90117
2022-01-18 01:57:55eric.araujosetnosy: + eric.araujo
messages: + msg410839
2021-12-16 10:00:31ajaksu2setmessages: + msg408682
2021-12-16 09:54:42ajaksu2setkeywords: + patch
nosy: + ajaksu2

pull_requests: + pull_request28355
stage: patch review
2021-12-15 21:07:12rhettingersetmessages: + msg408648
2021-12-15 03:07:28joshinselsetnosy: + joshinsel
messages: + msg408582
2021-12-13 22:15:11david-peledsetnosy: + david-peled
2021-12-02 22:39:53AlexWaygoodsetnosy: + AlexWaygood
2021-12-02 07:06:56rhettingercreate