Skip to content

Commit f781cae

Browse files
A few more type hints (#1556)
Co-authored-by: jan iversen <[email protected]>
1 parent 448a2a8 commit f781cae

File tree

6 files changed

+29
-19
lines changed

6 files changed

+29
-19
lines changed

API_changes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Version 3.3.0
1010
- move SqlSlaveContext, RedisSlaveContext to examples/contrib (due to lack of maintenance)
1111
- :code:`BinaryPayloadBuilder.to_string` was renamed to :code:`BinaryPayloadBuilder.encode`
1212
- on_reconnect_callback for async clients works slightly different
13+
- utilities/unpack_bitstring now expects an argument named `data` not `string`
1314

1415
-------------
1516
Version 3.2.0

pymodbus/device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ class ModbusControlBlock:
464464
__mode = "ASCII"
465465
__diagnostic = [False] * 16
466466
__listen_only = False
467-
__delimiter = "\r"
467+
__delimiter = b"\r"
468468
__counters = ModbusCountersHandler()
469469
__identity = ModbusDeviceIdentification()
470470
__plus = ModbusPlusStatistics()

pymodbus/events.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ def encode(self):
1717
1818
:raises NotImplementedException:
1919
"""
20-
raise NotImplementedException()
20+
raise NotImplementedException
2121

2222
def decode(self, event):
2323
"""Decode the event message to its status bits.
2424
2525
:param event: The event to decode
2626
:raises NotImplementedException:
2727
"""
28-
raise NotImplementedException()
28+
raise NotImplementedException
2929

3030

3131
class RemoteReceiveEvent(ModbusEvent):
@@ -54,7 +54,7 @@ def __init__(self, **kwargs):
5454
self.listen = kwargs.get("listen", False)
5555
self.broadcast = kwargs.get("broadcast", False)
5656

57-
def encode(self):
57+
def encode(self) -> bytes:
5858
"""Encode the status bits to an event message.
5959
6060
:returns: The encoded event message
@@ -64,7 +64,7 @@ def encode(self):
6464
packet = pack_bitstring(bits)
6565
return packet
6666

67-
def decode(self, event):
67+
def decode(self, event: bytes) -> None:
6868
"""Decode the event message to its status bits.
6969
7070
:param event: The event to decode

pymodbus/framer/base.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""Framer start."""
22
# pylint: disable=missing-type-doc
3+
from typing import Any, Dict, Union
4+
5+
from pymodbus.factory import ClientDecoder, ServerDecoder
36

47

58
# Unit ID, Function Code
@@ -18,15 +21,20 @@ class ModbusFramer:
1821

1922
name = ""
2023

21-
def __init__(self, decoder, client=None):
24+
def __init__(
25+
self,
26+
decoder: Union[ClientDecoder, ServerDecoder],
27+
client=None,
28+
) -> None:
2229
"""Initialize a new instance of the framer.
2330
2431
:param decoder: The decoder implementation to use
2532
"""
2633
self.decoder = decoder
2734
self.client = client
35+
self._header: Dict[str, Any] = {}
2836

29-
def _validate_slave_id(self, slaves, single):
37+
def _validate_slave_id(self, slaves: list, single: bool) -> bool:
3038
"""Validate if the received data is valid for the client.
3139
3240
:param slaves: list of slave id for which the transaction is valid
@@ -39,7 +47,7 @@ def _validate_slave_id(self, slaves, single):
3947
# Handle Modbus TCP slave identifier (0x00 0r 0xFF)
4048
# in asynchronous requests
4149
return True
42-
return self._header["uid"] in slaves # pylint: disable=no-member
50+
return self._header["uid"] in slaves
4351

4452
def sendPacket(self, message):
4553
"""Send packets on the bus.

pymodbus/framer/binary_framer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def decode_data(self, data):
6565
return {"slave": uid, "fcode": fcode}
6666
return {}
6767

68-
def checkFrame(self):
68+
def checkFrame(self) -> bool:
6969
"""Check and decode the next frame.
7070
7171
:returns: True if we are successful, False otherwise
@@ -84,7 +84,7 @@ def checkFrame(self):
8484
return checkCRC(data, self._header["crc"])
8585
return False
8686

87-
def advanceFrame(self):
87+
def advanceFrame(self) -> None:
8888
"""Skip over the current framed message.
8989
9090
This allows us to skip over the current message after we have processed
@@ -94,7 +94,7 @@ def advanceFrame(self):
9494
self._buffer = self._buffer[self._header["len"] + 2 :]
9595
self._header = {"crc": 0x0000, "len": 0, "uid": 0x00}
9696

97-
def isFrameReady(self):
97+
def isFrameReady(self) -> bool:
9898
"""Check if we should continue decode logic.
9999
100100
This is meant to be used in a while loop in the decoding phase to let

pymodbus/utilities.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
# pylint: disable=missing-type-doc
1919
import struct
20+
from typing import List
2021

2122

2223
class ModbusTransactionState: # pylint: disable=too-few-public-methods
@@ -106,10 +107,10 @@ def dict_property(store, index):
106107
# --------------------------------------------------------------------------- #
107108
# Bit packing functions
108109
# --------------------------------------------------------------------------- #
109-
def pack_bitstring(bits):
110-
"""Create a string out of an array of bits.
110+
def pack_bitstring(bits: List[bool]) -> bytes:
111+
"""Create a bytestring out of a list of bits.
111112
112-
:param bits: A bit array
113+
:param bits: A list of bits
113114
114115
example::
115116
@@ -133,20 +134,20 @@ def pack_bitstring(bits):
133134
return ret
134135

135136

136-
def unpack_bitstring(string):
137-
"""Create bit array out of a string.
137+
def unpack_bitstring(data: bytes) -> List[bool]:
138+
"""Create bit list out of a bytestring.
138139
139-
:param string: The modbus data packet to decode
140+
:param data: The modbus data packet to decode
140141
141142
example::
142143
143144
bytes = "bytes to decode"
144145
result = unpack_bitstring(bytes)
145146
"""
146-
byte_count = len(string)
147+
byte_count = len(data)
147148
bits = []
148149
for byte in range(byte_count):
149-
value = int(int(string[byte]))
150+
value = int(int(data[byte]))
150151
for _ in range(8):
151152
bits.append((value & 1) == 1)
152153
value >>= 1

0 commit comments

Comments
 (0)