-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
pprint for dict in sorted order or insert order? #74855
Comments
start from 3.6+, dict keys are ordered heir creation order. |
Until dict insertion order is guaranteed, it doesn't make sense to change pprint(). |
Concur with Raymond. |
If/when order is guaranteed (3.7?) we should have a pprint that respects current order, -or- we should get an improved pprint (maybe named pp or print?) that understands mappings and other abstract data types. I had a conversation about pprint at the Python meetup last night. It kinda went like this: https://www.youtube.com/watch?v=NpYEJx7PkWE Maybe now's the time for improved behavior? |
A year ago I proposed to add a method for detecting whether a dict preserves an order and in what sense. [1] But this idea was rejected. [1] https://mail.python.org/pipermail/python-dev/2016-October/146597.html |
IMHO since Guido said that dictionary order is guaranteed going forward [1], the order should now be preserved in 3.6+ Getting a sorted pprint'ed dict in a Py3.6.1 was a surprise to me coming from Python2. The default ordered dict is to me the killer feature of 3 and a major driver to migrate from 2. [1] https://mail.python.org/pipermail/python-dev/2017-December/151286.html |
@terry.reedy - If I understand correctly, this issue was closed by you awaiting what would happen with the dict insertion order after 3.6. It has been decided that dict insertion will stay for now and in the future. >>> Should this issue now be reopened? And I would favor backward compatibility (key sort order) with an additional keyword for pprint to display it "unsorted" order (from pprint perspective, insertion order for dicts and OrderedDicts, and whatever other order for whatever other dicts). |
Please reopen this. |
I reopened this issue, and change affected versions to 3.6, 3.7 and 3.8. |
Hi josephsmeng, dict order preservation is now used in a lot of place in Python. I will post a patch to make pprint use insertion order in a few hours. |
https://docs.python.org/3/library/pprint.html
https://mail.python.org/pipermail/python-dev/2017-December/151369.html . There was some discussion in the thread about this being a breaking change and the behavior of pprint. @remi.lapeyre I would suggest waiting for a core dev on approving this change before proceeding with a patch to avoid work being wasted. |
If you stop sorting keys in pprint, then unittest.TestCase.assertDictEquals() (and pytest and such) won't be able to generate a meaningful diff when 2 dicts aren't equal, and it will be much more complicated to understand why a test fails. Lines 1172 to 1175 in 89427cd
(well except if we fix unittest, but then external test runners will need fixing too, and it might not be the only place diff is used on the result of pprint to see how two objects differ) |
Consider adding a flag to switch between sorted order and insertion order (defaulting to the current behavior). For convenience, add a short-cut function to call pprint() with the flag set to False. def pprint(data, *, sort_dicts=True):
...
def pp(data, *args, **kwds):
pprint(data, *args, sort_dicts=False, **kwds) |
Hi!
We could use the parameters If we assign by default sort_dicts=True, that would have some error on unittest, isn't? |
pprint only sorts dicts currently so sort_dicts is better than sort that sounds more general. sort_dicts=True is the current behavior so I guess this is backwards compatible way to do this change. |
Yes. Because pprint just sort dicts, I think that is better rename the parameter like just sort. But was an opinion, that does not affect to the behavior.
I understand it. I was confuse. |
I agree with Karthikeyan, I would expect sort=True to sort all collections, including tuples and lists for example. |
Thanks for the contribution. |
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: