Skip to content

display does not honor custom __repr__ for NamedTuple when defined by partialmethod #14465

@randolf-scholz

Description

@randolf-scholz

The issue occurs when the custom __repr__ is defined via partialmethod:

from functools import partialmethod
from typing import NamedTuple

def my_pprint(obj, **options):
    return "Hello World"

def add_custom_pprint(cls=None, **options):
    if cls is None:
        def decorator(cls):
            return add_custom_pprint(cls, **options)
        return decorator
    cls.__repr__ = partialmethod(my_pprint, **options)
    return cls

@add_custom_pprint
class MyNamedTuple(NamedTuple):
    x: int
    y: int

x = MyNamedTuple(1, 2)
print(x)  # "Hello World"
display(x)  # (1, 2)
without partialmethod, both display and print give the same result:
from typing import NamedTuple

def my_pprint(obj, **options):
    return "Hello World"

def add_custom_pprint(cls=None, **options):
    if cls is None:
        def decorator(cls):
            return add_custom_pprint(cls, **options)
        return decorator
    cls.__repr__ = my_pprint
    return cls

@add_custom_pprint
class MyNamedTuple(NamedTuple):
    x: int
    y: int

x = MyNamedTuple(1, 2)
print(x)  # "Hello World"
display(x)   # "Hello World"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions