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
{{ message }}
Repository navigation
Pretty printing sorting for set and frozenset instances #71682
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='https://github.com/serhiy-storchaka'closed_at=Nonecreated_at=<Date2016-07-12.11:30:29.623>labels= ['type-bug', '3.8', '3.9', '3.10', '3.7', 'library']
title='Pretty printing sorting for set and frozenset instances'updated_at=<Date2020-10-25.20:05:08.751>user='https://github.com/danilobellini'
The pprint pretty printer in Python 3 sorts sets/frozensets only if their length don't fit in one single line/row for the given width, else it was just leaving repr(my_set_instance) alone, like:
That order is quite random in Python 3.2+. But on Python 2.6 and 2.7, the result is shown as:
set(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])
So for using pprint in doctests (or anything alike) with sets/frozensets, the pretty printer isn't as useful in Python 3 than it is in Python 2. The pprint tests for non-nested set/frozenset were only using some small ranges for testing. I've written a patch to solve that.
Since pprint() now supports all standard collections, I think it is worth to support them in saferepr() too. I have written an implementation and am finishing writing tests.
This issue is about sets/frozensets Python 3.2+, and I'm pretty sure it's backwards compatible, as I don't think any code running on Python 3.2.6 would depend on pprint randomness (how could?). Also, a multiline pprint would sort (tested with Python 3.2.6):
I see no reason to see a fix to this inconsistent behavior (sorting on multiline, not sorting on single line) as an enhancement just for a new Python 3.6 version. Besides being backwards compatible, the test_pprint was really verifying the order on set(range(n)) for small n, something that is already sorted by set.__repr__ but appears in test_pprint, which make me think it was intended as a pretty printer test, not as a set.__repr__ test.
Activity
danilobellini commentedon Jul 12, 2016
The pprint pretty printer in Python 3 sorts sets/frozensets only if their length don't fit in one single line/row for the given width, else it was just leaving repr(my_set_instance) alone, like:
That order is quite random in Python 3.2+. But on Python 2.6 and 2.7, the result is shown as:
set(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])
So for using pprint in doctests (or anything alike) with sets/frozensets, the pretty printer isn't as useful in Python 3 than it is in Python 2. The pprint tests for non-nested set/frozenset were only using some small ranges for testing. I've written a patch to solve that.
freddrake commentedon Jul 12, 2016
+1
It could reasonably be argued that not sorting is a bug for already-released 3.x versions.
rhettinger commentedon Jul 13, 2016
+1 for treating this as a bug fix.
serhiy-storchaka commentedon Jul 13, 2016
Since pprint() now supports all standard collections, I think it is worth to support them in saferepr() too. I have written an implementation and am finishing writing tests.
danilobellini commentedon Jul 13, 2016
Wouldn't a fix for all standard collections be a fix for Python 3.5+, therefore another issue? http://bugs.python.org/issue23870
This issue is about sets/frozensets Python 3.2+, and I'm pretty sure it's backwards compatible, as I don't think any code running on Python 3.2.6 would depend on pprint randomness (how could?). Also, a multiline pprint would sort (tested with Python 3.2.6):
I see no reason to see a fix to this inconsistent behavior (sorting on multiline, not sorting on single line) as an enhancement just for a new Python 3.6 version. Besides being backwards compatible, the test_pprint was really verifying the order on set(range(n)) for small n, something that is already sorted by set.__repr__ but appears in test_pprint, which make me think it was intended as a pretty printer test, not as a set.__repr__ test.
serhiy-storchaka commentedon Jul 24, 2016
Following patch adds support of all standard collections in pprint.saferepr().
stevendaprano commentedon Oct 25, 2016
SilentGhost commentedon May 24, 2020
Serhiy, would you be interested in converting your patch to a PR?