Skip to content

Should non-list values be allowed for multiple writes? #1363

@alexrudd2

Description

@alexrudd2

The type hints and tests for write_registers() in ModbusClientMixin indicate this method only accepts lists of values.

def write_registers(
self,
address: int,
values: List[Union[int, float, str]],
slave: int = 0,
**kwargs: Any
) -> pdu_req_write.WriteMultipleRegistersResponse:
"""Write registers (code 0x10).
:param address: Start address to write to
:param values: List of booleans to write
:param slave: (optional) Modbus slave unit ID
:param kwargs: (optional) Experimental parameters.
:returns: A deferred response handle
:raises ModbusException:
"""
return self.execute(
pdu_req_write.WriteMultipleRegistersRequest(
address, values, slave, **kwargs
)
)

However, for at least a decade (66bd765) the underlying WriteMultipleRegistersRequest will actually accept single values and convert to a list.

def __init__(self, address=None, values=None, unit=None, **kwargs):
"""Initialize a new instance.
:param address: The address to start writing to
:param values: The values to write
"""
super().__init__(unit=unit, **kwargs)
self.address = address
if values is None:
values = []
elif not hasattr(values, "__iter__"):
values = [values]

(1) Should this functionality be retained for write_registers()?
If no, we're technically breaking backwards compatibility although it's unclear if anyone uses it. pymodbus itself does not appear to.
If yes, we should

  • update docstrings
  • Type hints become (Union[int, float, str, List[Union[int, float, str]]] Union[List[int], int)
  • Add tests for this functionality

(2) Same for readwrite_registers()
(3) Same for write_multiple_coils() write_coils(values: Union[List[bool], bool])`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions