From 324eddd415640132c8d599479dddc5bb5768099d 2017-12-21 11:46:28 From: Thomas Kluyver Date: 2017-12-21 11:46:28 Subject: [PATCH] Don't sort dicts for display in Python >= 3.7 Closes gh-10110 --- diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index cbbb726..6787d38 100644 --- a/IPython/lib/pretty.py +++ b/IPython/lib/pretty.py @@ -95,6 +95,7 @@ __all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter', MAX_SEQ_LENGTH = 1000 +DICT_IS_ORDERED = sys.version_info >= (3, 7) _re_pattern_type = type(re.compile('')) def _safe_getattr(obj, attr, default=None): @@ -613,7 +614,9 @@ def _dict_pprinter_factory(start, end, basetype=None): p.begin_group(step, start) keys = obj.keys() # if dict isn't large enough to be truncated, sort keys before displaying - if not (p.max_seq_length and len(obj) >= p.max_seq_length): + # From Python 3.7, dicts preserve order by definition, so we don't sort. + if not DICT_IS_ORDERED \ + and not (p.max_seq_length and len(obj) >= p.max_seq_length): keys = _sorted_for_pprint(keys) for idx, key in p._enumerate(keys): if idx: