Repository: ipython/ipython

Issue:
update pretty to use Python 3 style for sets
In Python 3, `repr(set([1,2,3]))` is `{1,2,3}`, but pretty currently forces the output to be `set([1,2,3])`.  We should probably use the set-literal format on Python 3, or just use it all the time.



Discussion:
The `{1,2,3}` syntax is supported in Python 2.7, so I'm in favor of using it always.

You should also change `frozenset([])` to `frozenset({})` while you're at it.


Resolving patch:
diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py
--- a/IPython/lib/pretty.py
+++ b/IPython/lib/pretty.py
@@ -512,7 +512,7 @@ def _default_pprint(obj, p, cycle):
 def _seq_pprinter_factory(start, end, basetype):
     """
     Factory that returns a pprint function useful for sequences.  Used by
-    the default pprint for tuples, dicts, lists, sets and frozensets.
+    the default pprint for tuples, dicts, and lists.
     """
     def inner(obj, p, cycle):
         typ = type(obj)
@@ -536,6 +536,40 @@ def inner(obj, p, cycle):
     return inner
 
 
+def _set_pprinter_factory(start, end, basetype):
+    """
+    Factory that returns a pprint function useful for sets and frozensets.
+    """
+    def inner(obj, p, cycle):
+        typ = type(obj)
+        if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__:
+            # If the subclass provides its own repr, use it instead.
+            return p.text(typ.__repr__(obj))
+
+        if cycle:
+            return p.text(start + '...' + end)
+        if len(obj) == 0:
+            # Special case.
+            p.text(basetype.__name__ + '()')
+        else:
+            step = len(start)
+            p.begin_group(step, start)
+            # Like dictionary keys, we will try to sort the items.
+            items = list(obj)
+            try:
+                items.sort()
+            except Exception:
+                # Sometimes the items don't sort.
+                pass
+            for idx, x in enumerate(items):
+                if idx:
+                    p.text(',')
+                    p.breakable()
+                p.pretty(x)
+            p.end_group(step, end)
+    return inner
+
+
 def _dict_pprinter_factory(start, end, basetype=None):
     """
     Factory that returns a pprint function used by the default pprint of
@@ -668,8 +702,8 @@ def _exception_pprint(obj, p, cycle):
     list:                       _seq_pprinter_factory('[', ']', list),
     dict:                       _dict_pprinter_factory('{', '}', dict),
     
-    set:                        _seq_pprinter_factory('{', '}', set),
-    frozenset:                  _seq_pprinter_factory('frozenset([', '])', frozenset),
+    set:                        _set_pprinter_factory('{', '}', set),
+    frozenset:                  _set_pprinter_factory('frozenset({', '})', frozenset),
     super:                      _super_pprint,
     _re_pattern_type:           _re_pattern_pprint,
     type:                       _type_pprint,


Test patch:
diff --git a/IPython/lib/tests/test_pretty.py b/IPython/lib/tests/test_pretty.py
--- a/IPython/lib/tests/test_pretty.py
+++ b/IPython/lib/tests/test_pretty.py
@@ -84,6 +84,20 @@ def test_callability_checking():
 
     nt.assert_equal(gotoutput, expectedoutput)
 
+
+def test_sets():
+    """
+    Test that set and frozenset use Python 3 formatting.
+    """
+    objects = [set(), frozenset(), set([1]), frozenset([1]), set([1, 2]),
+        frozenset([1, 2]), set([-1, -2, -3])]
+    expected = ['set()', 'frozenset()', '{1}', 'frozenset({1})', '{1, 2}',
+        'frozenset({1, 2})', '{-3, -2, -1}']
+    for obj, expected_output in zip(objects, expected):
+        got_output = pretty.pretty(obj)
+        yield nt.assert_equal, got_output, expected_output
+
+
 @skip_without('xxlimited')
 def test_pprint_heap_allocated_type():
     """