fixes warnings tests

This commit is contained in:
Mose Müller 2023-12-05 11:20:03 +01:00
parent 1e55a4d914
commit 75ee71cbf8
3 changed files with 27 additions and 4 deletions

View File

@ -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)

View File

@ -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__,
)

View File

@ -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: