Enhance flexibility of dataclass repr

The desire for this came up for me in relation to a set of dataclasses used to define a tree structure where each item has a reference to its parent. Including the complete expansion of the parent (with its children and its parent with its children, etc.) is WAY too much information, but at the same time, I do want to at least identify the parent. Currently, the only way to get what I'm looking for is to write a custom `__repr__` from scratch. It would be great if there was at least 1 way to take advantage of the automatic repr and still customize its handling for specific fields. The first thing I thought of in that regard for my example was to add a `parent_name` property using `@property` and specify `repr=False` for `parent. The auto-generated repr is not aware of properties defined that way though. Maybe that could be solved by adding an argument named something like `descriptor=<True|False>` to `field()` where a `True` value means that getting and setting happens through a separately defined descriptor (e.g. via`@property`) and should not be implemented automatically, even though it should be otherwise treated as a dataclass property. The second thought I had is to be able to customize `repr` for any field. One way to do that might be to allow `field()`'s `repr` argument to accept a method name string and/or a callable that accepts an instance of the class in addition to accepting `True` or `False`. I actually like the idea of having both of those capabilities. Opinions?

I should add that… I did find it is already possible to define a dataclass field for a property that is implemented as `@property`-decorated function, but it's a bit of a hack. It only works if the property has a setter that succeeds, even it the attribute is supposed to be read-only or if it is not appropriate for it's setter to be called during initialization.

I should add that… I did find it is already possible to define a dataclass field for a property that is implemented as `@property`-decorated function, but it's a bit of a hack. It only works if the property has a setter that succeeds, even it the attribute is supposed to be read-only or if it is not appropriate for it's setter to be called during initialization.
participants (1)
-
Steve Jorgensen