python-attrs / python-attrs/attrs

Generalizing ad-hoc attribute callbacks

Open
#146 18 comments 2 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

While 37d38c4 is pretty cool, when working on it it was very obvious to me, that it should be united with converter. After @Tinche pointed it out too, there’s no way back.

IOW, something like this:

@attr.s
class C:
    x = attr.ib()

   @x.callback_but_a_better_name:
   def take_number_make_it_a_hex_string(self, attribute, value):
       if not isinstance(value, int):
           raise TypeError(f"{attribute.name} must be an int")
       return hex(value)

Any other ideas?

The downside that it breaks the simplicity of the current implementation and we’d have to treat those callbacks in a special way (instead of just like any other validator).


Thinking of it, it might make sense to make it a different feature altogether (i.e. having a lightweight validator and something more substantial) because requiring to return the new value is a source for errors…in any case it mustn’t be called validator because the semantics would be different.

I’d tend to say that people can just do a self.x = hex(value) but that breaks frozen classes and we have that problem already in #120.

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

Review the existing converter and validator behavior, then read the discussion around frozen classes in issue #120. The issue needs a settled API and semantics for callbacks that transform values; completion should include an agreed design and corresponding implementation scope.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.