diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index ccb07f6..9a60f43 100644 --- a/IPython/lib/pretty.py +++ b/IPython/lib/pretty.py @@ -554,7 +554,14 @@ def _set_pprinter_factory(start, end, basetype): else: step = len(start) p.begin_group(step, start) - for idx, x in enumerate(obj): + # 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() diff --git a/IPython/lib/tests/test_pretty.py b/IPython/lib/tests/test_pretty.py index b12c6d4..eff710f 100644 --- a/IPython/lib/tests/test_pretty.py +++ b/IPython/lib/tests/test_pretty.py @@ -89,10 +89,10 @@ 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])] + 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})'] + '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