Skip to content

Commit

Permalink
Eliminate last implicit optional!
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrudd2 committed Oct 26, 2023
1 parent ad75133 commit d607222
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 2 additions & 0 deletions pymodbus/client/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ def __init__(

self.last_frame_end = None

if not self.comm_params.baudrate:
raise AssertionError("Serial client requires a baudrate")
self._t0 = float(1 + bytesize + stopbits) / self.comm_params.baudrate

# Check every 4 bytes / 2 registers if the reading is ready
Expand Down
20 changes: 15 additions & 5 deletions pymodbus/transport/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,11 @@ async def transport_listen(self) -> bool:
self.loop = asyncio.get_running_loop()
self.is_closing = False
try:
self.transport = await self.call_create()
if isinstance(self.transport, tuple):
self.transport = self.transport[0]
_transport = await self.call_create()
if isinstance(_transport, tuple):
self.transport = _transport[0]
else:
self.transport = _transport
except OSError as exc:
Log.warning("Failed to start server {}", exc)
# self.transport_close(intern=True)
Expand Down Expand Up @@ -435,7 +437,7 @@ def transport_close(self, intern: bool = False, reconnect: bool = False) -> None

def reset_delay(self) -> None:
"""Reset wait time before next reconnect to minimal period."""
self.reconnect_delay_current = self.comm_params.reconnect_delay
self.reconnect_delay_current = self.comm_params.reconnect_delay or 0

def is_active(self) -> bool:
"""Return true if connected/listening."""
Expand Down Expand Up @@ -469,6 +471,12 @@ def handle_new_connection(self) -> ModbusProtocol:

async def do_reconnect(self) -> None:
"""Handle reconnect as a task."""
if not (
self.comm_params.reconnect_delay and self.comm_params.reconnect_delay_max
):
raise AssertionError(
"do_reconnect should not be called if reconnect_delay is None"
)
try:
self.reconnect_delay_current = self.comm_params.reconnect_delay
while True:
Expand Down Expand Up @@ -519,7 +527,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 | None = None
self.listen = listen
self.manipulator: Callable[[bytes], list[bytes]] | None = None
self._is_closing = False
Expand Down Expand Up @@ -605,6 +613,8 @@ def sendto(self, data: bytes, _addr: Any = None) -> None:

def write(self, data: bytes) -> None:
"""Send data"""
if not self.other_modem:
raise AssertionError("Missing other_modem")
if not self.manipulator:
self.other_modem.protocol.data_received(data)
return
Expand Down
8 changes: 4 additions & 4 deletions pymodbus/transport/transport_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def __init__(self, loop, protocol, *args, **kwargs) -> None:
super().__init__()
self.async_loop = loop
self._protocol: asyncio.BaseProtocol = protocol
self.sync_serial = serial.serial_for_url(*args, **kwargs)
self.sync_serial: serial.Serial | None = serial.serial_for_url(*args, **kwargs)
self._write_buffer: list[bytes] = []
self.poll_task = None
self.poll_task: asyncio.Task | None = None
self._poll_wait_time = 0.0005
self.sync_serial.timeout = 0
self.sync_serial.write_timeout = 0
Expand Down Expand Up @@ -58,12 +58,12 @@ def close(self, exc=None) -> None:
def write(self, data) -> None:
"""Write some data to the transport."""
self._write_buffer.append(data)
if not self.poll_task:
if not self.poll_task and self.sync_serial:
self.async_loop.add_writer(self.sync_serial.fileno(), self._write_ready)

def flush(self) -> None:
"""Clear output buffer and stops any more data being written"""
if not self.poll_task:
if not self.poll_task and self.sync_serial:
self.async_loop.remove_writer(self.sync_serial.fileno())
self._write_buffer.clear()

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ overgeneral-exceptions = "builtins.Exception"
bad-functions = "map,input"

[tool.mypy]
strict_optional = false
strict_optional = true
show_error_codes = true
local_partial_types = true
strict_equality = true
Expand Down

0 comments on commit d607222

Please sign in to comment.