Repository: pymodbus-dev/pymodbus

Issue:
Unnecessary python2->3 code can likely be removed
Since Python2 has been dropped, I think this code can be dropped and its callers changed to use `encode()` directly.

https://github.com/pymodbus-dev/pymodbus/blob/9c6acc385d2cb5362863cad740a90f693785f1e0/pymodbus/utilities.py#L156-L164


Discussion:

Resolving patch:
diff --git a/pymodbus/payload.py b/pymodbus/payload.py
--- a/pymodbus/payload.py
+++ b/pymodbus/payload.py
@@ -11,12 +11,12 @@
 
 # pylint: disable=missing-type-doc
 from struct import pack, unpack
+from typing import List
 
 from pymodbus.constants import Endian
 from pymodbus.exceptions import ParameterException
 from pymodbus.logging import Log
 from pymodbus.utilities import (
-    make_byte_string,
     pack_bitstring,
     unpack_bitstring,
 )
@@ -92,7 +92,7 @@ def __str__(self) -> str:
         """
         return self.encode().decode("utf-8")
 
-    def reset(self):
+    def reset(self) -> None:
         """Reset the payload buffer."""
         self._payload = []
 
@@ -111,7 +111,7 @@ def to_registers(self):
         Log.debug("{}", payload)
         return payload
 
-    def to_coils(self):
+    def to_coils(self) -> List[bool]:
         """Convert the payload buffer into a coil layout that can be used as a context block.
 
         :returns: The coil layout to use as a block
@@ -120,7 +120,7 @@ def to_coils(self):
         coils = [bool(int(bit)) for reg in payload for bit in format(reg, "016b")]
         return coils
 
-    def build(self):
+    def build(self) -> List[bytes]:
         """Return the payload buffer as a list.
 
         This list is two bytes per element and can
@@ -133,7 +133,7 @@ def build(self):
         buffer += b"\x00" * (length % 2)
         return [buffer[i : i + 2] for i in range(0, length, 2)]
 
-    def add_bits(self, values):
+    def add_bits(self, values: List[bool]) -> None:
         """Add a collection of bits to be encoded.
 
         If these are less than a multiple of eight,
@@ -145,7 +145,7 @@ def add_bits(self, values):
         value = pack_bitstring(values)
         self._payload.append(value)
 
-    def add_8bit_uint(self, value):
+    def add_8bit_uint(self, value: int) -> None:
         """Add a 8 bit unsigned int to the buffer.
 
         :param value: The value to add to the buffer
@@ -153,7 +153,7 @@ def add_8bit_uint(self, value):
         fstring = self._byteorder + "B"
         self._payload.append(pack(fstring, value))
 
-    def add_16bit_uint(self, value):
+    def add_16bit_uint(self, value: int) -> None:
         """Add a 16 bit unsigned int to the buffer.
 
         :param value: The value to add to the buffer
@@ -161,7 +161,7 @@ def add_16bit_uint(self, value):
         fstring = self._byteorder + "H"
         self._payload.append(pack(fstring, value))
 
-    def add_32bit_uint(self, value):
+    def add_32bit_uint(self, value: int) -> None:
         """Add a 32 bit unsigned int to the buffer.
 
         :param value: The value to add to the buffer
@@ -171,7 +171,7 @@ def add_32bit_uint(self, value):
         p_string = self._pack_words(fstring, value)
         self._payload.append(p_string)
 
-    def add_64bit_uint(self, value):
+    def add_64bit_uint(self, value: int) -> None:
         """Add a 64 bit unsigned int to the buffer.
 
         :param value: The value to add to the buffer
@@ -180,7 +180,7 @@ def add_64bit_uint(self, value):
         p_string = self._pack_words(fstring, value)
         self._payload.append(p_string)
 
-    def add_8bit_int(self, value):
+    def add_8bit_int(self, value: int) -> None:
         """Add a 8 bit signed int to the buffer.
 
         :param value: The value to add to the buffer
@@ -188,7 +188,7 @@ def add_8bit_int(self, value):
         fstring = self._byteorder + "b"
         self._payload.append(pack(fstring, value))
 
-    def add_16bit_int(self, value):
+    def add_16bit_int(self, value: int) -> None:
         """Add a 16 bit signed int to the buffer.
 
         :param value: The value to add to the buffer
@@ -196,7 +196,7 @@ def add_16bit_int(self, value):
         fstring = self._byteorder + "h"
         self._payload.append(pack(fstring, value))
 
-    def add_32bit_int(self, value):
+    def add_32bit_int(self, value: int) -> None:
         """Add a 32 bit signed int to the buffer.
 
         :param value: The value to add to the buffer
@@ -205,7 +205,7 @@ def add_32bit_int(self, value):
         p_string = self._pack_words(fstring, value)
         self._payload.append(p_string)
 
-    def add_64bit_int(self, value):
+    def add_64bit_int(self, value: int) -> None:
         """Add a 64 bit signed int to the buffer.
 
         :param value: The value to add to the buffer
@@ -214,7 +214,7 @@ def add_64bit_int(self, value):
         p_string = self._pack_words(fstring, value)
         self._payload.append(p_string)
 
-    def add_16bit_float(self, value):
+    def add_16bit_float(self, value: float) -> None:
         """Add a 16 bit float to the buffer.
 
         :param value: The value to add to the buffer
@@ -223,7 +223,7 @@ def add_16bit_float(self, value):
         p_string = self._pack_words(fstring, value)
         self._payload.append(p_string)
 
-    def add_32bit_float(self, value):
+    def add_32bit_float(self, value: float) -> None:
         """Add a 32 bit float to the buffer.
 
         :param value: The value to add to the buffer
@@ -232,7 +232,7 @@ def add_32bit_float(self, value):
         p_string = self._pack_words(fstring, value)
         self._payload.append(p_string)
 
-    def add_64bit_float(self, value):
+    def add_64bit_float(self, value: float) -> None:
         """Add a 64 bit float(double) to the buffer.
 
         :param value: The value to add to the buffer
@@ -241,14 +241,13 @@ def add_64bit_float(self, value):
         p_string = self._pack_words(fstring, value)
         self._payload.append(p_string)
 
-    def add_string(self, value):
+    def add_string(self, value: str) -> None:
         """Add a string to the buffer.
 
         :param value: The value to add to the buffer
         """
-        value = make_byte_string(value)
         fstring = self._byteorder + str(len(value)) + "s"
-        self._payload.append(pack(fstring, value))
+        self._payload.append(pack(fstring, value.encode()))
 
 
 class BinaryPayloadDecoder:
@@ -339,7 +338,6 @@ def _unpack_words(self, fstring, handle):
         :param handle: Value to be unpacked
         :return:
         """
-        handle = make_byte_string(handle)
         wc_value = WC.get(fstring.lower()) // 2
         handle = unpack(f"!{wc_value}H", handle)
         if self._wordorder == Endian.Little:
@@ -360,7 +358,6 @@ def decode_8bit_uint(self):
         self._pointer += 1
         fstring = self._byteorder + "B"
         handle = self._payload[self._pointer - 1 : self._pointer]
-        handle = make_byte_string(handle)
         return unpack(fstring, handle)[0]
 
     def decode_bits(self, package_len=1):
@@ -368,7 +365,6 @@ def decode_bits(self, package_len=1):
         self._pointer += package_len
         # fstring = self._endian + "B"
         handle = self._payload[self._pointer - 1 : self._pointer]
-        handle = make_byte_string(handle)
         return unpack_bitstring(handle)
 
     def decode_16bit_uint(self):
@@ -376,14 +372,12 @@ def decode_16bit_uint(self):
         self._pointer += 2
         fstring = self._byteorder + "H"
         handle = self._payload[self._pointer - 2 : self._pointer]
-        handle = make_byte_string(handle)
         return unpack(fstring, handle)[0]
 
     def decode_32bit_uint(self):
         """Decode a 32 bit unsigned int from the buffer."""
         self._pointer += 4
         fstring = "I"
-        # fstring = "I"
         handle = self._payload[self._pointer - 4 : self._pointer]
         handle = self._unpack_words(fstring, handle)
         return unpack("!" + fstring, handle)[0]
@@ -401,7 +395,6 @@ def decode_8bit_int(self):
         self._pointer += 1
         fstring = self._byteorder + "b"
         handle = self._payload[self._pointer - 1 : self._pointer]
-        handle = make_byte_string(handle)
         return unpack(fstring, handle)[0]
 
     def decode_16bit_int(self):
@@ -409,7 +402,6 @@ def decode_16bit_int(self):
         self._pointer += 2
         fstring = self._byteorder + "h"
         handle = self._payload[self._pointer - 2 : self._pointer]
-        handle = make_byte_string(handle)
         return unpack(fstring, handle)[0]
 
     def decode_32bit_int(self):
diff --git a/pymodbus/utilities.py b/pymodbus/utilities.py
--- a/pymodbus/utilities.py
+++ b/pymodbus/utilities.py
@@ -153,17 +153,6 @@ def unpack_bitstring(string):
     return bits
 
 
-def make_byte_string(byte_string):
-    """Return byte string from a given string, python3 specific fix.
-
-    :param byte_string:
-    :return:
-    """
-    if isinstance(byte_string, str):
-        byte_string = byte_string.encode()
-    return byte_string
-
-
 # --------------------------------------------------------------------------- #
 # Error Detection Functions
 # --------------------------------------------------------------------------- #


Test patch:
diff --git a/test/test_payload.py b/test/test_payload.py
--- a/test/test_payload.py
+++ b/test/test_payload.py
@@ -57,7 +57,7 @@ def test_little_endian_payload_builder(self):
         builder.add_32bit_float(1.25)
         builder.add_64bit_float(6.25)
         builder.add_16bit_uint(1)  # placeholder
-        builder.add_string(b"test")
+        builder.add_string("test")
         builder.add_bits(self.bitstring)
         assert self.little_endian_payload == builder.encode()