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

Make ModbusClientMixin Generic to fix type issues for sync and async #1980

Merged
merged 8 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
19 changes: 10 additions & 9 deletions pymodbus/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import asyncio
import socket
from dataclasses import dataclass
from typing import Any, Callable, Type, cast
from typing import Any, Awaitable, Callable, Type, cast

from pymodbus.client.mixin import ModbusClientMixin
from pymodbus.exceptions import ConnectionException, ModbusIOException
Expand All @@ -17,7 +17,7 @@
from pymodbus.utilities import ModbusTransactionState


class ModbusBaseClient(ModbusClientMixin, ModbusProtocol):
class ModbusBaseClient(ModbusClientMixin[Awaitable[ModbusResponse]], ModbusProtocol):
"""**ModbusBaseClient**.

Fixed parameters:
Expand Down Expand Up @@ -60,7 +60,7 @@ def __init__(
**kwargs: Any,
) -> None:
"""Initialize a client instance."""
ModbusClientMixin.__init__(self)
alexrudd2 marked this conversation as resolved.
Show resolved Hide resolved
ModbusClientMixin.__init__(self) # type: ignore[arg-type]
ModbusProtocol.__init__(
self,
CommParams(
Expand Down Expand Up @@ -136,7 +136,7 @@ def idle_time(self) -> float:
return 0
return self.last_frame_end + self.silent_interval

def execute(self, request: ModbusRequest | None = None) -> ModbusResponse:
def execute(self, request: ModbusRequest | None = None):
janiversen marked this conversation as resolved.
Show resolved Hide resolved
"""Execute request and get response (call **sync/async**).

:param request: The request to process
Expand All @@ -150,7 +150,7 @@ def execute(self, request: ModbusRequest | None = None) -> ModbusResponse:
# ----------------------------------------------------------------------- #
# Merged client methods
# ----------------------------------------------------------------------- #
async def async_execute(self, request=None):
async def async_execute(self, request=None) -> ModbusResponse:
"""Execute requests asynchronously."""
request.transaction_id = self.transaction.getNextTID()
packet = self.framer.buildPacket(request)
Expand All @@ -176,7 +176,7 @@ async def async_execute(self, request=None):
f"ERROR: No response received after {self.retries} retries"
)

return resp
return resp # type: ignore[return-value]

def callback_data(self, data: bytes, addr: tuple | None = None) -> int:
"""Handle received data.
Expand Down Expand Up @@ -266,7 +266,8 @@ def __str__(self):
f"{self.__class__.__name__} {self.comm_params.host}:{self.comm_params.port}"
)

class ModbusBaseSyncClient(ModbusClientMixin, ModbusProtocol):

class ModbusBaseSyncClient(ModbusClientMixin[ModbusResponse], ModbusProtocol):
"""**ModbusBaseClient**.

Fixed parameters:
Expand Down Expand Up @@ -319,7 +320,7 @@ def __init__(
**kwargs: Any,
) -> None:
"""Initialize a client instance."""
ModbusClientMixin.__init__(self)
ModbusClientMixin.__init__(self) # type: ignore[arg-type]
ModbusProtocol.__init__(
self,
CommParams(
Expand All @@ -337,7 +338,7 @@ def __init__(
parity=kwargs.get("parity", None),
stopbits=kwargs.get("stopbits", None),
handle_local_echo=kwargs.get("handle_local_echo", False),
on_reconnect_callback = on_reconnect_callback,
on_reconnect_callback=on_reconnect_callback,
),
False,
)
Expand Down
Loading