-
Notifications
You must be signed in to change notification settings - Fork 111
Description
Hi, came across a potential bug that when writing to nested structures it only works when the nested structures is an array of structures. I modified the test_bytes_from_dict test to give an example. The test fails due to KeyError:
structure_def = (
('iVar9', pyads.PLCTYPE_USINT, 1),
('structVar', substructure_def, 1),
)
values = OrderedDict(
[
("iVar9", 29),
("structVar", subvalues)
]
)
# fmt: off
bytes_list = [29] + subbytes_list
# fmt: on
self.assertEqual(bytes_list, pyads.bytes_from_dict(values, structure_def))
This throws an error:
tests/test_ads.py", line 848, in test_bytes_from_dict
self.assertEqual(bytes_list, pyads.bytes_from_dict(values, structure_def))
pyads/pyads/ads.py", line 441, in bytes_from_dict
values=var[i], structure_def=plc_datatype
~~~^^^
KeyError: 0
Error comes from the fact that var can be of type OrderedDict and not of type list as is expected:
elif type(plc_datatype) is tuple:
bytecount = bytes_from_dict(
values=var[i], structure_def=plc_datatype
)
byte_list += bytecount
I would think this is not expected behavior as this forces the user to wrap the nested structures inside list comprehension if they are not array types. My own use case was to be able to do a read_structure_by_name (this contains nested structures) -> modify a value from this structure -> Write the modified structure back into the PLC using write_structure_by_name. Due to this error I am unable to do it without modifying the type of the nested structure to be a list. Maybe it would be good to check in bytes_from_dict that the var is actually a list?