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

Use field *repr* callable to dynamically define repr based on instance value #1193

Open
RNKuhns opened this issue Oct 18, 2023 · 4 comments
Open
Labels

Comments

@RNKuhns
Copy link

RNKuhns commented Oct 18, 2023

@hynek thanks for the work on attrs!

Ideally, I'd only like a field to appear in a class' repr if that field is set to a non-default value. An example of this approach is scikit-learn's approach to printing class representations.

I'm trying to understand if this is already possible currently in attrs. I know that attrs accepts a callable for the repr parameter of an attrs.field. But reading through the documentation, it is not clear what context (if any) is available to the repr. If the class' default values and the actual value of the parameter are as context to the callable, it would be relatively straightforward to craft a returned string that accomplished this.

Can you provide some info on what's available to the callable so I can understand if this is possible?

@hynek
Copy link
Member

hynek commented Oct 21, 2023

Unfortunately, what you want it is currently not possible. The repr function only receives the value that's being repr-ed and the return value is added to the repr unconditionally:

attrs/src/attr/_make.py

Lines 1874 to 1878 in c849440

fragment = (
"%s={%s!r}" % (name, accessor)
if r == repr
else "%s={%s_repr(%s)}" % (name, name, accessor)
)

@hynek hynek added the Feature label Oct 21, 2023
@RNKuhns
Copy link
Author

RNKuhns commented Feb 7, 2024

Unfortunately, what you want it is currently not possible. The repr function only receives the value that's being repr-ed and the return value is added to the repr unconditionally:

attrs/src/attr/_make.py

Lines 1874 to 1878 in c849440

fragment = (
"%s={%s!r}" % (name, accessor)
if r == repr
else "%s={%s_repr(%s)}" % (name, name, accessor)
)

@hynek Is this something you'd be interesting in having added. If so, I can look into the codebase and see if it falls into something I could make a PR for.

@hynek
Copy link
Member

hynek commented Feb 26, 2024

If someone came up with a good PR, I would merge it.

But I would strongly suggest to discuss your implementation approach with us first, before you put work into this that doesn't have a chance to be merged!

@RNKuhns
Copy link
Author

RNKuhns commented Feb 27, 2024

@hynek that sounds reasonable to me.

I've been looking at the code in attrs/src/attr/_make.py.

I'm going to do a bit more investigation on where adding the repr fits into the order of things.

I'll make a proposal if something seems workable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants