python-attrs / python-attrs/attrs
Use field *repr* callable to dynamically define repr based on instance value
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.8k
- Forks
- 480
- Avg merge
- 2h 15m
- Merged PRs (30d)
- 2
Description
@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?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the documentation for attrs.field's repr parameter and the behavior described in this issue. Determine what context the repr callable receives and whether it can distinguish default from instance values; done means documenting the available context or clarifying the supported approach.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- developer-experience
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100