python-attrs / python-attrs/attrs

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

Open
#1,193 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Feature
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.