attrs icon indicating copy to clipboard operation
attrs copied to clipboard

Inherit annotation type from base class that defines new default value

Open R0ll1ngSt0ne opened this issue 2 years ago • 1 comments

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?

R0ll1ngSt0ne avatar Apr 04 '22 13:04 R0ll1ngSt0ne

I don't think this can be implemented in a way that doesn't introduce a load of edge cases and surprising behavior.

I suspect this is more another case for #637.

hynek avatar Apr 05 '22 13:04 hynek