-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
Comments
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 |
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. |
I have tried to add this. The PR adds:
Would a NEWS entry or other form of documentation be necessary? |
The PR looks good! When the last comments are resolved, unless there is activity from other core devs, I will merge it. |
I believe the PR is ready for review. |
(we're getting the PR in at the pycon US sprint today) |
* 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>
Thanks for the contribution! Finally merged. 😁 |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: