-
-
Notifications
You must be signed in to change notification settings - Fork 374
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
MRO vs. inherited attributes #428
Comments
Hm that’s unfortunate and fixing it likely means breakage. :| |
So this is interesting. The underlying issue is that As such, I was able to make your test case work by ignoring inherited attributes iff D is also decorated with There's another problem too: users expect the attributes to be ordered (in @attr.s
class C(object):
c = attr.ib(default=100)
x = attr.ib(default=1)
b = attr.ib(default=23)
@attr.s
class D(C):
a = attr.ib(default=42)
x = attr.ib(default=2)
d = attr.ib(default=3.14) The attribute order is c, b, a, x, d (N.B. that x changed position due to overwrite). If I ignore inherited attributes and walk the mro, the order becomes a, x, d, c, b. I'm open to suggestions how to resolve this to make everyone happy but I banged my head against it for over an hour and didn't really come up with anything viable. :( I guess traversing it twice would work? Once for getting the order of the names right and then again to resolve them to the correct classes? 🤔 |
If you don't mind, have a look at #635 whether it covers everything you'd expect. |
I have merged it now, please let me know if something is not OK. |
The way how attributes are collected from super classes is different comparing to how attributes and methods are searched by
getattr
(based on MRO for new-style classes).Example:
I think it would be great to the use the same approach for collecting attributes as used for searching methods and attributes.
The difference is caused by the fact that the function
_transform_attrs
inattrs/_make.py
considers all attributes (own+inherited stored in__attrs_attrs__
) and not only own attributes of super classes:attrs/src/attr/_make.py
Lines 367 to 368 in 6a07b03
The text was updated successfully, but these errors were encountered: