ni / ni/measurement-plugin-python
Enum arrays do not work with an empty default value
@WesleyTangNationalInstruments is already working on this.
Since Sep 6, 2023.
- Dominant language
- Python
- Stars
- 21
- Forks
- 20
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 3
Description
Bug Report
Deserializing an enum array does not work when the configuration's default_value parameter is set to [] (empty list).
Repro or Code Sample
--- a/examples/sample_measurement/measurement.py
+++ b/examples/sample_measurement/measurement.py
@@ -55,6 +55,9 @@ class Color(Enum):
@sample_measurement_service.configuration(
"String Array In", nims.DataType.StringArray1D, ["String1", "String2"]
)
+@sample_measurement_service.configuration(
+ "Enum Array In", nims.DataType.EnumArray1D, [], enum_type=Color
+)
@sample_measurement_service.output("Float out", nims.DataType.Float)
@sample_measurement_service.output("Double Array out", nims.DataType.DoubleArray1D)
@sample_measurement_service.output("Bool out", nims.DataType.Boolean)
@@ -72,6 +75,7 @@ def measure(
enum_input,
protobuf_enum_input,
string_array_in,
+ enum_array_in,
):
"""Perform a loopback measurement with various data types."""
logging.info("Executing measurement")
@@ -81,6 +85,8 @@ def measure(
sample_measurement_service.context.add_cancel_callback(cancel_callback)
+ logging.info("enum_array_in: %r", enum_array_in)
+
float_output = float_input
float_array_output = double_array_input
bool_output = bool_input
Steps:
- Open MeasurementLink UI Editor.
- Drop a Numeric Array Input.
- Bind it to
Enum Array In. - Set the first element to 1.
- Run the measurement.
Expected Behavior
The measurement logs this message:
enum_array_in: [<Color.RED: 1>]
Current Behavior
The measurement produces this error:
PS D:\dev\measurementlink-python\examples\sample_measurement> poetry run python .\measurement.py -v
2023-09-01 17:48:26,593 INFO: Measurement service hosted on port: 52794
2023-09-01 17:48:27,123 INFO: Successfully registered with discovery service.
Press enter to close the measurement service.
2023-09-01 17:49:09,385 INFO: gRPC server call /ni.measurementlink.measurement.v2.MeasurementService/GetMetadata responded OK in 0.9639 ms
2023-09-01 17:50:07,890 INFO: gRPC server call /ni.measurementlink.measurement.v2.MeasurementService/Measure responded OK in 0.6051 ms
2023-09-01 17:50:07,890 ERROR: Exception iterating responses: 'int' object is not iterable
Traceback (most recent call last):
File "D:\dev\measurementlink-python\examples\sample_measurement\.venv\lib\site-packages\grpc\_server.py", line 589, in _take_response_from_response_iterator
return next(response_iterator), True
File "D:\dev\measurementlink-python\ni_measurementlink_service\_loggers.py", line 391, in __next__
response = next(self._inner_iterator)
File "D:\dev\measurementlink-python\ni_measurementlink_service\_internal\grpc_servicer.py", line 385, in Measure
mapping_by_id = serializer.deserialize_parameters(
File "D:\dev\measurementlink-python\ni_measurementlink_service\_internal\parameter\serializer.py", line 41, in deserialize_parameters
_deserialize_enum_parameters(parameter_metadata_dict, overlapping_parameter_by_id)
File "D:\dev\measurementlink-python\ni_measurementlink_service\_internal\parameter\serializer.py", line 231, in _deserialize_enum_parameters
parameter_by_id[id][index] = enum_type(member_value)
TypeError: 'int' object is not iterable
Workaround
Make the default value non-empty (e.g. [Color.BLUE]).
Possible Solution
I think we should save the passed-in enum_type and use it during deserialization, instead of using the type of the default value.
Inferring the enum type from the default value as described in https://github.com/ni/measurementlink-python/issues/364 could still work, but not when the default value is an empty list.
Context
https://github.com/ni/measurementlink-python/issues/364
Your Environment
- OS & Device: Windows
ni-measurementlink-serviceversion: 1.1.0, 1.2 main branch- MeasurementLink version: 2023 Q3 (23.5)
- Python version: 3.9.13
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.