Declaration of properties, attributes and command type with typing hints
Closes #53 (closed) and #373 (closed)
This MR makes possible to declare type of properties, attributes and commands in high-API servers with type hint, if server executed with python >= 3.9. E.g.:
host: str = device_property()
or
@command
def set_and_check_voltage(self, voltage_to_set: float) -> float:
device.set_voltage(voltage_to_set)
return device.get_voltage()
For the attributes type hint can be done at assignment (like property), or in the read/write method (like command).
voltage: float = attribute()
current = attribute()
def read_current(self) -> float:
return 10
At any cases the prority to obtain type is:
- dtype kword
- assignment
- read method
- write method
To define SPECTRUM and IMAGE attributes/properties/commands one can use list[type]
or tuple[type]
. In case of tuple the dimensions can be also specified from type hint: tuple[type, type]
, or tuple[tuple[type, type], tuple[type, type]]
Status of what done:
-
static attributes -
dynamic attributes -
static commands -
dynamic commands -
properties -
documentation
Changes in the code:
- Properties:
dtype kword made optional with default value None and later. At the server creation if dtype is None we try to pick it from the Class.__annotations__
- Attributes:
Added 2 additional filed to AttrData: has_dtype_kword, has_size_kword, which are False by default, and they set to True if users specify type and size in kwords (default behavior, as it was before). At the server creation if has_dtype_kword is False we try to pick it from the
Class.__annotations__
and then fromread/write_method.__annotations__
.
In case of dynamic attribute, we try to get info during methods patching. Due to we cannot do Class.__annotations__
, since attributes arr defined within the function and their assignment hints are not saved, so we can pick only from methods.
I also had to modify slightly the logic of AttrData class in a way, that self.attr_args
are now created at the last moment - since we post-updating dtype and format after init of AttrData
-
We just look to the annotation of function. Again, with the priority to the standard kwords.
-
There is additional
parce_type_hint
method in utils.py, that converts type hint to "tango format" of dtype -
tests of all possible combinations were added
-
Tried to explain everything in docs.