Skip to content

Decorate namedtuple's default repr with @reprlib.recursive_repr #129387

@bswck

Description

@bswck

Feature or enhancement

Proposal:

For

from __future__ import annotations

from typing import NamedTuple


class Biz(NamedTuple):
    our_final_destination: list[Foo]


class Bar(NamedTuple):
    even_longer_attribute_name_presumably: Biz


class Foo(NamedTuple):
    very_long_attribute_name: Bar


foo = Foo(Bar(Biz(Bar(dest := []))))
dest.append(foo)

print(foo)

Before decorating:

Foo(very_long_attribute_name=Bar(even_longer_attribute_name_presumably=Biz(our_final_destination=Bar(even_longer_attribute_name_presumably=[Foo(very_long_attribute_name=Bar(even_longer_attribute_name_presumably=Biz(our_final_destination=Bar(even_longer_attribute_name_presumably=[...]))))]))))

after decorating:

Foo(very_long_attribute_name=Bar(even_longer_attribute_name_presumably=Biz(our_final_destination=Bar(even_longer_attribute_name_presumably=[...]))))

(Note that if our_final_destination pointed to Foo, e.g. via a mutable object that didn't have recursive repr support but included foo in its repr, printing foo would fail with a recursion limit excess. It's not a bug of CPython by any means though.)

The decorator is already available in the module as _recursive_repr , so it's just a matter of application.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

Labels

pendingThe issue will be closed if no feedback is providedtype-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions