diff --git a/pymodbus/transport/transport.py b/pymodbus/transport/transport.py index 9339bdcd5..747ccc089 100644 --- a/pymodbus/transport/transport.py +++ b/pymodbus/transport/transport.py @@ -159,7 +159,7 @@ def __init__( self.transport: asyncio.BaseTransport = None self.loop: asyncio.AbstractEventLoop = None self.recv_buffer: bytes = b"" - self.call_create: Callable[[], Coroutine[Any, Any, Any]] = lambda: None + self.call_create: Callable[[], Coroutine[Any, Any, Any]] = lambda: None # type: ignore[assignment] if self.is_server: self.active_connections: dict[str, ModbusProtocol] = {} else: @@ -517,7 +517,7 @@ def __init__(self, protocol: ModbusProtocol, listen: int | None = None) -> None: asyncio.DatagramTransport.__init__(self) asyncio.Transport.__init__(self) self.protocol: ModbusProtocol = protocol - self.other_modem: NullModem = None + self.other_modem: NullModem self.listen = listen self.manipulator: Callable[[bytes], list[bytes]] | None = None self._is_closing = False @@ -590,10 +590,10 @@ def close(self) -> None: if self.connections: with suppress(KeyError): del self.connections[self] - if self.other_modem: - self.other_modem.other_modem = None + if hasattr(self, 'other_modem'): + del self.other_modem.other_modem self.other_modem.close() - self.other_modem = None + del self.other_modem if self.protocol: self.protocol.connection_lost(None) diff --git a/pymodbus/transport/transport_serial.py b/pymodbus/transport/transport_serial.py index 63c1affa5..730f9f7fe 100644 --- a/pymodbus/transport/transport_serial.py +++ b/pymodbus/transport/transport_serial.py @@ -38,7 +38,7 @@ def setup(self): def close(self, exc: Exception | None = None) -> None: """Close the transport gracefully.""" - if not self.sync_serial: + if not hasattr(self, 'sync_serial'): return with contextlib.suppress(Exception): self.sync_serial.flush() @@ -51,7 +51,7 @@ def close(self, exc: Exception | None = None) -> None: else: self.async_loop.remove_reader(self.sync_serial.fileno()) self.sync_serial.close() - self.sync_serial = None + del self.sync_serial if exc: with contextlib.suppress(Exception): self._protocol.connection_lost(exc)