tiqi-group / pydase

A flexible and robust Python library for creating, managing, and interacting with data services, with built-in support for web and RPC servers, and customizable features for diverse use cases.
MIT License
0 stars 1 forks source link

Fix: dynamic list entry with property #102

Closed mosmuell closed 4 months ago

mosmuell commented 4 months ago

Dynamically adding a class instance containing a property to a list used to emit change notifications of this property although the list change wasn't emitted yet. So this code:

    class PropertyClass(pydase.DataService):
        _name = "Hello"

        @property
        def name(self) -> str:
            """The name property."""
            return self._name

    class MyService(pydase.DataService):
        def __init__(self) -> None:
            super().__init__()
            self.channels = []

        def toggle_high_voltage(self) -> None:
            self.channels = []
            self.channels.append(PropertyClass())
            self.channels[0]._name = "Hoooo"

would emit:

2024-02-20 12:23:30.065 | DEBUG    | pydase.data_service.data_service_observer:on_change:43 - 'channels[0].name' changed to 'Hello'
2024-02-20 12:23:30.065 | DEBUG    | pydase.data_service.data_service_observer:on_change:43 - 'channels' changed to '[<__main__.PropertyClass object at 0x7f3c2db6f350>]'
2024-02-20 12:23:30.065 | DEBUG    | pydase.data_service.data_service_observer:on_change:43 - 'channels[0].name' changed to 'Hello'
2024-02-20 12:23:30.066 | DEBUG    | pydase.data_service.data_service_observer:on_change:43 - 'channels[0].name' changed to 'Hello'
2024-02-20 12:23:30.066 | DEBUG    | pydase.data_service.data_service_observer:on_change:43 - 'channels[0].name' changed to 'Hoooo'

This is caused by the serialization of the object when it is compared to the cached_value. As the object has been added to the _ObservableList, it could already emit such a notification even though the 'channels' changed to '[<__main__.PropertyClass object at 0x7f3c2db6f350>]' notification hasn't yet been emitted.

This MR fixes this by