Skip to content

Attribute changed notification hooks #622

@danieljfarrell

Description

@danieljfarrell

I'm interested in extending attrs to provide change notifications. Or at least making my classes generated with attrs emit notifications when an attribute changes.

How would you go about doing this?

I'm wondering if attrs could generate an 'x'_did_change method/notification hook when synthesizing the attributes. This is similar to traitlets trait change notifications/observations although that's more dynamic as observers can be added and removed at runtime.

import attr
import typing

@attr.s(auto_attribs=True)
class SomeClass:
    a_number: int = 42
    list_of_numbers: typing.List[int] = attr.Factory(list)

    def _a_number_did_change(self, info):
        old = info['old']
        new = info['new']

    def _list_of_numbers_did_change(self, info):
        added = info['added']
        removed = info['removed']

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions