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

Draft: fix: read only dict is not picking up changes #188

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/pydase/data_service/data_service_observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ def _update_cache_value(
)

def _notify_dependent_property_changes(self, changed_attr_path: str) -> None:
normalized_attr_path = normalize_full_access_path_string(changed_attr_path)
changed_props = self.property_deps_dict.get(normalized_attr_path, [])
changed_props = self._get_changed_props_from_changed_attr_path(
changed_attr_path
)
for prop in changed_props:
# only notify about changing attribute if it is not currently being
# "changed" e.g. when calling the getter of a property within another
Expand All @@ -114,6 +115,17 @@ def _notify_dependent_property_changes(self, changed_attr_path: str) -> None:
get_object_attr_from_path(self.observable, prop),
)

def _get_changed_props_from_changed_attr_path(
self, changed_attr_path: str
) -> list[str]:
changed_props: list[str] = []
normalized_attr_path = normalize_full_access_path_string(changed_attr_path)
for key, value in self.property_deps_dict.items():
if normalized_attr_path.startswith(key):
changed_props.extend(value)

return changed_props

def add_notification_callback(
self, callback: Callable[[str, Any, SerializedObject], None]
) -> None:
Expand Down
19 changes: 19 additions & 0 deletions tests/data_service/test_data_service_observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,22 @@ def __init__(self) -> None:
)
assert service_instance.service_dict["one"].prop == 12.0
assert "'service_dict[\"one\"].prop' changed to '12.0'" in caplog.text


def test_read_only_dict_property(caplog: pytest.LogCaptureFixture) -> None:
class MyObservable(pydase.DataService):
def __init__(self) -> None:
super().__init__()
self._dict_attr = {"dotted.key": 1.0}

@property
def dict_attr(self) -> dict[str, Any]:
return self._dict_attr

service_instance = MyObservable()
state_manager = StateManager(service=service_instance)
DataServiceObserver(state_manager)

service_instance._dict_attr["dotted.key"] = 2.0

assert "'dict_attr[\"dotted.key\"]' changed to '2.0'" in caplog.text
Loading