Skip to content

Commit 32e4881

Browse files
authored
Eliminate implicit optional in simulator (#1841)
1 parent e0571ca commit 32e4881

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

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/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()

0 commit comments

Comments
 (0)