You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Confusing error about __attrs_attrs__ when using multiple inheritance #12065
importattr@attr.sclassA:
x=attr.ib(type=int)
@attr.sclassB:
y=attr.ib(type=int)
# Definition of "__attrs_attrs__" in base class "A" is incompatible with definition in base class "B"classAB(A, B):
pass
The code is arguably questionable, and the error goes away if we decorate AB with @attr.s, but that's not clear from the error message. It would be better to add a note, such as something like this:
... :note: Should class "AB" use the @attr.s decorator?
This started happening after #11794, and it might be considered a minor regression because of the confusing message.
(Behind the scenes, a __attrs_attrs__ attribute gets generated with a class-specific named tuple as the type. These named tuples aren't compatible with each other.)
Yeah, the note approach seems fine. If you don't use @attr.s in the derived class, the inherited __init__ doesn't initialize all the attributes, the value of __attrs_attrs__ won't include all attributes, etc.
Activity
Tinche commentedon Jan 25, 2022
Interesting, I can take a look at this.
Tinche commentedon Jan 25, 2022
In runtime,
So I'm guessing it just follows the MRO and gets the class attribute from A.
Here's the equivalent situation without attrs:
This is a Mypy error (
error: Definition of "_test_" in base class "C" is incompatible with definition in base class "D").So if we want to keep this behavior and still raise an error, the note approach is the way to go?
JukkaL commentedon Jan 25, 2022
Yeah, the note approach seems fine. If you don't use
@attr.sin the derived class, the inherited__init__doesn't initialize all the attributes, the value of__attrs_attrs__won't include all attributes, etc.JelleZijlstra commentedon Jan 25, 2022
This feels similar to #12008. (Does attrs autocreate
__match_args__?)Tinche commentedon Jan 25, 2022
It does.
Fix multiple inheritance false positives in dataclasses and attrs cla…
Fix inheritance false positives with dataclasses/attrs (#12411)
Fix inheritance false positives with dataclasses/attrs (#12411)