-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding additional signals (e.g. for computed_field) #337
Comments
so @sjdemartini, am I correct that the gist of this is essentially that you'd just like to have a mechanism to add signals in addition to those derived from fields? or is it more specifically just that I can imagine a few solutions:
I don't love any of these, since they all feel slightly unexpected to me. i might be happiest with number 2, but only if you @sjdemartini also feel like it's a satisfying and natural solution. Would you perhaps be able to give me a quick toy example, expanding on your PrivateAttr example, that shows how exactly you might emit the signal? |
tldr: "custom" events with approaches like (1), (2), or (3) seem unnecessary if:
@tlambert03 It's more the latter. I don't have a need to add miscellaneous custom events currently, but do have Here's a toy example which perhaps illustrates the use-case: from psygnal import EventedModel
from pydantic import computed_field, PrivateAttr
class MyModel(EventedModel):
_items_dict: dict[str, int] = PrivateAttr(default_factory=dict)
@computed_field
@property
def item_names(self) -> list[int]:
return list(self._items_dict.keys())
@computed_field
@property
def item_sum(self) -> int:
return sum(self._items_dict.values())
def add_item(self, name: str, value: int) -> None:
if name in self._items_dict:
raise ValueError(f"Name {name} already exists!")
# Do other validation here...
self._items_dict = {**self._items_dict, name: value}
# --> Emit change events here manually, if Psygnal can't handle it automatically
# This could alternatively use the following instead for better perf, though the above could work more
# "automatically" for psygnal event emission if PrivateAttr were supported as a computed dependency:
# self._items_dict[name] = value
# Ideally the following would work
class Config:
field_dependencies = {
"item_names": ["_items_dict"],
"item_sum": ["_items_dict"],
} In my case, I'm serializing my Pydantic model for a web client, and send updates to the model based on Psygnal events (via Websocket). I want the client to see/utilize the As with your last example you gave here #334 (comment), it would be great to set up configuration like This may be a separate discussion, but is there a specific reason that For what it's worth, my current workaround has been to define custom signals via from typing import ClassVar
from psygnal import EventedModel, Signal
from pydantic import ConfigDict, PrivateAttr, computed_field, field_serializer
class MyModel(EventedModel):
item_names_changed: ClassVar[Signal] = Signal(list[str])
...
def add_item(self, name: str, value: int) -> None:
...
self.item_names_changed.emit(self.item_names) The consumer then needs to subscribe to both (
I don't have a deep enough familiarity with the APIs for |
from @sjdemartini in #334:
see also, discussion starting here: #334 (comment)
The text was updated successfully, but these errors were encountered: