-
-
Notifications
You must be signed in to change notification settings - Fork 385
Allow repr (+ str?) to take callables #212
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
Comments
See #224 (comment) for a patch to support a callable with |
This is an interesting idea. I think our repr support can be improved in any case; we're doing more work in the method that is generally needed, so it can be sped up by eval-ing a tailored function. Part of this work can be changing the |
Another use case where this is useful are numbers which you'd like to show in hexadecimal ( |
Another use case I have is when you have a list attribute and you'd like Would be happy to know if there is another way to do it. |
I've created a small extension which provides callable reprs and allows titles to be overridden too (e.g. for the length case cited above): attrs_flexible_reprs.py Input/feedback welcomed. |
@gimbo I think your flexible repr doesn’t take into account our recursion handler? Re earlier comments, I wouldn’t make this issue depend on transforming our repr generator to code generation. It would be nice to have, but not required for fixing this particular issue. |
@hynek I'm sure you're right - it's very simplistic, just the simplest thing I came up with that scratched the itch I had. I haven't delved into the code of |
Glyph has recently taught our repr to detect loops in definitions so it doesn’t run into max recursion error. You'd have to look at the current code and shoehorn the callable thing into it. |
Took only two years!!!1! |
@hynek |
It would be useful to be able to pass in a callable to do
repr
'ing of attributes.For me this happens if I want to present a value in the repr differently than just
repr(foo)
, which is sometimes useful, especially if the "different way" is to use e.g.reprlib.repr
to do auto-truncation of long values.Being able to pass in callables to the
repr
argument seems at first glance like a reasonably easy way to do that.E.g.
attr.ib(repr=reprlib.repr)
and
attr.ib(repr=lambda value: repr(value) * 12)
You could preserve backwards compatibility by having True be equivalent to
repr
, and False be a sentinel (which you need anyways).I think this could also satisfy #41, if you do the same to
attr.s
, and then provide anattr.simple_repr
, so you'd doattr.s(repr=attr.simple_repr)
and get the normal bracket repr instead of the eval one.The text was updated successfully, but these errors were encountered: