You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:iforiinrange(100)}
pprint(d) # This is handled correctlypprint(d.keys()) # Printed on one linepprint(d.values()) # Printed on one linepprint(d.items()) # Printed on one line
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
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.
devdanzin commented on Dec 16, 2021
devdanzinmannequin
Mannequin
More actions
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?
Activity
rhettinger commentedon Dec 2, 2021
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():
joshinsel commentedon Dec 15, 2021
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
rhettinger commentedon Dec 15, 2021
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.
devdanzin commentedon Dec 16, 2021
I have tried to add this. The PR adds:
= PrettyPrinter._pprint_dict_items_view to handle dict_items (sorted using _safe_tuple).
Would a NEWS entry or other form of documentation be necessary?
merwok commentedon Jan 18, 2022
The PR looks good! When the last comments are resolved, unless there is activity from other core devs, I will merge it.
AbhigyanBose commentedon Apr 24, 2022
@merwok Hey, what's the situation with the PR for this issue ?
Is there anything that needs to be done in the PR ?
If there is, I'm happy to help complete it, or put in a different PR, whichever is required.
devdanzin commentedon May 30, 2024
I believe the PR is ready for review.
gpshead commentedon May 20, 2025
(we're getting the PR in at the pycon US sprint today)
gh-90117: handle dict and mapping views in pprint (#30135)
gpshead commentedon May 20, 2025
Thanks for the contribution! Finally merged. 😁
pythongh-90117: handle dict and mapping views in pprint (python#30135)
GH-90117: Check for list and tuple before MappingView in pprint (GH-1…
pythonGH-90117: Check for list and tuple before MappingView in pprint (…
pythongh-90117: handle dict and mapping views in pprint (python#30135)
pythonGH-90117: Check for list and tuple before MappingView in pprint (…
pythongh-90117: handle dict and mapping views in pprint (python#30135)
pythonGH-90117: Check for list and tuple before MappingView in pprint (…
pythonGH-90117: Check for list and tuple before MappingView in pprint (…
pythongh-90117: handle dict and mapping views in pprint (python#30135)