Merge pull request #51 from tiqi-group/49-wrong-types-for-non-standard-saved-variables

fix: loading of ColouredEnum and Quantity from settings file
This commit is contained in:
Mose Müller 2023-10-25 16:16:43 +02:00 committed by GitHub
commit 2713dad423
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 1 deletions

View File

@ -185,6 +185,10 @@ class DataService(rpyc.Service, AbstractDataService):
parts = path.split(".") parts = path.split(".")
attr_name = parts[-1] 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) self.update_DataService_attribute(parts[:-1], attr_name, value)
else: else:
logger.info( logger.info(

View File

@ -5,7 +5,16 @@ from typing import Any, Optional, cast
logger = logging.getLogger(__name__) 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]: def get_class_and_instance_attributes(obj: object) -> dict[str, Any]:

View File

@ -124,3 +124,44 @@ def test_autoconvert_offset_to_baseunit() -> None:
quantity = 10 * u.units.degC quantity = 10 * u.units.degC
except pint.errors.OffsetUnitCalculusError as exc: except pint.errors.OffsetUnitCalculusError as exc:
assert False, f"Offset unit raises exception {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