diff --git a/src/pydase/data_service/data_service.py b/src/pydase/data_service/data_service.py index 7a7c2bd..2e6a587 100644 --- a/src/pydase/data_service/data_service.py +++ b/src/pydase/data_service/data_service.py @@ -70,6 +70,11 @@ class DataService(rpyc.Service, AbstractDataService): # Warn if setting private attributes self._warn_on_private_attr_set(__name) + # every class defined by the user should inherit from DataService if it is + # assigned to a public attribute + if not __name.startswith("_"): + warn_if_instance_class_does_not_inherit_from_data_service(__value) + # Set the attribute super().__setattr__(__name, __value) diff --git a/src/pydase/utils/warnings.py b/src/pydase/utils/warnings.py index 500be15..2e9cb30 100644 --- a/src/pydase/utils/warnings.py +++ b/src/pydase/utils/warnings.py @@ -22,6 +22,7 @@ def warn_if_instance_class_does_not_inherit_from_data_service(__value: object) - and type(__value).__name__ not in ["CallbackManager", "TaskManager", "Quantity"] ): logger.warning( - "Warning: Class '%s' does not inherit from DataService.", + "Class '%s' does not inherit from DataService. This may lead to unexpected " + "behaviour!", type(__value).__name__, ) diff --git a/tests/utils/test_warnings.py b/tests/utils/test_warnings.py index b251749..0abca37 100644 --- a/tests/utils/test_warnings.py +++ b/tests/utils/test_warnings.py @@ -2,8 +2,22 @@ from pydase import DataService from pytest import LogCaptureFixture -def test_setattr_warnings(caplog: LogCaptureFixture) -> None: - # def test_setattr_warnings(capsys: CaptureFixture) -> None: +def test_class_attr_inheritance_warning(caplog: LogCaptureFixture) -> None: + class SubClass: + name = "Hello" + + class ServiceClass(DataService): + attr_1 = SubClass() + + ServiceClass() + + assert ( + "Class 'SubClass' does not inherit from DataService. This may lead to " + "unexpected behaviour!" + ) in caplog.text + + +def test_instance_attr_inheritance_warning(caplog: LogCaptureFixture) -> None: class SubClass: name = "Hello" @@ -14,7 +28,10 @@ def test_setattr_warnings(caplog: LogCaptureFixture) -> None: ServiceClass() - assert "Warning: Class 'SubClass' does not inherit from DataService." in caplog.text + assert ( + "Class 'SubClass' does not inherit from DataService. This may lead to " + "unexpected behaviour!" + ) in caplog.text def test_private_attribute_warning(caplog: LogCaptureFixture) -> None: