diff --git a/src/pydase/data_service/data_service.py b/src/pydase/data_service/data_service.py index 3c4a0c3..97c4678 100644 --- a/src/pydase/data_service/data_service.py +++ b/src/pydase/data_service/data_service.py @@ -185,6 +185,10 @@ class DataService(rpyc.Service, AbstractDataService): parts = path.split(".") attr_name = parts[-1] + # Convert dictionary into Quantity + if class_value_type == "Quantity": + value = u.convert_to_quantity(value) + self.update_DataService_attribute(parts[:-1], attr_name, value) else: logger.info( diff --git a/src/pydase/utils/helpers.py b/src/pydase/utils/helpers.py index c9a7aca..8d2d053 100644 --- a/src/pydase/utils/helpers.py +++ b/src/pydase/utils/helpers.py @@ -5,7 +5,16 @@ from typing import Any, Optional, cast logger = logging.getLogger(__name__) -STANDARD_TYPES = ("int", "float", "bool", "str", "Enum", "NoneType", "Quantity") +STANDARD_TYPES = ( + "int", + "float", + "bool", + "str", + "Enum", + "NoneType", + "Quantity", + "ColouredEnum", +) def get_class_and_instance_attributes(obj: object) -> dict[str, Any]: diff --git a/tests/test_units.py b/tests/test_units.py index 55b6aa3..9d28cb9 100644 --- a/tests/test_units.py +++ b/tests/test_units.py @@ -124,3 +124,44 @@ def test_autoconvert_offset_to_baseunit() -> None: quantity = 10 * u.units.degC except pint.errors.OffsetUnitCalculusError as exc: assert False, f"Offset unit raises exception {exc}" + + +def test_loading_from_json(capsys: CaptureFixture) -> None: + """This function tests if the quantity read from the json description is actually + passed as a quantity to the property setter.""" + JSON_DICT = { + "some_unit": { + "type": "Quantity", + "value": {"magnitude": 10.0, "unit": "A"}, + "readonly": False, + "doc": None, + } + } + + class ServiceClass(DataService): + def __init__(self): + self._unit: u.Quantity = 1 * u.units.A + super().__init__() + + @property + def some_unit(self) -> u.Quantity: + return self._unit + + @some_unit.setter + def some_unit(self, value: u.Quantity) -> None: + assert isinstance(value, u.Quantity) + self._unit = value + + service = ServiceClass() + + service.load_DataService_from_JSON(JSON_DICT) + + captured = capsys.readouterr() + + expected_output = sorted( + [ + "ServiceClass.some_unit = 10.0 A", + ] + ) + actual_output = sorted(captured.out.strip().split("\n")) # type: ignore + assert actual_output == expected_output