##// END OF EJS Templates
Remove unnecessary code checking for __repr__ on a subclass of builtin types...
Thomas Kluyver -
Show More
@@ -541,17 +541,12 b' def _default_pprint(obj, p, cycle):'
541 p.end_group(1, '>')
541 p.end_group(1, '>')
542
542
543
543
544 def _seq_pprinter_factory(start, end, basetype):
544 def _seq_pprinter_factory(start, end):
545 """
545 """
546 Factory that returns a pprint function useful for sequences. Used by
546 Factory that returns a pprint function useful for sequences. Used by
547 the default pprint for tuples, dicts, and lists.
547 the default pprint for tuples, dicts, and lists.
548 """
548 """
549 def inner(obj, p, cycle):
549 def inner(obj, p, cycle):
550 typ = type(obj)
551 if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__:
552 # If the subclass provides its own repr, use it instead.
553 return p.text(typ.__repr__(obj))
554
555 if cycle:
550 if cycle:
556 return p.text(start + '...' + end)
551 return p.text(start + '...' + end)
557 step = len(start)
552 step = len(start)
@@ -568,21 +563,16 b' def _seq_pprinter_factory(start, end, basetype):'
568 return inner
563 return inner
569
564
570
565
571 def _set_pprinter_factory(start, end, basetype):
566 def _set_pprinter_factory(start, end):
572 """
567 """
573 Factory that returns a pprint function useful for sets and frozensets.
568 Factory that returns a pprint function useful for sets and frozensets.
574 """
569 """
575 def inner(obj, p, cycle):
570 def inner(obj, p, cycle):
576 typ = type(obj)
577 if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__:
578 # If the subclass provides its own repr, use it instead.
579 return p.text(typ.__repr__(obj))
580
581 if cycle:
571 if cycle:
582 return p.text(start + '...' + end)
572 return p.text(start + '...' + end)
583 if len(obj) == 0:
573 if len(obj) == 0:
584 # Special case.
574 # Special case.
585 p.text(basetype.__name__ + '()')
575 p.text(type(obj).__name__ + '()')
586 else:
576 else:
587 step = len(start)
577 step = len(start)
588 p.begin_group(step, start)
578 p.begin_group(step, start)
@@ -600,17 +590,12 b' def _set_pprinter_factory(start, end, basetype):'
600 return inner
590 return inner
601
591
602
592
603 def _dict_pprinter_factory(start, end, basetype=None):
593 def _dict_pprinter_factory(start, end):
604 """
594 """
605 Factory that returns a pprint function used by the default pprint of
595 Factory that returns a pprint function used by the default pprint of
606 dicts and dict proxies.
596 dicts and dict proxies.
607 """
597 """
608 def inner(obj, p, cycle):
598 def inner(obj, p, cycle):
609 typ = type(obj)
610 if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__:
611 # If the subclass provides its own repr, use it instead.
612 return p.text(typ.__repr__(obj))
613
614 if cycle:
599 if cycle:
615 return p.text('{...}')
600 return p.text('{...}')
616 step = len(start)
601 step = len(start)
@@ -749,12 +734,12 b' _type_pprinters = {'
749 int: _repr_pprint,
734 int: _repr_pprint,
750 float: _repr_pprint,
735 float: _repr_pprint,
751 str: _repr_pprint,
736 str: _repr_pprint,
752 tuple: _seq_pprinter_factory('(', ')', tuple),
737 tuple: _seq_pprinter_factory('(', ')'),
753 list: _seq_pprinter_factory('[', ']', list),
738 list: _seq_pprinter_factory('[', ']'),
754 dict: _dict_pprinter_factory('{', '}', dict),
739 dict: _dict_pprinter_factory('{', '}'),
755
740
756 set: _set_pprinter_factory('{', '}', set),
741 set: _set_pprinter_factory('{', '}'),
757 frozenset: _set_pprinter_factory('frozenset({', '})', frozenset),
742 frozenset: _set_pprinter_factory('frozenset({', '})'),
758 super: _super_pprint,
743 super: _super_pprint,
759 _re_pattern_type: _re_pattern_pprint,
744 _re_pattern_type: _re_pattern_pprint,
760 type: _type_pprint,
745 type: _type_pprint,
@@ -431,7 +431,13 b' class OrderedCounter(Counter, OrderedDict):'
431 def __reduce__(self):
431 def __reduce__(self):
432 return self.__class__, (OrderedDict(self),)
432 return self.__class__, (OrderedDict(self),)
433
433
434 class MySet(set): # Override repr of a basic type
435 def __repr__(self):
436 return 'mine'
437
434 def test_custom_repr():
438 def test_custom_repr():
435 """A custom repr should override a pretty printer for a parent type"""
439 """A custom repr should override a pretty printer for a parent type"""
436 oc = OrderedCounter("abracadabra")
440 oc = OrderedCounter("abracadabra")
437 nt.assert_in("OrderedCounter(OrderedDict", pretty.pretty(oc))
441 nt.assert_in("OrderedCounter(OrderedDict", pretty.pretty(oc))
442
443 nt.assert_equal(pretty.pretty(MySet()), 'mine')
General Comments 0
You need to be logged in to leave comments. Login now