Skip to content

attr.s-level on_setattr is removed on inheritance #717

@Drino

Description

@Drino

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions