Skip to content

Commit

Permalink
Write_registers/pdu typing again. (#2468)
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen authored Nov 24, 2024
1 parent 8d43c9e commit d475478
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
_logger.setLevel("DEBUG")


def setup_async_client(description=None, cmdline=None):
def setup_async_client(description: str | None =None, cmdline: str | None = None) -> modbusClient.ModbusBaseClient:
"""Run client setup."""
args = helper.get_commandline(
server=False, description=description, cmdline=cmdline
Expand Down
5 changes: 2 additions & 3 deletions examples/client_async_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import sys

from pymodbus import ModbusException
from pymodbus.client import ModbusBaseClient
from pymodbus.pdu import FileRecord


Expand Down Expand Up @@ -157,19 +158,17 @@ async def async_handle_holding_registers(client):
assert not rr.isError() # test that call was OK
assert rr.registers == arguments["values"]

async def async_write_registers_mypy(client):
async def async_write_registers_mypy(client: ModbusBaseClient) -> None:
"""Read/write holding registers."""
regs1: list[int] = [10] * 8
await client.write_registers(1, regs1, slave=SLAVE)
rr = await client.read_holding_registers(1, count=len(regs1), slave=SLAVE)
assert not rr.isError() # test that call was OK
assert rr.registers == regs1

regs2: list[bytes] = [b'\x01\x02', b'\x03\x04']
await client.write_registers(1, regs2, slave=SLAVE)
rr = await client.read_holding_registers(1, count=len(regs2), slave=SLAVE)
assert not rr.isError() # test that call was OK
assert rr.registers == regs2


async def async_handle_input_registers(client):
Expand Down
3 changes: 2 additions & 1 deletion examples/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
import argparse
import logging
import os
from typing import Any

from pymodbus import pymodbus_apply_logging_config


_logger = logging.getLogger(__file__)


def get_commandline(server=False, description=None, extras=None, cmdline=None):
def get_commandline(server: bool = False, description: str | None = None, extras: Any = None, cmdline: str | None = None):
"""Read and validate command line arguments."""
parser = argparse.ArgumentParser(description=description)
parser.add_argument(
Expand Down
11 changes: 8 additions & 3 deletions pymodbus/client/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import struct
from collections.abc import Sequence
from enum import Enum
from typing import Generic, TypeVar

Expand Down Expand Up @@ -501,7 +502,7 @@ def write_coils(
def write_registers(
self,
address: int,
values: list[bytes | int],
values: Sequence[int | bytes],
*,
slave: int = 1,
no_response_expected: bool = False
Expand All @@ -515,7 +516,9 @@ def write_registers(
:raises ModbusException:
.. tip::
values= entries defined as bytes are silently converted to int !
values= parameter:
entries defined as bytes are silently converted to int !
only list[int], list[bytes] or list[bytes | int] are expected (others may work unsupported)
This function is used to write a block of contiguous registers
(1 to approx. 120 registers) in a remote device.
Expand Down Expand Up @@ -627,7 +630,9 @@ def readwrite_registers(
:raises ModbusException:
.. tip::
values= entries defined as bytes are silently converted to int !
values= parameter:
entries defined as bytes are silently converted to int !
only list[int], list[bytes] or list[bytes | int] are expected (others may work unsupported)
This function performs a combination of one read operation and one
write operation in a single MODBUS transaction. The write
Expand Down
4 changes: 2 additions & 2 deletions pymodbus/datastore/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ class Setup:
:meta private:
"""

def __init__(self, runtime):
def __init__(self, runtime: Any) -> None:
"""Initialize."""
self.runtime = runtime
self.config = {}
self.config: Any = {}
self.config_types: dict[str, dict[str, Any]] = {
Label.type_bits: {
Label.type: CellType.BITS,
Expand Down
3 changes: 2 additions & 1 deletion pymodbus/pdu/pdu.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import asyncio
import struct
from abc import abstractmethod
from collections.abc import Sequence
from typing import cast

from pymodbus.exceptions import NotImplementedException
Expand All @@ -24,7 +25,7 @@ def __init__(self,
address: int = 0,
count: int = 0,
bits: list[bool] | None = None,
registers: list[int] | list[bytes] | list[bytes | int] | None = None,
registers: Sequence[bytes | int] | None = None,
status: int = 1,
) -> None:
"""Initialize the base data for a modbus request."""
Expand Down

0 comments on commit d475478

Please sign in to comment.