This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Pretty printing sorting for set and frozenset instances
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: SilentGhost, bbonenfant, danilo.bellini, fdrake, rhettinger, serhiy.storchaka, steven.daprano
Priority: normal Keywords: patch

Created on 2016-07-12 11:30 by danilo.bellini, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
pprint_small_set_sorted.patch danilo.bellini, 2016-07-12 11:30 Sorts small set/frozenset on pprint and adds test_sorted_set_frozenset review
saferepr_collections.patch serhiy.storchaka, 2016-07-24 14:25 review
Pull Requests
URL Status Linked Edit
PR 22977 open bbonenfant, 2020-10-25 20:05
Messages (8)
msg270237 - (view) Author: Danilo J. S. Bellini (danilo.bellini) * Date: 2016-07-12 11:30
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:

>>> import string, pprint
>>> pprint.pprint(set(string.digits))
{'7', '5', '2', '4', '1', '9', '6', '3', '0', '8'}

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.
msg270246 - (view) Author: Fred Drake (fdrake) (Python committer) Date: 2016-07-12 14:01
+1

It could reasonably be argued that not sorting is a bug for already-released 3.x versions.
msg270325 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-07-13 16:43
+1 for treating this as a bug fix.
msg270326 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-07-13 16:51
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.
msg270332 - (view) Author: Danilo J. S. Bellini (danilo.bellini) * Date: 2016-07-13 17:34
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):

>>> pprint.pprint(set(string.digits), width=7)
{'0',
 '1',
 '2',
 '3',
 '4',
 '5',
 '6',
 '7',
 '8',
 '9'}

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.
msg271155 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-07-24 14:25
Following patch adds support of all standard collections in pprint.saferepr().
msg279412 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-10-25 14:59
There seems to be consensus that this should be treated as a bug fix, not a new feature. Could this still make it into 3.6 even though it missed the first beta?
msg369795 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2020-05-24 12:15
Serhiy, would you be interested in converting your patch to a PR?
History
Date User Action Args
2022-04-11 14:58:33adminsetgithub: 71682
2020-10-25 20:05:08bbonenfantsetnosy: + bbonenfant
pull_requests: + pull_request21894
2020-05-24 12:15:25SilentGhostsetmessages: + msg369795
versions: + Python 3.10
2019-12-13 15:41:25SilentGhostsetnosy: + SilentGhost

type: enhancement -> behavior
versions: + Python 3.7, Python 3.8, Python 3.9, - Python 3.6
2016-10-25 14:59:16steven.dapranosetnosy: + steven.daprano
messages: + msg279412
2016-07-24 14:25:54serhiy.storchakasetfiles: + saferepr_collections.patch

messages: + msg271155
2016-07-13 17:34:36danilo.bellinisetmessages: + msg270332
2016-07-13 16:51:43serhiy.storchakasetmessages: + msg270326
2016-07-13 16:43:00rhettingersetmessages: + msg270325
2016-07-12 14:01:01fdrakesetmessages: + msg270246
2016-07-12 12:38:45serhiy.storchakasetassignee: serhiy.storchaka

type: behavior -> enhancement
nosy: + rhettinger, serhiy.storchaka
versions: - Python 3.5
2016-07-12 12:25:32SilentGhostsetnosy: + fdrake
stage: patch review

versions: - Python 3.2, Python 3.3, Python 3.4
2016-07-12 11:30:29danilo.bellinicreate