Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e9d4437

Browse files
committed
Add a types.MappingProxyType pretty printer ipython#9821
Also fixed the _dict_pprinter_factory indentation step
1 parent ca9e56d commit e9d4437

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

IPython/lib/pretty.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,8 @@ def inner(obj, p, cycle):
605605

606606
if cycle:
607607
return p.text('{...}')
608-
p.begin_group(1, start)
608+
step = len(start)
609+
p.begin_group(step, start)
609610
keys = obj.keys()
610611
# if dict isn't large enough to be truncated, sort keys before displaying
611612
if not (p.max_seq_length and len(obj) >= p.max_seq_length):
@@ -621,7 +622,7 @@ def inner(obj, p, cycle):
621622
p.pretty(key)
622623
p.text(': ')
623624
p.pretty(obj[key])
624-
p.end_group(1, end)
625+
p.end_group(step, end)
625626
return inner
626627

627628

@@ -760,6 +761,8 @@ def _exception_pprint(obj, p, cycle):
760761
_type_pprinters[types.ClassType] = _type_pprint
761762
_type_pprinters[types.SliceType] = _repr_pprint
762763
except AttributeError: # Python 3
764+
_type_pprinters[types.MappingProxyType] = \
765+
_dict_pprinter_factory('mappingproxy({', '})')
763766
_type_pprinters[slice] = _repr_pprint
764767

765768
try:

IPython/lib/tests/test_pretty.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
from __future__ import print_function
88

99
from collections import Counter, defaultdict, deque, OrderedDict
10+
import types, string
1011

1112
import nose.tools as nt
1213

1314
from IPython.lib import pretty
14-
from IPython.testing.decorators import skip_without, py2_only
15+
from IPython.testing.decorators import skip_without, py2_only, py3_only
1516
from IPython.utils.py3compat import PY3, unicode_to_str
1617

1718
if PY3:
@@ -436,3 +437,48 @@ class MyCounter(Counter):
436437
]
437438
for obj, expected in cases:
438439
nt.assert_equal(pretty.pretty(obj), expected)
440+
441+
@py3_only
442+
def test_mappingproxy():
443+
MP = types.MappingProxyType
444+
underlying_dict = {}
445+
mp_recursive = MP(underlying_dict)
446+
underlying_dict[2] = mp_recursive
447+
underlying_dict[3] = underlying_dict
448+
449+
cases = [
450+
(MP({}), "mappingproxy({})"),
451+
(MP({None: MP({})}), "mappingproxy({None: mappingproxy({})})"),
452+
(MP({k: k.upper() for k in string.ascii_lowercase}),
453+
"mappingproxy({'a': 'A',\n"
454+
" 'b': 'B',\n"
455+
" 'c': 'C',\n"
456+
" 'd': 'D',\n"
457+
" 'e': 'E',\n"
458+
" 'f': 'F',\n"
459+
" 'g': 'G',\n"
460+
" 'h': 'H',\n"
461+
" 'i': 'I',\n"
462+
" 'j': 'J',\n"
463+
" 'k': 'K',\n"
464+
" 'l': 'L',\n"
465+
" 'm': 'M',\n"
466+
" 'n': 'N',\n"
467+
" 'o': 'O',\n"
468+
" 'p': 'P',\n"
469+
" 'q': 'Q',\n"
470+
" 'r': 'R',\n"
471+
" 's': 'S',\n"
472+
" 't': 'T',\n"
473+
" 'u': 'U',\n"
474+
" 'v': 'V',\n"
475+
" 'w': 'W',\n"
476+
" 'x': 'X',\n"
477+
" 'y': 'Y',\n"
478+
" 'z': 'Z'})"),
479+
(mp_recursive, "mappingproxy({2: {...}, 3: {2: {...}, 3: {...}}})"),
480+
(underlying_dict,
481+
"{2: mappingproxy({2: {...}, 3: {...}}), 3: {...}}"),
482+
]
483+
for obj, expected in cases:
484+
nt.assert_equal(pretty.pretty(obj), expected)

0 commit comments

Comments
 (0)