Show More
@@ -103,7 +103,7 b' def test_bad_precision():' | |||||
103 |
|
103 | |||
104 | def test_for_type(): |
|
104 | def test_for_type(): | |
105 | f = PlainTextFormatter() |
|
105 | f = PlainTextFormatter() | |
106 |
|
106 | |||
107 | # initial return, None |
|
107 | # initial return, None | |
108 | assert f.for_type(C, foo_printer) is None |
|
108 | assert f.for_type(C, foo_printer) is None | |
109 | # no func queries |
|
109 | # no func queries | |
@@ -116,9 +116,9 b' def test_for_type():' | |||||
116 |
|
116 | |||
117 | def test_for_type_string(): |
|
117 | def test_for_type_string(): | |
118 | f = PlainTextFormatter() |
|
118 | f = PlainTextFormatter() | |
119 |
|
119 | |||
120 | type_str = '%s.%s' % (C.__module__, 'C') |
|
120 | type_str = '%s.%s' % (C.__module__, 'C') | |
121 |
|
121 | |||
122 | # initial return, None |
|
122 | # initial return, None | |
123 | assert f.for_type(type_str, foo_printer) is None |
|
123 | assert f.for_type(type_str, foo_printer) is None | |
124 | # no func queries |
|
124 | # no func queries | |
@@ -130,9 +130,9 b' def test_for_type_string():' | |||||
130 |
|
130 | |||
131 | def test_for_type_by_name(): |
|
131 | def test_for_type_by_name(): | |
132 | f = PlainTextFormatter() |
|
132 | f = PlainTextFormatter() | |
133 |
|
133 | |||
134 | mod = C.__module__ |
|
134 | mod = C.__module__ | |
135 |
|
135 | |||
136 | # initial return, None |
|
136 | # initial return, None | |
137 | assert f.for_type_by_name(mod, "C", foo_printer) is None |
|
137 | assert f.for_type_by_name(mod, "C", foo_printer) is None | |
138 | # no func queries |
|
138 | # no func queries | |
@@ -146,7 +146,7 b' def test_for_type_by_name():' | |||||
146 |
|
146 | |||
147 | def test_lookup(): |
|
147 | def test_lookup(): | |
148 | f = PlainTextFormatter() |
|
148 | f = PlainTextFormatter() | |
149 |
|
149 | |||
150 | f.for_type(C, foo_printer) |
|
150 | f.for_type(C, foo_printer) | |
151 | assert f.lookup(C()) is foo_printer |
|
151 | assert f.lookup(C()) is foo_printer | |
152 | with pytest.raises(KeyError): |
|
152 | with pytest.raises(KeyError): | |
@@ -155,7 +155,7 b' def test_lookup():' | |||||
155 | def test_lookup_string(): |
|
155 | def test_lookup_string(): | |
156 | f = PlainTextFormatter() |
|
156 | f = PlainTextFormatter() | |
157 | type_str = '%s.%s' % (C.__module__, 'C') |
|
157 | type_str = '%s.%s' % (C.__module__, 'C') | |
158 |
|
158 | |||
159 | f.for_type(type_str, foo_printer) |
|
159 | f.for_type(type_str, foo_printer) | |
160 | assert f.lookup(C()) is foo_printer |
|
160 | assert f.lookup(C()) is foo_printer | |
161 | # should move from deferred to imported dict |
|
161 | # should move from deferred to imported dict | |
@@ -173,16 +173,16 b' def test_lookup_by_type_string():' | |||||
173 | f = PlainTextFormatter() |
|
173 | f = PlainTextFormatter() | |
174 | type_str = '%s.%s' % (C.__module__, 'C') |
|
174 | type_str = '%s.%s' % (C.__module__, 'C') | |
175 | f.for_type(type_str, foo_printer) |
|
175 | f.for_type(type_str, foo_printer) | |
176 |
|
176 | |||
177 | # verify insertion |
|
177 | # verify insertion | |
178 | assert _mod_name_key(C) in f.deferred_printers |
|
178 | assert _mod_name_key(C) in f.deferred_printers | |
179 | assert C not in f.type_printers |
|
179 | assert C not in f.type_printers | |
180 |
|
180 | |||
181 | assert f.lookup_by_type(type_str) is foo_printer |
|
181 | assert f.lookup_by_type(type_str) is foo_printer | |
182 | # lookup by string doesn't cause import |
|
182 | # lookup by string doesn't cause import | |
183 | assert _mod_name_key(C) in f.deferred_printers |
|
183 | assert _mod_name_key(C) in f.deferred_printers | |
184 | assert C not in f.type_printers |
|
184 | assert C not in f.type_printers | |
185 |
|
185 | |||
186 | assert f.lookup_by_type(C) is foo_printer |
|
186 | assert f.lookup_by_type(C) is foo_printer | |
187 | # should move from deferred to imported dict |
|
187 | # should move from deferred to imported dict | |
188 | assert _mod_name_key(C) not in f.deferred_printers |
|
188 | assert _mod_name_key(C) not in f.deferred_printers | |
@@ -220,10 +220,10 b' def test_pop():' | |||||
220 | def test_pop_string(): |
|
220 | def test_pop_string(): | |
221 | f = PlainTextFormatter() |
|
221 | f = PlainTextFormatter() | |
222 | type_str = '%s.%s' % (C.__module__, 'C') |
|
222 | type_str = '%s.%s' % (C.__module__, 'C') | |
223 |
|
223 | |||
224 | with pytest.raises(KeyError): |
|
224 | with pytest.raises(KeyError): | |
225 | f.pop(type_str) |
|
225 | f.pop(type_str) | |
226 |
|
226 | |||
227 | f.for_type(type_str, foo_printer) |
|
227 | f.for_type(type_str, foo_printer) | |
228 | f.pop(type_str) |
|
228 | f.pop(type_str) | |
229 | with pytest.raises(KeyError): |
|
229 | with pytest.raises(KeyError): | |
@@ -238,7 +238,7 b' def test_pop_string():' | |||||
238 | with pytest.raises(KeyError): |
|
238 | with pytest.raises(KeyError): | |
239 | f.pop(type_str) |
|
239 | f.pop(type_str) | |
240 | assert f.pop(type_str, None) is None |
|
240 | assert f.pop(type_str, None) is None | |
241 |
|
241 | |||
242 |
|
242 | |||
243 | def test_error_method(): |
|
243 | def test_error_method(): | |
244 | f = HTMLFormatter() |
|
244 | f = HTMLFormatter() | |
@@ -341,14 +341,14 b' def test_print_method_weird():' | |||||
341 | assert text_hat._repr_html_ == "_repr_html_" |
|
341 | assert text_hat._repr_html_ == "_repr_html_" | |
342 | with capture_output() as captured: |
|
342 | with capture_output() as captured: | |
343 | result = f(text_hat) |
|
343 | result = f(text_hat) | |
344 |
|
344 | |||
345 | assert result is None |
|
345 | assert result is None | |
346 | assert "FormatterWarning" not in captured.stderr |
|
346 | assert "FormatterWarning" not in captured.stderr | |
347 |
|
347 | |||
348 | class CallableMagicHat(object): |
|
348 | class CallableMagicHat(object): | |
349 | def __getattr__(self, key): |
|
349 | def __getattr__(self, key): | |
350 | return lambda : key |
|
350 | return lambda : key | |
351 |
|
351 | |||
352 | call_hat = CallableMagicHat() |
|
352 | call_hat = CallableMagicHat() | |
353 | with capture_output() as captured: |
|
353 | with capture_output() as captured: | |
354 | result = f(call_hat) |
|
354 | result = f(call_hat) | |
@@ -358,11 +358,11 b' def test_print_method_weird():' | |||||
358 | class BadReprArgs(object): |
|
358 | class BadReprArgs(object): | |
359 | def _repr_html_(self, extra, args): |
|
359 | def _repr_html_(self, extra, args): | |
360 | return "html" |
|
360 | return "html" | |
361 |
|
361 | |||
362 | bad = BadReprArgs() |
|
362 | bad = BadReprArgs() | |
363 | with capture_output() as captured: |
|
363 | with capture_output() as captured: | |
364 | result = f(bad) |
|
364 | result = f(bad) | |
365 |
|
365 | |||
366 | assert result is None |
|
366 | assert result is None | |
367 | assert "FormatterWarning" not in captured.stderr |
|
367 | assert "FormatterWarning" not in captured.stderr | |
368 |
|
368 | |||
@@ -406,13 +406,13 b' def test_ipython_display_formatter():' | |||||
406 | class NotSelfDisplaying(object): |
|
406 | class NotSelfDisplaying(object): | |
407 | def __repr__(self): |
|
407 | def __repr__(self): | |
408 | return "NotSelfDisplaying" |
|
408 | return "NotSelfDisplaying" | |
409 |
|
409 | |||
410 | def _ipython_display_(self): |
|
410 | def _ipython_display_(self): | |
411 | raise NotImplementedError |
|
411 | raise NotImplementedError | |
412 |
|
412 | |||
413 | save_enabled = f.ipython_display_formatter.enabled |
|
413 | save_enabled = f.ipython_display_formatter.enabled | |
414 | f.ipython_display_formatter.enabled = True |
|
414 | f.ipython_display_formatter.enabled = True | |
415 |
|
415 | |||
416 | yes = SelfDisplaying() |
|
416 | yes = SelfDisplaying() | |
417 | no = NotSelfDisplaying() |
|
417 | no = NotSelfDisplaying() | |
418 |
|
418 | |||
@@ -444,7 +444,7 b' def test_repr_mime():' | |||||
444 | return 'should-be-overwritten' |
|
444 | return 'should-be-overwritten' | |
445 | def _repr_html_(self): |
|
445 | def _repr_html_(self): | |
446 | return '<b>hi!</b>' |
|
446 | return '<b>hi!</b>' | |
447 |
|
447 | |||
448 | f = get_ipython().display_formatter |
|
448 | f = get_ipython().display_formatter | |
449 | html_f = f.formatters['text/html'] |
|
449 | html_f = f.formatters['text/html'] | |
450 | save_enabled = html_f.enabled |
|
450 | save_enabled = html_f.enabled | |
@@ -507,7 +507,7 b' def test_repr_mime_meta():' | |||||
507 | } |
|
507 | } | |
508 | } |
|
508 | } | |
509 | return (data, metadata) |
|
509 | return (data, metadata) | |
510 |
|
510 | |||
511 | f = get_ipython().display_formatter |
|
511 | f = get_ipython().display_formatter | |
512 | obj = HasReprMimeMeta() |
|
512 | obj = HasReprMimeMeta() | |
513 | d, md = f.format(obj) |
|
513 | d, md = f.format(obj) | |
@@ -529,3 +529,18 b' def test_repr_mime_failure():' | |||||
529 | obj = BadReprMime() |
|
529 | obj = BadReprMime() | |
530 | d, md = f.format(obj) |
|
530 | d, md = f.format(obj) | |
531 | assert "text/plain" in d |
|
531 | assert "text/plain" in d | |
|
532 | ||||
|
533 | ||||
|
534 | def test_custom_repr_namedtuple_partialmethod(): | |||
|
535 | from functools import partialmethod | |||
|
536 | from typing import NamedTuple | |||
|
537 | ||||
|
538 | class Foo(NamedTuple): | |||
|
539 | ... | |||
|
540 | ||||
|
541 | Foo.__repr__ = partialmethod(lambda obj: "Hello World") | |||
|
542 | foo = Foo() | |||
|
543 | ||||
|
544 | f = PlainTextFormatter() | |||
|
545 | assert f.pprint | |||
|
546 | assert f(foo) == "Hello World" |
@@ -406,8 +406,16 b' class RepresentationPrinter(PrettyPrinter):' | |||||
406 | meth = cls._repr_pretty_ |
|
406 | meth = cls._repr_pretty_ | |
407 | if callable(meth): |
|
407 | if callable(meth): | |
408 | return meth(obj, self, cycle) |
|
408 | return meth(obj, self, cycle) | |
409 |
if |
|
409 | if ( | |
410 | and callable(cls.__dict__.get('__repr__')): |
|
410 | cls is not object | |
|
411 | # check if cls defines __repr__ | |||
|
412 | and "__repr__" in cls.__dict__ | |||
|
413 | # check if __repr__ is callable. | |||
|
414 | # Note: we need to test getattr(cls, '__repr__') | |||
|
415 | # instead of cls.__dict__['__repr__'] | |||
|
416 | # in order to work with descriptors like partialmethod, | |||
|
417 | and callable(_safe_getattr(cls, "__repr__", None)) | |||
|
418 | ): | |||
411 | return _repr_pprint(obj, self, cycle) |
|
419 | return _repr_pprint(obj, self, cycle) | |
412 |
|
420 | |||
413 | return _default_pprint(obj, self, cycle) |
|
421 | return _default_pprint(obj, self, cycle) |
General Comments 0
You need to be logged in to leave comments.
Login now