Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bswck opened this issue Jan 28, 2025 · 2 comments
Open

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

bswck opened this issue Jan 28, 2025 · 2 comments
Labels
type-feature A feature request or enhancement

Comments

@bswck
Copy link
Contributor

bswck commented Jan 28, 2025

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

@bswck bswck added the type-feature A feature request or enhancement label Jan 28, 2025
@bswck
Copy link
Contributor Author

bswck commented Jan 28, 2025

Now that could be backported, but I don't think it's worth the effort or even classifies.

@ericvsmith
Copy link
Member

Could you explain what problem this solves?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants