Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate more implicit optional #1858

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pymodbus/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ def __init__( # pylint: disable=too-many-arguments
self.transaction = DictTransactionManager(
self, retries=retries, retry_on_empty=retry_on_empty, **kwargs
)
self.reconnect_delay_current = self.params.reconnect_delay
self.reconnect_delay_current = self.params.reconnect_delay or 0
self.use_udp = False
self.state = ModbusTransactionState.IDLE
self.last_frame_end: float = 0
self.last_frame_end: float | None = 0
self.silent_interval: float = 0

# ----------------------------------------------------------------------- #
Expand Down Expand Up @@ -165,7 +165,7 @@ def idle_time(self) -> float:
return 0
return self.last_frame_end + self.silent_interval

def execute(self, request: ModbusRequest = None) -> ModbusResponse:
def execute(self, request: ModbusRequest | None = None) -> ModbusResponse:
"""Execute request and get response (call **sync/async**).
:param request: The request to process
Expand Down Expand Up @@ -211,7 +211,7 @@ async def async_execute(self, request=None):

return resp

def callback_data(self, data: bytes, addr: tuple = None) -> int:
def callback_data(self, data: bytes, addr: tuple | None = None) -> int:
"""Handle received data
returns number of bytes consumed
Expand Down
2 changes: 1 addition & 1 deletion pymodbus/client/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ def read_fifo_queue(
# code 0x2B sub 0x0D: CANopen General Reference Request and Response, NOT IMPLEMENTED

def read_device_information(
self, read_code: int = None, object_id: int = 0x00, **kwargs: Any
self, read_code: int | None = None, object_id: int = 0x00, **kwargs: Any
) -> ModbusResponse | Awaitable[ModbusResponse]:
"""Read FIFO queue (code 0x2B sub 0x0E).
Expand Down
3 changes: 1 addition & 2 deletions pymodbus/client/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ def __init__(
"""Initialize Modbus Serial Client."""
self.transport = None
kwargs["use_sync"] = True
ModbusBaseClient.__init__(
self,
super().__init__(
framer,
CommType=CommType.SERIAL,
host=port,
Expand Down
2 changes: 1 addition & 1 deletion pymodbus/server/async_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ async def _recv_(self): # pragma: no cover
result = None
return result

def callback_data(self, data: bytes, addr: tuple = ()) -> int:
def callback_data(self, data: bytes, addr: tuple | None = ()) -> int:
"""Handle received data."""
if addr != ():
self.receive_queue.put_nowait((data, addr))
Expand Down
26 changes: 16 additions & 10 deletions pymodbus/transport/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,18 @@ 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

async def _noop():
...

self.call_create: Callable[[], Coroutine[Any, Any, Any]] = _noop
if self.is_server:
self.active_connections: dict[str, ModbusProtocol] = {}
else:
self.listener: ModbusProtocol = None
self.listener: ModbusProtocol | None = None
self.unique_id: str = str(id(self))
self.reconnect_task: asyncio.Task = None
self.reconnect_delay_current: float = 0.0
self.reconnect_task: asyncio.Task | None = None
self.reconnect_delay_current = 0.0
self.sent_buffer: bytes = b""

# ModbusProtocol specific setup
Expand Down Expand Up @@ -313,7 +317,7 @@ def data_received(self, data: bytes) -> None:
"""
self.datagram_received(data, None)

def datagram_received(self, data: bytes, addr: tuple) -> None:
def datagram_received(self, data: bytes, addr: tuple | None) -> None:
"""Receive datagram (UDP connections)."""
if self.comm_params.handle_local_echo and self.sent_buffer:
if data.startswith(self.sent_buffer):
Expand Down Expand Up @@ -378,15 +382,15 @@ def callback_disconnected(self, exc: Exception | None) -> None:
"""Call when connection is lost."""
Log.debug("callback_disconnected called: {}", exc)

def callback_data(self, data: bytes, addr: tuple = None) -> int:
def callback_data(self, data: bytes, addr: tuple | None = None) -> int:
"""Handle received data."""
Log.debug("callback_data called: {} addr={}", data, ":hex", addr)
return 0

# ----------------------------------- #
# Helper methods for external classes #
# ----------------------------------- #
def transport_send(self, data: bytes, addr: tuple = None) -> None:
def transport_send(self, data: bytes, addr: tuple | None = None) -> None:
"""Send request.
:param data: non-empty bytes object with data to send.
Expand Down Expand Up @@ -514,14 +518,14 @@ class NullModem(asyncio.DatagramTransport, asyncio.Transport):
listeners: dict[int, ModbusProtocol] = {}
connections: dict[NullModem, int] = {}

def __init__(self, protocol: ModbusProtocol, listen: int = None) -> None:
def __init__(self, protocol: ModbusProtocol, listen: int | None = None) -> None:
"""Create half part of null modem"""
asyncio.DatagramTransport.__init__(self)
asyncio.Transport.__init__(self)
self.protocol: ModbusProtocol = protocol
self.other_modem: NullModem = None
self.listen = listen
self.manipulator: Callable[[bytes], list[bytes]] = None
self.manipulator: Callable[[bytes], list[bytes]] | None = None
self._is_closing = False

# -------------------------- #
Expand Down Expand Up @@ -631,7 +635,9 @@ def get_write_buffer_limits(self) -> tuple[int, int]:
"""Set flush limits"""
return (1, 1024)

def set_write_buffer_limits(self, high: int = None, low: int = None) -> None:
def set_write_buffer_limits(
self, high: int | None = None, low: int | None = None
) -> None:
"""Set flush limits"""

def write_eof(self) -> None:
Expand Down
6 changes: 3 additions & 3 deletions pymodbus/transport/transport_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, loop, protocol, *args, **kwargs) -> None:
self._protocol: asyncio.BaseProtocol = protocol
self.sync_serial = 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 All @@ -35,7 +35,7 @@ def setup(self):
self.async_loop.add_reader(self.sync_serial.fileno(), self._read_ready)
self.async_loop.call_soon(self._protocol.connection_made, self)

def close(self, exc=None):
def close(self, exc: Exception | None = None) -> None:
"""Close the transport gracefully."""
if not self.sync_serial:
return
Expand Down Expand Up @@ -115,7 +115,7 @@ def is_closing(self):
"""Return True if the transport is closing or closed."""
return False

def abort(self):
def abort(self) -> None:
"""Close the transport immediately."""
self.close()

Expand Down