8000 Teach pprint about dict views · Issue #90117 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Teach pprint about dict views #90117

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

Closed
rhettinger opened this issue Dec 2, 2021 · 9 comments
Closed

Teach pprint about dict views #90117

rhettinger opened this issue Dec 2, 2021 · 9 comments
Labels
3.11 only security fixes easy stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@rhettinger
Copy link
Contributor
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']

    @rhettinger
    Copy link
    Contributor Author

    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

    @rhettinger rhettinger added 3.11 only security fixes stdlib Python modules in the Lib dir easy type-feature A feature request or enhancement labels Dec 2, 2021
    @joshinsel
    Copy link
    Mannequin
    joshinsel mannequin commented 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
    Copy link
    Contributor Author

    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
    Copy link
    Mannequin
    devdanzin mannequin commented Dec 16, 2021

    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?

    @merwok
    Copy link
    Member
    merwok commented 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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @AbhigyanBose
    Copy link
    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.

    @devdanzin
    Copy link
    Contributor

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

    I believe the PR is ready for review.

    @gpshead
    Copy link
    Member
    gpshead commented May 20, 2025

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

    gpshead added a commit that referenced this issue May 20, 2025
    * Teach pprint about dict views with PrettyPrinter._pprint_dict_view and ._pprint_dict_items_view.
    * Use _private names for _dict_*_view attributes of PrettyPrinter.
    * Use explicit 'items' keyword when calling _pprint_dict_view from _pprint_dict_items_view.
    * 📜🤖 Added by blurb_it.
    * Improve tests
    * Add tests for collections.abc.[Keys|Items|Mapping|Values]View support in pprint.
    * Add support for collections.abc.[Keys|Items|Mapping|Values]View in pprint.
    * Split _pprint_dict_view into _pprint_abc_view, so pretty-printing normal dict views and ABC views is handled in two simple methods.
    * Simplify redundant code.
    * Add collections.abc views to some existing pprint tests.
    * Test that views from collection.UserDict are correctly formatted by pprint.
    * Handle recursive dict and ABC views.
    * Test that subclasses of ABC views work in pprint.
    * Test dict views coming from collections.Counter.
    * Test ABC views coming from collections.ChainMap.
    * Test odict views coming from collections.OrderedDict.
    * Rename _pprint_abc_view to _pprint_mapping_abc_view.
    * Add pprint test for mapping ABC views where ._mapping has a custom __repr__ and fix ChainMap test.
    * When a mapping ABC view has a ._mapping that defines a custom __repr__, dispatch pretty-printing it by that __repr__.
    * Add tests for ABC mapping views subclasses that don't replace __repr__, also handling those that delete ._mapping on instances.
    * Simplify the pretty printing of ABC mapping views.
    * Add a test for depth handling when pretty printing dict views.
    * Fix checking whether the view type is a subclass of an items view, add a test.
    * Move construction of the views __repr__ set out of _safe_repr.
    
    ---------
    
    Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
    Co-authored-by: Éric <merwok@netwok.org>
    Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
    Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
    Co-authored-by: Gregory P. Smith <greg@krypto.org>
    @gpshead
    Copy link
    Member
    gpshead commented May 20, 2025

    Thanks for the contribution! Finally merged. 😁

    @gpshead gpshead closed this as completed May 20, 2025
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes easy stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants
    0