From b790b6a6ca74979ed22c8d14791c7a1ea03b08e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Wed, 25 Oct 2023 10:47:15 +0200 Subject: [PATCH 1/3] fix: adds ColouredEnum to STANDARD_TYPES --- src/pydase/utils/helpers.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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]: From 9054f05f3058a0db00cf84a0e9f79fb09ed838c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Wed, 25 Oct 2023 16:15:19 +0200 Subject: [PATCH 2/3] fix: convert quantity dict to quantity when loading from json --- src/pydase/data_service/data_service.py | 4 ++++ 1 file changed, 4 insertions(+) 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( From 6ea4cf3eb7e34bc767416b577283472ce01eb6c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Wed, 25 Oct 2023 16:15:31 +0200 Subject: [PATCH 3/3] adds test for loading units from json --- tests/test_units.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) 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