python-attrs / python-attrs/attrs

Inherit annotation type from base class that defines new default value

Open
#946 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Consider this code:

import attr


@attr.s(auto_attribs=True, kw_only=True)
class A:
    foo: str


@attr.s(auto_attribs=True, kw_only=True)
class B(A):
    foo = "bar"


b = B()

When compiled, this fails with:

TypeError: __init__() missing 1 required keyword-only argument: 'foo'

Yes, in this case, it would not be trouble to re-annotate "foo" as "foo: str" in the child class as well - but our classes use heavy/multi-level annotations like:

@attr.s(auto_attribs=True, kw_only=True)
class A:
    foo: List[Union[Need, Job, Sequence]]
    bar: List[Union[Job, Sequence]]
    baz: Union[bool, str, int, List[int]]
    .
    .
    .

We have dozens of such required parameters - it would be a great hassle to keep re-annotating them in every child class that provides a new default value. It would also be almost impossible to keep track of all re-annotations, as the number of child classes is growing every day. The whole idea of using attrs was to avoid having to re-annotate the arguments in every child class. Is it possible to achieve with the current framework? If not, can this be implemented?

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

No files or tests are named in the issue. Start by reproducing the A/B example and inspect how attrs processes inherited annotations and defaults; done means B() succeeds while retaining the inherited annotation and applying the child default.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.