Skip to content
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 group descriptor in nested pydantic models #343

Closed
jacopoabramo opened this issue Dec 2, 2024 · 2 comments
Closed

Adding group descriptor in nested pydantic models #343

jacopoabramo opened this issue Dec 2, 2024 · 2 comments

Comments

@jacopoabramo
Copy link

jacopoabramo commented Dec 2, 2024

  • psygnal version: 0.11.1
  • Python version: >= 3.9
  • Operating System: Windows

Description

Not a bug per se but a curiosity. I'm currently using pydantic to build some nested models in order to give the user the possibility to add extra configurations. At the same time I want to render these models evented by adding the SignalGroupDescriptor.

What I Did

For type hinting purposes I did the following:

from psygnal import EmissionInfo, SignalGroupDescriptor
from pydantic import BaseModel

from typing import ClassVar

class MyModel(BaseModel):
    message: str
    events: ClassVar[SignalGroupDescriptor] = SignalGroupDescriptor()
    
class MyChildModel(MyModel):
    child_message: str
    events: ClassVar[SignalGroupDescriptor] = SignalGroupDescriptor()


class ModelWrapper:
    
    def __init__(self, model: MyChildModel) -> None:
        self.model = model
        self.model.events.connect(self.on_event)
    
    def on_event(self, info: EmissionInfo):
        print(f"Signal {info.signal.name} received with data {info.args}")

child = MyChildModel(message="Hello", child_message="World")

wrapper = ModelWrapper(child)
wrapper.model.message = "Goodbye"
wrapper.model.child_message = "Universe"

It seems to work. Then I realized: what happens if I remove events from the child model? Apparently, if you comment it out, it still works.

My ignorance in the matter is obvious, so I'm just checking in if this behavior may cause issues in the future or if it's an acceptable usage.

EDIT: the idea is that these models are singletons, a.k.a. they're created only once by the application and the user just configures a new model and passes it to the application which builds it, the constructor can't and shouldn't be called by the users themselves.

@tlambert03
Copy link
Member

tlambert03 commented Dec 2, 2024

definitely an acceptable usage, and more or less the same general idea that I'm trying to automate in #298 (in terms of finding and connecting child events without the need for the __init__)

the general pattern of connecting a signal emission to a re-emission of another signal is fully supported

@jacopoabramo
Copy link
Author

Alright, thanks for the confirmation. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants