##// END OF EJS Templates
Add pretty-printing for types.SimpleNamespace
Eric Wieser -
Show More
@@ -648,6 +648,22 b' def _re_pattern_pprint(obj, p, cycle):'
648 p.text(')')
648 p.text(')')
649
649
650
650
651 def _types_simplenamespace_pprint(obj, p, cycle):
652 """The pprint function for types.SimpleNamespace."""
653 name = 'namespace'
654 with p.group(len(name) + 1, name + '(', ')'):
655 if cycle:
656 p.text('...')
657 else:
658 for idx, (attr, value) in enumerate(obj.__dict__.items()):
659 if idx:
660 p.text(',')
661 p.breakable()
662 attr_kwarg = '{}='.format(attr)
663 with p.group(len(attr_kwarg), attr_kwarg):
664 p.pretty(value)
665
666
651 def _type_pprint(obj, p, cycle):
667 def _type_pprint(obj, p, cycle):
652 """The pprint for classes and types."""
668 """The pprint for classes and types."""
653 # Heap allocated types might not have the module attribute,
669 # Heap allocated types might not have the module attribute,
@@ -741,6 +757,7 b' _type_pprinters = {'
741 types.FunctionType: _function_pprint,
757 types.FunctionType: _function_pprint,
742 types.BuiltinFunctionType: _function_pprint,
758 types.BuiltinFunctionType: _function_pprint,
743 types.MethodType: _repr_pprint,
759 types.MethodType: _repr_pprint,
760 types.SimpleNamespace: _types_simplenamespace_pprint,
744 datetime.datetime: _repr_pprint,
761 datetime.datetime: _repr_pprint,
745 datetime.timedelta: _repr_pprint,
762 datetime.timedelta: _repr_pprint,
746 _exception_base: _exception_pprint
763 _exception_base: _exception_pprint
@@ -407,6 +407,26 b' def test_mappingproxy():'
407 nt.assert_equal(pretty.pretty(obj), expected)
407 nt.assert_equal(pretty.pretty(obj), expected)
408
408
409
409
410 def test_simplenamespace():
411 SN = types.SimpleNamespace
412
413 sn_recursive = SN()
414 sn_recursive.first = sn_recursive
415 sn_recursive.second = sn_recursive
416 cases = [
417 (SN(), "namespace()"),
418 (SN(x=SN()), "namespace(x=namespace())"),
419 (SN(a_long_name=[SN(s=string.ascii_lowercase)]*3, a_short_name=None),
420 "namespace(a_long_name=[namespace(s='abcdefghijklmnopqrstuvwxyz'),\n"
421 " namespace(s='abcdefghijklmnopqrstuvwxyz'),\n"
422 " namespace(s='abcdefghijklmnopqrstuvwxyz')],\n"
423 " a_short_name=None)"),
424 (sn_recursive, "namespace(first=namespace(...), second=namespace(...))"),
425 ]
426 for obj, expected in cases:
427 nt.assert_equal(pretty.pretty(obj), expected)
428
429
410 def test_pretty_environ():
430 def test_pretty_environ():
411 dict_repr = pretty.pretty(dict(os.environ))
431 dict_repr = pretty.pretty(dict(os.environ))
412 # reindent to align with 'environ' prefix
432 # reindent to align with 'environ' prefix
General Comments 0
You need to be logged in to leave comments. Login now