Show More
@@ -49,7 +49,7 b' def test_pretty():' | |||
|
49 | 49 | f = PlainTextFormatter() |
|
50 | 50 | f.for_type(A, foo_printer) |
|
51 | 51 | nt.assert_equal(f(A()), 'foo') |
|
52 |
nt.assert_equal(f(B()), ' |
|
|
52 | nt.assert_equal(f(B()), 'B()') | |
|
53 | 53 | nt.assert_equal(f(GoodPretty()), 'foo') |
|
54 | 54 | # Just don't raise an exception for the following: |
|
55 | 55 | f(BadPretty()) |
@@ -395,6 +395,10 b' class RepresentationPrinter(PrettyPrinter):' | |||
|
395 | 395 | meth = cls._repr_pretty_ |
|
396 | 396 | if callable(meth): |
|
397 | 397 | return meth(obj, self, cycle) |
|
398 | if cls is not object \ | |
|
399 | and callable(cls.__dict__.get('__repr__')): | |
|
400 | return _repr_pprint(obj, self, cycle) | |
|
401 | ||
|
398 | 402 | return _default_pprint(obj, self, cycle) |
|
399 | 403 | finally: |
|
400 | 404 | self.end_group() |
@@ -540,17 +544,12 b' def _default_pprint(obj, p, cycle):' | |||
|
540 | 544 | p.end_group(1, '>') |
|
541 | 545 | |
|
542 | 546 | |
|
543 |
def _seq_pprinter_factory(start, end |
|
|
547 | def _seq_pprinter_factory(start, end): | |
|
544 | 548 | """ |
|
545 | 549 | Factory that returns a pprint function useful for sequences. Used by |
|
546 | 550 | the default pprint for tuples, dicts, and lists. |
|
547 | 551 | """ |
|
548 | 552 | def inner(obj, p, cycle): |
|
549 | typ = type(obj) | |
|
550 | if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__: | |
|
551 | # If the subclass provides its own repr, use it instead. | |
|
552 | return p.text(typ.__repr__(obj)) | |
|
553 | ||
|
554 | 553 | if cycle: |
|
555 | 554 | return p.text(start + '...' + end) |
|
556 | 555 | step = len(start) |
@@ -567,21 +566,16 b' def _seq_pprinter_factory(start, end, basetype):' | |||
|
567 | 566 | return inner |
|
568 | 567 | |
|
569 | 568 | |
|
570 |
def _set_pprinter_factory(start, end |
|
|
569 | def _set_pprinter_factory(start, end): | |
|
571 | 570 | """ |
|
572 | 571 | Factory that returns a pprint function useful for sets and frozensets. |
|
573 | 572 | """ |
|
574 | 573 | def inner(obj, p, cycle): |
|
575 | typ = type(obj) | |
|
576 | if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__: | |
|
577 | # If the subclass provides its own repr, use it instead. | |
|
578 | return p.text(typ.__repr__(obj)) | |
|
579 | ||
|
580 | 574 | if cycle: |
|
581 | 575 | return p.text(start + '...' + end) |
|
582 | 576 | if len(obj) == 0: |
|
583 | 577 | # Special case. |
|
584 |
p.text( |
|
|
578 | p.text(type(obj).__name__ + '()') | |
|
585 | 579 | else: |
|
586 | 580 | step = len(start) |
|
587 | 581 | p.begin_group(step, start) |
@@ -599,17 +593,12 b' def _set_pprinter_factory(start, end, basetype):' | |||
|
599 | 593 | return inner |
|
600 | 594 | |
|
601 | 595 | |
|
602 |
def _dict_pprinter_factory(start, end |
|
|
596 | def _dict_pprinter_factory(start, end): | |
|
603 | 597 | """ |
|
604 | 598 | Factory that returns a pprint function used by the default pprint of |
|
605 | 599 | dicts and dict proxies. |
|
606 | 600 | """ |
|
607 | 601 | def inner(obj, p, cycle): |
|
608 | typ = type(obj) | |
|
609 | if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__: | |
|
610 | # If the subclass provides its own repr, use it instead. | |
|
611 | return p.text(typ.__repr__(obj)) | |
|
612 | ||
|
613 | 602 | if cycle: |
|
614 | 603 | return p.text('{...}') |
|
615 | 604 | step = len(start) |
@@ -750,12 +739,12 b' _type_pprinters = {' | |||
|
750 | 739 | int: _repr_pprint, |
|
751 | 740 | float: _repr_pprint, |
|
752 | 741 | str: _repr_pprint, |
|
753 |
tuple: _seq_pprinter_factory('(', ')' |
|
|
754 |
list: _seq_pprinter_factory('[', ']' |
|
|
755 |
dict: _dict_pprinter_factory('{', '}' |
|
|
742 | tuple: _seq_pprinter_factory('(', ')'), | |
|
743 | list: _seq_pprinter_factory('[', ']'), | |
|
744 | dict: _dict_pprinter_factory('{', '}'), | |
|
756 | 745 | |
|
757 |
set: _set_pprinter_factory('{', '}' |
|
|
758 |
frozenset: _set_pprinter_factory('frozenset({', '})' |
|
|
746 | set: _set_pprinter_factory('{', '}'), | |
|
747 | frozenset: _set_pprinter_factory('frozenset({', '})'), | |
|
759 | 748 | super: _super_pprint, |
|
760 | 749 | _re_pattern_type: _re_pattern_pprint, |
|
761 | 750 | type: _type_pprint, |
@@ -420,4 +420,24 b' def test_function_pretty():' | |||
|
420 | 420 | return "Don't panic" |
|
421 | 421 | |
|
422 | 422 | nt.assert_in('meaning_of_life(question=None)', pretty.pretty(meaning_of_life)) |
|
423 | ||
|
423 | ||
|
424 | ||
|
425 | class OrderedCounter(Counter, OrderedDict): | |
|
426 | 'Counter that remembers the order elements are first encountered' | |
|
427 | ||
|
428 | def __repr__(self): | |
|
429 | return '%s(%r)' % (self.__class__.__name__, OrderedDict(self)) | |
|
430 | ||
|
431 | def __reduce__(self): | |
|
432 | return self.__class__, (OrderedDict(self),) | |
|
433 | ||
|
434 | class MySet(set): # Override repr of a basic type | |
|
435 | def __repr__(self): | |
|
436 | return 'mine' | |
|
437 | ||
|
438 | def test_custom_repr(): | |
|
439 | """A custom repr should override a pretty printer for a parent type""" | |
|
440 | oc = OrderedCounter("abracadabra") | |
|
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