attrs icon indicating copy to clipboard operation
attrs copied to clipboard

`attr.s`-level `on_setattr` is removed on inheritance

Open Drino opened this issue 4 years ago • 3 comments

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?

Drino avatar Nov 13 '20 14:11 Drino

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?

hynek avatar Dec 13 '20 16:12 hynek

Got it now, thank you for the answer :)

Drino avatar Dec 20 '20 11:12 Drino

Any action left here?

wsanchez avatar Jan 28 '21 19:01 wsanchez