Skip to content

Pretty printing sorting for set and frozenset instances #71682

Open

Description

@danilobellini
BPO 27495
Nosy @freddrake, @rhettinger, @stevendaprano, @serhiy-storchaka, @danilobellini, @bbonenfant
PRs
  • gh-71682: Make pprint.pformat always order sets #22977
  • Files
  • pprint_small_set_sorted.patch: Sorts small set/frozenset on pprint and adds test_sorted_set_frozenset
  • saferepr_collections.patch
  • 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 = None
    created_at = <Date 2016-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 = <Date 2020-10-25.20:05:08.751>
    user = 'https://github.com/danilobellini'

    bugs.python.org fields:

    activity = <Date 2020-10-25.20:05:08.751>
    actor = 'bbonenfant'
    assignee = 'serhiy.storchaka'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2016-07-12.11:30:29.623>
    creator = 'danilo.bellini'
    dependencies = []
    files = ['43698', '43864']
    hgrepos = []
    issue_num = 27495
    keywords = ['patch']
    message_count = 8.0
    messages = ['270237', '270246', '270325', '270326', '270332', '271155', '279412', '369795']
    nosy_count = 7.0
    nosy_names = ['fdrake', 'rhettinger', 'steven.daprano', 'SilentGhost', 'serhiy.storchaka', 'danilo.bellini', 'bbonenfant']
    pr_nums = ['22977']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue27495'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10']

    Activity

    1. danilobellini commented on Jul 12, 2016

      danilobellinimannequin
      MannequinAuthor

      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.

    2. added
      type-bug
      stdlib
      on Jul 12, 2016
    3. added
      type-feature
      and removed
      type-bug
      on Jul 12, 2016
    4. freddrake commented on Jul 12, 2016

      @freddrake
      Member

      +1

      It could reasonably be argued that not sorting is a bug for already-released 3.x versions.

    5. rhettinger commented on Jul 13, 2016

      @rhettinger
      Contributor

      +1 for treating this as a bug fix.

    6. serhiy-storchaka commented on Jul 13, 2016

      @serhiy-storchaka
      Member

      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.

    7. danilobellini commented on Jul 13, 2016

      danilobellinimannequin
      MannequinAuthor

      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.

    8. serhiy-storchaka commented on Jul 24, 2016

      @serhiy-storchaka
      Member

      Following patch adds support of all standard collections in pprint.saferepr().

    9. stevendaprano commented on Oct 25, 2016

      @stevendaprano
    10. added
      3.7 (EOL)
      3.8 (EOL)
      3.9 (EOL)
      type-bug
      and removed
      type-feature
      on Dec 13, 2019
    11. SilentGhost commented on May 24, 2020

      SilentGhostmannequin
      Mannequin

      Serhiy, would you be interested in converting your patch to a PR?

    12. transferred this issue fromon Apr 10, 2022
    13. added
      type-feature
      and removed
      type-bug
      3.10 (EOL)
      3.9 (EOL)
      3.8 (EOL)
      3.7 (EOL)
      on Dec 3, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Labels

    stdlibtype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions