python-attrs / python-attrs/attrs

More convenient shorthand for derived attributes

Open
#310 7 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

Resolving #165 allowed setting the default for an attribute based on other attributes using a decorator like this:

@attrs(frozen=True, slots=True)
class Foo:
   bar = attr.ib()
   moo = attr.ib()

   @moo.default
   def _init_moo(self):
      return self.bar + 2

Many times in frozen classes we want to derive one attribute from another without giving the user the opportunity to override it. The Immutables library for Java provides for this via Value.Derived: http://immutables.github.io/immutable.html#derived-attributes . Perhaps attrs could have a @field.derived decorator which does the same thing?

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 by reading the existing @moo.default decorator behavior and how attrs handles frozen classes. Compare that with the proposed @field.derived entry point and the linked Immutables example. Done means a derived attribute can be calculated from other attributes without allowing the user to override it, with behavior and scope agreed for attrs.

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
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.