Show More
@@ -541,17 +541,12 b' def _default_pprint(obj, p, cycle):' | |||
|
541 | 541 | p.end_group(1, '>') |
|
542 | 542 | |
|
543 | 543 | |
|
544 |
def _seq_pprinter_factory(start, end |
|
|
544 | def _seq_pprinter_factory(start, end): | |
|
545 | 545 | """ |
|
546 | 546 | Factory that returns a pprint function useful for sequences. Used by |
|
547 | 547 | the default pprint for tuples, dicts, and lists. |
|
548 | 548 | """ |
|
549 | 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 | 550 | if cycle: |
|
556 | 551 | return p.text(start + '...' + end) |
|
557 | 552 | step = len(start) |
@@ -568,21 +563,16 b' def _seq_pprinter_factory(start, end, basetype):' | |||
|
568 | 563 | return inner |
|
569 | 564 | |
|
570 | 565 | |
|
571 |
def _set_pprinter_factory(start, end |
|
|
566 | def _set_pprinter_factory(start, end): | |
|
572 | 567 | """ |
|
573 | 568 | Factory that returns a pprint function useful for sets and frozensets. |
|
574 | 569 | """ |
|
575 | 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 | 571 | if cycle: |
|
582 | 572 | return p.text(start + '...' + end) |
|
583 | 573 | if len(obj) == 0: |
|
584 | 574 | # Special case. |
|
585 |
p.text( |
|
|
575 | p.text(type(obj).__name__ + '()') | |
|
586 | 576 | else: |
|
587 | 577 | step = len(start) |
|
588 | 578 | p.begin_group(step, start) |
@@ -600,17 +590,12 b' def _set_pprinter_factory(start, end, basetype):' | |||
|
600 | 590 | return inner |
|
601 | 591 | |
|
602 | 592 | |
|
603 |
def _dict_pprinter_factory(start, end |
|
|
593 | def _dict_pprinter_factory(start, end): | |
|
604 | 594 | """ |
|
605 | 595 | Factory that returns a pprint function used by the default pprint of |
|
606 | 596 | dicts and dict proxies. |
|
607 | 597 | """ |
|
608 | 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 | 599 | if cycle: |
|
615 | 600 | return p.text('{...}') |
|
616 | 601 | step = len(start) |
@@ -749,12 +734,12 b' _type_pprinters = {' | |||
|
749 | 734 | int: _repr_pprint, |
|
750 | 735 | float: _repr_pprint, |
|
751 | 736 | str: _repr_pprint, |
|
752 |
tuple: _seq_pprinter_factory('(', ')' |
|
|
753 |
list: _seq_pprinter_factory('[', ']' |
|
|
754 |
dict: _dict_pprinter_factory('{', '}' |
|
|
737 | tuple: _seq_pprinter_factory('(', ')'), | |
|
738 | list: _seq_pprinter_factory('[', ']'), | |
|
739 | dict: _dict_pprinter_factory('{', '}'), | |
|
755 | 740 | |
|
756 |
set: _set_pprinter_factory('{', '}' |
|
|
757 |
frozenset: _set_pprinter_factory('frozenset({', '})' |
|
|
741 | set: _set_pprinter_factory('{', '}'), | |
|
742 | frozenset: _set_pprinter_factory('frozenset({', '})'), | |
|
758 | 743 | super: _super_pprint, |
|
759 | 744 | _re_pattern_type: _re_pattern_pprint, |
|
760 | 745 | type: _type_pprint, |
@@ -431,7 +431,13 b' class OrderedCounter(Counter, OrderedDict):' | |||
|
431 | 431 | def __reduce__(self): |
|
432 | 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 | 438 | def test_custom_repr(): |
|
435 | 439 | """A custom repr should override a pretty printer for a parent type""" |
|
436 | 440 | oc = OrderedCounter("abracadabra") |
|
437 | 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