Support abstract functionality for attrs classes

Open
#976 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
python

Research direction

Start from the requested @abstract and @override behavior in the issue's Parent/Child example, then investigate how attrs currently handles generated classes and inheritance. Done would require a decided, documented design for rejecting abstract instantiation and invalid overrides, along with corresponding tests.

Written by the indexing model from the issue text.

Description

I'd love to do something like:

@attr.s(auto_attribs=True, frozen=True)
class Parent:
    @abstract
    param: str

@attr.s(auto_attribs=True, frozen=True)
class Child(Parent):
    @override
    param = "test"

where you wouldn't be able to create an instance of a class with @abstract decorators, and you wouldn't be able to put the @override decorator on something that wasn't defined in the parent.

I commonly use attrs classes to hold configuration parameters, and allowing this functionality would make it much safer and more readable. Currently it's too easy to accidentally think you're overriding something in Child that's actually not defined in Parent (typo, got removed, etc) - and it's not clear when reading Child whether an attribute is new to the child or just overriding a default value from the parent. And leaving a value without a default in Parent is a little annoying because you have to put all the attrs without default first (which is not a big deal, but disrupts the logical grouping of members of the class).

I have no idea if this is remotely possible, so feel free to close if not!

Dominant language
Python
Stars
5.8k
Forks
480
Avg merge
2h 15m
Merged PRs (30d)
2

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.

More from python-attrs/attrs

All issues in python-attrs/attrs

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.