Skip to content

Teach pprint about dict views #90117

Closed
Closed

Description

@rhettinger
BPO 45959
Nosy @rhettinger, @devdanzin, @merwok, @AlexWaygood, @david-peled, @joshinsel
PRs
  • gh-90117: handle dict and mapping views in pprint #30135
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2021-12-02.07:06:56.201>
    labels = ['easy', 'type-feature', 'library', '3.11']
    title = 'Teach pprint about dict views'
    updated_at = <Date 2022-01-18.01:57:55.609>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2022-01-18.01:57:55.609>
    actor = 'eric.araujo'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2021-12-02.07:06:56.201>
    creator = 'rhettinger'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45959
    keywords = ['patch', 'easy']
    message_count = 5.0
    messages = ['407513', '408582', '408648', '408682', '410839']
    nosy_count = 6.0
    nosy_names = ['rhettinger', 'ajaksu2', 'eric.araujo', 'AlexWaygood', 'david-peled', 'joshinsel']
    pr_nums = ['30135']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue45959'
    versions = ['Python 3.11']

    Linked PRs

    Activity

    1. rhettinger commented on Dec 2, 2021

      @rhettinger
      ContributorAuthor

      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
    2. added
      3.11only security fixes
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Dec 2, 2021
    3. joshinsel commented on Dec 15, 2021

      joshinselmannequin
      Mannequin

      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

    4. rhettinger commented on Dec 15, 2021

      @rhettinger
      ContributorAuthor

      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.

    5. devdanzin commented on Dec 16, 2021

      devdanzinmannequin
      Mannequin

      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?

    6. merwok commented on Jan 18, 2022

      @merwok
      Member

      The PR looks good! When the last comments are resolved, unless there is activity from other core devs, I will merge it.

    7. transferred this issue fromon Apr 10, 2022
    8. AbhigyanBose commented on Apr 24, 2022

      @AbhigyanBose
      Contributor

      @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.

    9. devdanzin commented on May 30, 2024

      @devdanzin
      Member

      Is there anything that needs to be done in the PR ?

      I believe the PR is ready for review.

    10. gpshead commented on May 20, 2025

      @gpshead
      Member

      (we're getting the PR in at the pycon US sprint today)

    11. added a commit that references this issue on May 20, 2025
    12. gpshead commented on May 20, 2025

      @gpshead
      Member

      Thanks for the contribution! Finally merged. 😁

    13. added a commit that references this issue on May 26, 2025
    14. added a commit that references this issue on Jun 24, 2025
    15. added a commit that references this issue on Jul 11, 2025
    16. added 2 commits that reference this issue on Jul 12, 2025
    17. added 2 commits that reference this issue on Aug 4, 2025
    18. added a commit that references this issue on Aug 19, 2025
    19. added a commit that references this issue on Jun 2, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.11only security fixeseasystdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions