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

alternative repr that does not look like it can be evaluated #41

Open
wbolster opened this issue May 5, 2016 · 8 comments
Open

alternative repr that does not look like it can be evaluated #41

wbolster opened this issue May 5, 2016 · 8 comments
Labels

Comments

@wbolster
Copy link
Member

wbolster commented May 5, 2016

the repr() generated by attrs looks something like this by default:

MyClass(a=1, b=2)

this is fine in simple cases. however, when using init=False, and implementing my own __init__(), this repr() is misleading since it looks like code that can be evaluated, while actually it is not (and if it is, it's not an intended usage pattern anyway). what about having something like this instead for those cases:

<MyClass a=1 b=2>

...which is still very useful (e.g. for debugging), and it resembles a bit what python produces for classes that do not implement a custom __repr__():

>>> x = object()
>>> x
<object object at 0x7f6cda0c45e0>

obviously, this custom repr() behaviour would require explicit enabling. a flag to attr.s(), e.g. attr.s(use_eval_repr=False) (please come up with a better name), would make sense to me.

what do you think?

@wbolster
Copy link
Member Author

wbolster commented May 5, 2016

btw, i can take a stab at a pr if you think this feature is worth adding. is seems that passing a flag to _add_repr() and using a slightly different format string depending on that flag is pretty much all that is needed.

@hynek
Copy link
Member

hynek commented May 6, 2016

I agree but I’m not sure if it’s worth the complexity and confusion. Just because the __init__ doesn’t come from us, doesn’t mean it’s not evaluable and users might get confused by the changing behavior.

@wbolster
Copy link
Member Author

wbolster commented May 6, 2016

that's why i suggested to have another flag to enable this "custom repr". it would be an independent flag (not dependent on init=False in any way) so that it's just an additional opt-in feature.

@hynek hynek added the Feature label Aug 21, 2016
@Julian
Copy link
Member

Julian commented Jun 15, 2017

I think I have an alternate solution that might satisfy this too. Adding some thoughts to a new issue (will link it in a sec).

@hynek
Copy link
Member

hynek commented May 14, 2020

Solved by #568!

@hynek hynek closed this as completed May 14, 2020
@wbolster
Copy link
Member Author

wbolster commented May 14, 2020

not sure that's correct, @hynek. i opened this ticket about custom repr for classes, but #568 (also written by me) is about attribute-level custom repr.

see the examples in the description of this issue; i don't think that can be achieved with #568.

(tentatively reopening the ticket, feel free to close if things turn out to be different from what i wrote.)

@wbolster wbolster reopened this May 14, 2020
@Julian
Copy link
Member

Julian commented May 14, 2020

IIRC this ticket was about "Make attr.s(repr=, rather than attr.ib(repr= which is #568, also take a callable which collects and assembles the final one (and then secondly possibly implement a concrete callable implementation for bracket-looking-repr or soemthing).

@wbolster
Copy link
Member Author

wbolster commented May 14, 2020

exactly, @Julian. in #568 i added a repr=... with a custom callable for attr.ib().

in this (much older!) issue i didn't suggest a specific API (well, i did but said it wasn't good), but i agree using repr=... with a custom callable, but for attr.s() seems the logical choice.

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

3 participants