Skip to content

Inherit annotation type from base class that defines new default value #946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
R0ll1ngSt0ne opened this issue Apr 4, 2022 · 1 comment

Comments

@R0ll1ngSt0ne
Copy link

R0ll1ngSt0ne commented Apr 4, 2022

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 R0ll1ngSt0ne changed the title Inherit annotation type from base class Inherit annotation type from base class that defines new default value Apr 4, 2022
@hynek
Copy link
Member

hynek commented Apr 5, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants