33
44Created on 7/19/2023 to support Python 3.8 to 3.11 on macOS, Ubuntu, or Windows.
55"""
6+ from __future__ import annotations
67
78import contextlib
89import os
910import shutil
1011import tempfile
1112from dataclasses import dataclass
1213from html .parser import HTMLParser
13- from typing import List , Optional , Tuple , Union
1414from urllib import request
1515from urllib .error import HTTPError
1616
@@ -27,10 +27,10 @@ class ParsedModbusResult: # pylint: disable=too-many-instance-attributes
2727 unit_id : int
2828 func_code : int
2929 is_receive : bool
30- zero_index_reg : Optional [ int ] = None
31- quantity : Optional [ int ] = None
32- byte_count : Optional [ int ] = None
33- registers : Optional [ List [ int ]] = None
30+ zero_index_reg : int | None = None
31+ quantity : int | None = None
32+ byte_count : int | None = None
33+ registers : list [ int ] | None = None
3434
3535 def summarize (self ) -> dict :
3636 """Get a summary representation for readability."""
@@ -46,7 +46,7 @@ def explain_with_rapid_scada(
4646 packet : str ,
4747 is_modbus_tcp : bool = True ,
4848 is_receive : bool = False ,
49- timeout : Union [ float , Tuple [float , float ], None ] = 15.0 ,
49+ timeout : float | tuple [float , float ] | None = 15.0 ,
5050) -> ParsedModbusResult :
5151 """
5252 Explain a Modbus packet using https://rapidscada.net/modbus/.
@@ -69,7 +69,7 @@ def __init__(self, *, convert_charrefs=True):
6969 self ._data = []
7070
7171 @property
72- def data (self ) -> List [str ]:
72+ def data (self ) -> list [str ]:
7373 return self ._data
7474
7575 def handle_data (self , data : str ) -> None :
@@ -101,7 +101,7 @@ def handle_data(self, data: str) -> None:
101101 parser .feed (response_data )
102102
103103 # pylint: disable-next=dangerous-default-value
104- def get_next_field (prior_field : str , data : List [str ] = parser .data ) -> str :
104+ def get_next_field (prior_field : str , data : list [str ] = parser .data ) -> str :
105105 return data [data .index (prior_field ) + 1 ]
106106
107107 def parse_next_field (prior_field : str , split_index : int = 0 ) -> int :
@@ -114,7 +114,7 @@ def parse_next_field(prior_field: str, split_index: int = 0) -> int:
114114 "func_code" : parse_next_field ("Function code" ),
115115 "is_receive" : is_receive ,
116116 }
117- is_receive_fn_code : Tuple [bool , int ] = is_receive , base_result_data ["func_code" ]
117+ is_receive_fn_code : tuple [bool , int ] = is_receive , base_result_data ["func_code" ]
118118 if is_receive_fn_code in [(False , 0x03 ), (True , 0x10 )]:
119119 return ParsedModbusResult (
120120 ** base_result_data ,
@@ -136,7 +136,7 @@ def parse_next_field(prior_field: str, split_index: int = 0) -> int:
136136 )
137137
138138
139- def annotate_pymodbus_logs (file : Union [ str , os .PathLike ] ) -> None :
139+ def annotate_pymodbus_logs (file : str | os .PathLike ) -> None :
140140 """Annotate a pymodbus log file in-place with explanations."""
141141 with open (file , encoding = "utf-8" ) as in_file , tempfile .NamedTemporaryFile (
142142 mode = "w" , encoding = "utf-8" , delete = False
0 commit comments