python-attrs / python-attrs/attrs

Combine multiple classes

Open
#1,392 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

Is it possible to combine two (or more) class declarations into one?
This should be similar to multiple inheritance I guess, but should work with slots.

For example:

@define
class Point2d:
    x : int
    y : int

@define
class Line:
    start : Point2d
    end : Point2d
    
@define
class StrokeProperties:
     size : float
     color : Color
     
DrawnPoint = some_magic(Point2d, StrokeProperties)
DrawnLine = some_magic(Line, StrokeProperties)

print(DrawnPoint.x, DrawnPoint.size)

One could argue, that composition should be used instead, but I think it could be nicer to be able to access attributes without nesting.

Is there something like this in attrs, or how it could be done using existing api?

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 reviewing attrs' existing class-composition and inheritance APIs, with particular attention to the slots requirement described in the issue. Determine whether the requested flattened attribute access is supported or needs a new design; done would be a documented existing approach or a clearly scoped feature proposal.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.