attrs
attrs copied to clipboard
`attr.s`-level `on_setattr` is removed on inheritance
Seems like attr.s
-level on_setattr
is applied to all attributes including inherited, but doesn't become a part of Attribute
definition (to save memory in sa_attrs
dictionary, as far as I understand):
import attr
@attr.s
class Parent:
a = attr.ib()
b = attr.ib(type=int, on_setattr=attr.setters.validate)
def print_fun(self, attr, value):
print(f'Setting {attr.name} to {value}')
return value
@attr.s(on_setatr=print_fun)
class Child(Parent):
c = attr.ib()
c = Child(1, 2, 3)
c.a = 100 # prints 'Setting a to 100'
print(attr.fields(Child).a.on_setattr) # prints None
@attr.s
class NextChild(Child):
pass
c = NextChild(1, 2, 3)
c.a = 100 # silently passes
So, class-level on_setattr
is totally removed on inheritance. Is it expected behaviour?
Yes, somewhat. At least I've implemented it separately in #663. I was thinking to have a special value for on_setattr
to either inherit or drop?
Got it now, thank you for the answer :)
Any action left here?