Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing to nested structure failure #440

Open
ArttuLinden opened this issue Dec 30, 2024 · 0 comments · May be fixed by #442
Open

Writing to nested structure failure #440

ArttuLinden opened this issue Dec 30, 2024 · 0 comments · May be fixed by #442

Comments

@ArttuLinden
Copy link

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?

msfur added a commit to msfur/pyads that referenced this issue Jan 13, 2025
@msfur msfur linked a pull request Jan 13, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant