My problem, I am using attrs to make a dataclass with a name attribute.
I want to modify the name before setting it, but I cannot use attrs.converters because they are
not bound to the instance. So I create a private _name field and use a name property to validate the name
before setting it.
Now I would like to emit a signal when name is changed. This is my working example using PR #260.
Now the signal is emitted at self.events._name_changed but I would like it to be self.events.name_changed or self.events.name, i.e. without the "_" prefix because then I will add other signals like self.events.color and I want it to be consistent, so all without the "_" prefix.
Any idea how I can do that without another PR :) ?
from typing import ClassVar
from attrs import define, field
from psygnal import SignalGroupDescriptor, EmissionInfo
@define
class Model:
events: ClassVar[SignalGroupDescriptor] = SignalGroupDescriptor(signal_suffix="_changed")
_controller: str = field()
_name: str = field(kw_only=True)
@property
def name(self) -> str:
return self._name
@name.setter
def name(self, value: str) -> None:
# Generate a unique name among siblings
siblings = [self.controller]
value = f"{value}_1" if value in siblings else value
self._name = value
@property
def controller(self) -> str:
return self._controller
m = Model("ctt", name="c")
@m.events.connect
def on_any_change(info: EmissionInfo):
print(f"field {info.signal.name!r} changed to {info.args}")
m.name = "ctt"; m.name
# >> field '_name_changed' changed to ('ctt_1', 'c')
# I would like it to be 'name_changed'
My problem, I am using
attrsto make a dataclass with anameattribute.I want to modify the
namebefore setting it, but I cannot useattrs.convertersbecause they arenot bound to the instance. So I create a private
_namefield and use anameproperty to validate the namebefore setting it.
Now I would like to emit a signal when
nameis changed. This is my working example using PR #260.Now the signal is emitted at
self.events._name_changedbut I would like it to beself.events.name_changedorself.events.name, i.e. without the "_" prefix because then I will add other signals likeself.events.colorand I want it to be consistent, so all without the "_" prefix.Any idea how I can do that without another PR :) ?