From 36d0a21f1d52055de400bd14ce593b96449200bb Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Sun, 26 Feb 2023 19:51:48 -0600 Subject: [PATCH 1/4] Remove unused private attribute --- pymodbus/device.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pymodbus/device.py b/pymodbus/device.py index ad74a23bb..dabf81529 100644 --- a/pymodbus/device.py +++ b/pymodbus/device.py @@ -456,7 +456,6 @@ class ModbusControlBlock: __mode = "ASCII" __diagnostic = [False] * 16 - __instance = None __listen_only = False __delimiter = "\r" __counters = ModbusCountersHandler() From b1b60b95a683aa7cf5495e133d1f7831d8bc91ee Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Sun, 26 Feb 2023 19:52:11 -0600 Subject: [PATCH 2/4] Add type hints --- pymodbus/repl/client/helper.py | 7 ++++--- pymodbus/server/async_io.py | 7 +++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/pymodbus/repl/client/helper.py b/pymodbus/repl/client/helper.py index ddd2f3438..7790eb0f0 100644 --- a/pymodbus/repl/client/helper.py +++ b/pymodbus/repl/client/helper.py @@ -4,6 +4,7 @@ # pylint: disable=missing-type-doc import json from collections import OrderedDict +from typing import Any, Dict, List, Union import pygments from prompt_toolkit import print_formatted_text @@ -58,7 +59,7 @@ "set_timeout", "get_serial_settings", ] -CLIENT_ATTRIBUTES = [] +CLIENT_ATTRIBUTES: List[str] = [] class Command: @@ -230,8 +231,8 @@ def get_commands(client): class Result: """Represent result command.""" - function_code = None - data = None + function_code: int = None + data: Union[Dict[int, Any], Any] = None def __init__(self, result): """Initialize. diff --git a/pymodbus/server/async_io.py b/pymodbus/server/async_io.py index 074867a73..ec398a1e8 100644 --- a/pymodbus/server/async_io.py +++ b/pymodbus/server/async_io.py @@ -4,6 +4,7 @@ import ssl import time import traceback +from typing import Union from pymodbus.client.serial_asyncio import create_serial_connection from pymodbus.constants import Defaults @@ -873,7 +874,7 @@ class ModbusSerialServer: # pylint: disable=too-many-instance-attributes server context instance. """ - handler = None + handler: ModbusSingleRequestHandler = None def __init__( self, context, framer=ModbusRtuFramer, identity=None, **kwargs @@ -1052,7 +1053,9 @@ class _serverList: :meta private: """ - active_server = None + active_server: Union[ + ModbusUnixServer, ModbusTcpServer, ModbusUdpServer, ModbusSerialServer + ] = None def __init__(self, server): """Register new server.""" From 5a5039b8e65e1b7b02aa9c6813c00d400b267f48 Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Sun, 26 Feb 2023 19:52:24 -0600 Subject: [PATCH 3/4] Expand type hint to allow single ints --- pymodbus/server/reactive/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pymodbus/server/reactive/main.py b/pymodbus/server/reactive/main.py index 56db4d504..c9b7d53bc 100644 --- a/pymodbus/server/reactive/main.py +++ b/pymodbus/server/reactive/main.py @@ -10,6 +10,7 @@ import threading import time from enum import Enum +from typing import Union try: @@ -396,7 +397,7 @@ def create_identity( def create_context( cls, data_block_settings: dict = {}, - unit: list[int] = [1], + unit: Union[list[int]] | int = [1], single: bool = False, randomize: int = 0, change_rate: int = 0, From 9a0b0fac55e5aef7260300118d7914496c649097 Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Sun, 26 Feb 2023 19:54:02 -0600 Subject: [PATCH 4/4] Enable more type checks --- setup.cfg | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 289b804a0..f51ec2a38 100644 --- a/setup.cfg +++ b/setup.cfg @@ -503,7 +503,29 @@ noqa-require-code = True [mypy] strict_optional = False -exclude = pymodbus/client/base.py +exclude = pymodbus/client/base.py + +# below are those used in HomeAssistant +show_error_codes = true +# follow_imports = silent +# ignore_missing_imports = true +local_partial_types = true +strict_equality = true +warn_incomplete_stub = true +warn_redundant_casts = true +warn_unused_configs = true +warn_unused_ignores = true +enable_error_code = ignore-without-code, redundant-self, truthy-iterable +disable_error_code = annotation-unchecked +strict_concatenate = false +# check_untyped_defs = true +# disallow_incomplete_defs = true +disallow_subclassing_any = true +# disallow_untyped_calls = true +disallow_untyped_decorators = true +# disallow_untyped_defs = true +# warn_return_any = true +warn_unreachable = true [egg_info]