Skip to content

Commit 730cfdd

Browse files
authored
Merge branch 'dev' into ruff
2 parents e669cdf + 32e4881 commit 730cfdd

File tree

5 files changed

+27
-23
lines changed

5 files changed

+27
-23
lines changed

pymodbus/client/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def callback_data(self, data: bytes, addr: tuple = None) -> int:
219219
self.framer.processIncomingPacket(data, self._handle_response, slave=0)
220220
return len(data)
221221

222-
def callback_disconnected(self, _reason: Exception) -> None:
222+
def callback_disconnected(self, _reason: Exception | None) -> None:
223223
"""Handle lost connection"""
224224
for tid in list(self.transaction):
225225
self.raise_future(

pymodbus/datastore/simulator.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""Pymodbus ModbusSimulatorContext."""
2+
from __future__ import annotations
3+
24
import dataclasses
35
import random
46
import struct
57
from datetime import datetime
6-
from typing import Any, Callable, Dict, List
8+
from typing import Any, Callable
79

810

911
WORD_SIZE = 16
@@ -30,7 +32,7 @@ class Cell:
3032
access: bool = False
3133
value: int = 0
3234
action: int = 0
33-
action_kwargs: Dict[str, Any] = None
35+
action_kwargs: dict[str, Any] | None = None
3436
count_read: int = 0
3537
count_write: int = 0
3638

@@ -452,18 +454,18 @@ class ModbusSimulatorContext:
452454
start_time = int(datetime.now().timestamp())
453455

454456
def __init__(
455-
self, config: Dict[str, Any], custom_actions: Dict[str, Callable]
457+
self, config: dict[str, Any], custom_actions: dict[str, Callable]
456458
) -> None:
457459
"""Initialize."""
458-
self.registers: List[int] = []
459-
self.fc_offset: Dict[int, int] = {}
460+
self.registers: list[int] = []
461+
self.fc_offset: dict[int, int] = {}
460462
self.register_count = 0
461463
self.type_exception = False
462-
self.action_name_to_id: Dict[str, int] = {}
463-
self.action_id_to_name: List[str] = []
464-
self.action_methods: List[Callable] = []
465-
self.registerType_name_to_id: Dict[str, int] = {}
466-
self.registerType_id_to_name: List[str] = []
464+
self.action_name_to_id: dict[str, int] = {}
465+
self.action_id_to_name: list[str] = []
466+
self.action_methods: list[Callable] = []
467+
self.registerType_name_to_id: dict[str, int] = {}
468+
self.registerType_id_to_name: list[str] = []
467469
Setup(self).setup(config, custom_actions)
468470

469471
# --------------------------------------------

pymodbus/server/async_io.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"""Implementation of a Threaded Modbus Server."""
22
# pylint: disable=missing-type-doc
3+
from __future__ import annotations
4+
35
import asyncio
46
import os
57
import time
68
import traceback
79
from contextlib import suppress
8-
from typing import Union
910

1011
from pymodbus.datastore import ModbusServerContext
1112
from pymodbus.device import ModbusControlBlock, ModbusDeviceIdentification
@@ -74,7 +75,7 @@ def callback_connected(self) -> None:
7475
traceback.format_exc(),
7576
)
7677

77-
def callback_disconnected(self, call_exc: Exception) -> None:
78+
def callback_disconnected(self, call_exc: Exception | None) -> None:
7879
"""Call when connection is lost."""
7980
try:
8081
if self.handler_task:
@@ -229,9 +230,9 @@ async def _recv_(self): # pragma: no cover
229230
result = None
230231
return result
231232

232-
def callback_data(self, data: bytes, addr: tuple = None) -> int:
233+
def callback_data(self, data: bytes, addr: tuple = ()) -> int:
233234
"""Handle received data."""
234-
if addr:
235+
if addr != ():
235236
self.receive_queue.put_nowait((data, addr))
236237
else:
237238
self.receive_queue.put_nowait(data)
@@ -554,7 +555,7 @@ class _serverList:
554555
:meta private:
555556
"""
556557

557-
active_server: Union[ModbusTcpServer, ModbusUdpServer, ModbusSerialServer] = None
558+
active_server: ModbusTcpServer | ModbusUdpServer | ModbusSerialServer
558559

559560
def __init__(self, server):
560561
"""Register new server."""

pymodbus/server/simulator/http_server.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"""HTTP server for modbus simulator."""
2+
from __future__ import annotations
3+
24
import asyncio
35
import contextlib
46
import dataclasses
57
import importlib
68
import json
79
import os
810
from time import time
9-
from typing import List
1011

1112

1213
try:
@@ -118,7 +119,7 @@ def __init__(
118119
http_port: int = 8080,
119120
log_file: str = "server.log",
120121
json_file: str = "setup.json",
121-
custom_actions_module: str = None,
122+
custom_actions_module: str | None = None,
122123
):
123124
"""Initialize http interface."""
124125
if AIOHTTP_MISSING:
@@ -139,15 +140,15 @@ def __init__(
139140
actions_module = importlib.import_module(custom_actions_module)
140141
custom_actions_dict = actions_module.custom_actions_dict
141142
else:
142-
custom_actions_dict = None
143+
custom_actions_dict = {}
143144
server = setup["server_list"][modbus_server]
144145
if server["comm"] != "serial":
145146
server["address"] = (server["host"], server["port"])
146147
del server["host"]
147148
del server["port"]
148149
device = setup["device_list"][modbus_device]
149150
self.datastore_context = ModbusSimulatorContext(
150-
device, custom_actions_dict or None
151+
device, custom_actions_dict or {}
151152
)
152153
datastore = ModbusServerContext(slaves=self.datastore_context, single=True)
153154
comm = comm_class[server.pop("comm")]
@@ -200,8 +201,8 @@ def __init__(
200201
with open(html_file, encoding="utf-8") as handle:
201202
self.generator_html[entry][0] = handle.read()
202203
self.refresh_rate = 0
203-
self.register_filter: List[int] = []
204-
self.call_list: List[tuple] = []
204+
self.register_filter: list[int] = []
205+
self.call_list: list[tuple] = []
205206
self.request_lookup = ServerDecoder.getFCdict()
206207
self.call_monitor = CallTypeMonitor()
207208
self.call_response = CallTypeResponse()

pymodbus/transport/transport.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def callback_connected(self) -> None:
374374
"""Call when connection is succcesfull."""
375375
Log.debug("callback_connected called")
376376

377-
def callback_disconnected(self, exc: Exception) -> None:
377+
def callback_disconnected(self, exc: Exception | None) -> None:
378378
"""Call when connection is lost."""
379379
Log.debug("callback_disconnected called: {}", exc)
380380

0 commit comments

Comments
 (0)