Skip to content

Commit 39d8096

Browse files
committed
Be more careful with type checking for framer
1 parent 7c964f4 commit 39d8096

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

pymodbus/client/base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ def __init__(
3535
"""
3636
ModbusClientMixin.__init__(self) # type: ignore[arg-type]
3737
self.comm_params = comm_params
38+
if isinstance(framer, FramerType):
39+
framer = FRAMER_NAME_TO_CLASS[framer]
3840
self.ctx = TransactionManager(
3941
comm_params,
40-
FRAMER_NAME_TO_CLASS.get(framer, framer)(DecodePDU(False)),
42+
framer(DecodePDU(False)),
4143
retries,
4244
False,
4345
trace_packet,
@@ -133,7 +135,9 @@ def __init__(
133135
self.slaves: list[int] = []
134136

135137
# Common variables.
136-
self.framer: FramerBase = FRAMER_NAME_TO_CLASS.get(framer, framer)(DecodePDU(False))
138+
if isinstance(framer, FramerType):
139+
framer = FRAMER_NAME_TO_CLASS[framer]
140+
self.framer: FramerBase = framer(DecodePDU(False))
137141
self.transaction = TransactionManager(
138142
self.comm_params,
139143
self.framer,

pymodbus/server/async_io.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pymodbus.datastore import ModbusServerContext
1111
from pymodbus.device import ModbusControlBlock, ModbusDeviceIdentification
1212
from pymodbus.exceptions import NoSuchSlaveException
13-
from pymodbus.framer import FRAMER_NAME_TO_CLASS, FramerType, FramerBase
13+
from pymodbus.framer import FRAMER_NAME_TO_CLASS, FramerBase, FramerType
1414
from pymodbus.logging import Log
1515
from pymodbus.pdu import DecodePDU, ModbusPDU
1616
from pymodbus.pdu.pdu import ExceptionResponse
@@ -224,7 +224,9 @@ def __init__(
224224
if isinstance(identity, ModbusDeviceIdentification):
225225
self.control.Identity.update(identity)
226226
# Support mapping of FramerType to a Framer class, or a Framer class
227-
self.framer = FRAMER_NAME_TO_CLASS.get(framer, framer)
227+
if isinstance(framer, FramerType):
228+
framer = FRAMER_NAME_TO_CLASS[framer]
229+
self.framer = framer
228230
self.serving: asyncio.Future = asyncio.Future()
229231

230232
def callback_new_connection(self):
@@ -273,7 +275,7 @@ def __init__(
273275
self,
274276
context: ModbusServerContext,
275277
*,
276-
framer=FramerType.SOCKET,
278+
framer: FramerType | type[FramerBase] = FramerType.SOCKET,
277279
identity: ModbusDeviceIdentification | None = None,
278280
address: tuple[str, int] = ("", 502),
279281
ignore_missing_slaves: bool = False,
@@ -336,7 +338,7 @@ def __init__( # pylint: disable=too-many-arguments
336338
self,
337339
context: ModbusServerContext,
338340
*,
339-
framer=FramerType.TLS,
341+
framer: FramerType | type[FramerBase] = FramerType.TLS,
340342
identity: ModbusDeviceIdentification | None = None,
341343
address: tuple[str, int] = ("", 502),
342344
sslctx=None,
@@ -403,7 +405,7 @@ def __init__(
403405
self,
404406
context: ModbusServerContext,
405407
*,
406-
framer=FramerType.SOCKET,
408+
framer: FramerType | type[FramerBase] = FramerType.SOCKET,
407409
identity: ModbusDeviceIdentification | None = None,
408410
address: tuple[str, int] = ("", 502),
409411
ignore_missing_slaves: bool = False,
@@ -463,7 +465,7 @@ def __init__(
463465
self,
464466
context: ModbusServerContext,
465467
*,
466-
framer: FramerType = FramerType.RTU,
468+
framer: FramerType | type[FramerBase] = FramerType.RTU,
467469
ignore_missing_slaves: bool = False,
468470
identity: ModbusDeviceIdentification | None = None,
469471
broadcast_enable: bool = False,

0 commit comments

Comments
 (0)