From 53c1b5e6a3f83ae221d6b8d581e7d959faec43c1 2024-06-20 10:59:36 From: Randolf Scholz Date: 2024-06-20 10:59:36 Subject: [PATCH] fixed honoring custom repr for NamedTuple if assigned by partialmethod --- diff --git a/IPython/core/tests/test_formatters.py b/IPython/core/tests/test_formatters.py index c642bef..2bfffea 100644 --- a/IPython/core/tests/test_formatters.py +++ b/IPython/core/tests/test_formatters.py @@ -529,3 +529,18 @@ def test_repr_mime_failure(): obj = BadReprMime() d, md = f.format(obj) assert "text/plain" in d + + +def test_custom_repr_namedtuple_partialmethod(): + from functools import partialmethod + from typing import NamedTuple + + class Foo(NamedTuple): + ... + + Foo.__repr__ = partialmethod(lambda obj: "Hello World") + foo = Foo() + + f = PlainTextFormatter() + assert f.pprint + assert f(foo) == "Hello World" diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index 631445b..44df8e4 100644 --- a/IPython/lib/pretty.py +++ b/IPython/lib/pretty.py @@ -406,8 +406,7 @@ class RepresentationPrinter(PrettyPrinter): meth = cls._repr_pretty_ if callable(meth): return meth(obj, self, cycle) - if cls is not object \ - and callable(cls.__dict__.get('__repr__')): + if cls is not object and callable(getattr(cls, '__repr__', None)): return _repr_pprint(obj, self, cycle) return _default_pprint(obj, self, cycle)