Skip to content

Commit 54c531c

Browse files
committed
FIX: more ruff and mypy linting
1 parent eecb837 commit 54c531c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+423
-408
lines changed

MethodicConfigurator/annotate_params.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class Par:
133133
comment (Optional[str]): An optional comment associated with the parameter.
134134
"""
135135

136-
def __init__(self, value: float, comment: Optional[str] = None):
136+
def __init__(self, value: float, comment: Optional[str] = None) -> None:
137137
self.value = value
138138
self.comment = comment
139139

@@ -184,7 +184,7 @@ def load_param_file_into_dict(param_file: str) -> dict[str, "Par"]:
184184
return parameter_dict
185185

186186
@staticmethod
187-
def validate_parameter(param_file, parameter_dict, i, original_line, comment, parameter, value): # pylint: disable=too-many-arguments
187+
def validate_parameter(param_file, parameter_dict, i, original_line, comment, parameter, value) -> None: # pylint: disable=too-many-arguments
188188
if len(parameter) > PARAM_NAME_MAX_LEN:
189189
raise SystemExit(f"Too long parameter name: {parameter} in {param_file} line {i}")
190190
if not re.match(PARAM_NAME_REGEX, parameter):
@@ -646,7 +646,7 @@ def update_parameter_documentation_file( # pylint: disable=too-many-locals, too
646646
param_file,
647647
lines,
648648
delete_documentation_annotations: bool,
649-
):
649+
) -> None:
650650
new_lines = []
651651
if os_path.basename(param_file).endswith("16_pid_adjustment.param"):
652652
new_lines.extend(lines[0:5]) # copy the first 6 lines verbatim
@@ -703,7 +703,7 @@ def update_parameter_documentation_file( # pylint: disable=too-many-locals, too
703703
file.writelines(new_lines)
704704

705705

706-
def print_read_only_params(doc):
706+
def print_read_only_params(doc) -> None:
707707
"""
708708
Print the names of read-only parameters.
709709
@@ -751,7 +751,7 @@ def parse_parameter_metadata(
751751
return create_doc_dict(xml_root, vehicle_type, max_line_length)
752752

753753

754-
def main():
754+
def main() -> None:
755755
args = arg_parser()
756756
try:
757757
xml_url = get_xml_url(args.vehicle_type, args.firmware_version)

MethodicConfigurator/argparse_check_range.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
https://gist.github.com/dmitriykovalev/2ab1aa33a8099ef2d514925d84aa89e7
1111
"""
1212

13-
from argparse import Action, ArgumentError
13+
from argparse import Action, ArgumentError, ArgumentParser, Namespace
1414
from operator import ge, gt, le, lt
1515

1616
from MethodicConfigurator import _
@@ -21,7 +21,7 @@ class CheckRange(Action):
2121
Check if the Argparse argument value is within the specified range
2222
"""
2323

24-
def __init__(self, *args, **kwargs):
24+
def __init__(self, *args, **kwargs) -> None:
2525
if "min" in kwargs and "inf" in kwargs:
2626
raise ValueError(_("either min or inf, but not both"))
2727
if "max" in kwargs and "sup" in kwargs:
@@ -52,7 +52,7 @@ def interval(self):
5252
msg = _("valid range: {_lo}, {_up}")
5353
return msg.format(**locals())
5454

55-
def __call__(self, parser, namespace, values, option_string=None):
55+
def __call__(self, parser: ArgumentParser, namespace: Namespace, values, option_string=None) -> None:
5656
if not isinstance(values, (int, float)):
5757
raise ArgumentError(self, _("Value must be a number."))
5858

MethodicConfigurator/backend_filesystem.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010

1111
# from sys import exit as sys_exit
12+
from argparse import ArgumentParser
1213
from logging import debug as logging_debug
1314
from logging import error as logging_error
1415
from logging import info as logging_info
@@ -21,7 +22,7 @@
2122
from re import compile as re_compile
2223
from shutil import copy2 as shutil_copy2
2324
from shutil import copytree as shutil_copytree
24-
from typing import Any
25+
from typing import Any, Optional
2526
from zipfile import ZipFile
2627

2728
from requests import get as requests_get # type: ignore[import-untyped]
@@ -81,7 +82,7 @@ class LocalFilesystem(VehicleComponents, ConfigurationSteps, ProgramSettings):
8182
doc_dict (dict): A dictionary containing documentation for each parameter.
8283
"""
8384

84-
def __init__(self, vehicle_dir: str, vehicle_type: str, fw_version: str, allow_editing_template_files: bool):
85+
def __init__(self, vehicle_dir: str, vehicle_type: str, fw_version: str, allow_editing_template_files: bool) -> None:
8586
self.file_parameters: dict[str, dict[str, Par]] = {}
8687
VehicleComponents.__init__(self)
8788
ConfigurationSteps.__init__(self, vehicle_dir, vehicle_type)
@@ -95,7 +96,7 @@ def __init__(self, vehicle_dir: str, vehicle_type: str, fw_version: str, allow_e
9596
if vehicle_dir is not None:
9697
self.re_init(vehicle_dir, vehicle_type)
9798

98-
def re_init(self, vehicle_dir: str, vehicle_type: str):
99+
def re_init(self, vehicle_dir: str, vehicle_type: str) -> None:
99100
self.vehicle_dir = vehicle_dir
100101
self.doc_dict = {}
101102

@@ -149,7 +150,7 @@ def vehicle_configuration_files_exist(self, vehicle_dir: str) -> bool:
149150
return True
150151
return False
151152

152-
def rename_parameter_files(self):
153+
def rename_parameter_files(self) -> None:
153154
if self.vehicle_dir is None or self.configuration_steps is None:
154155
return
155156
# Rename parameter files if some new files got added to the vehicle directory
@@ -170,7 +171,7 @@ def rename_parameter_files(self):
170171
os_rename(old_filename_path, new_filename_path)
171172
logging_info("Renamed %s to %s", old_filename, new_filename)
172173

173-
def __extend_and_reformat_parameter_documentation_metadata(self): # pylint: disable=too-many-branches
174+
def __extend_and_reformat_parameter_documentation_metadata(self) -> None: # pylint: disable=too-many-branches
174175
for param_name, param_info in self.doc_dict.items():
175176
if "fields" in param_info:
176177
param_fields = param_info["fields"]
@@ -242,7 +243,7 @@ def read_params_from_files(self) -> dict[str, dict[str, "Par"]]:
242243
return parameters
243244

244245
@staticmethod
245-
def str_to_bool(s):
246+
def str_to_bool(s) -> Optional[bool]:
246247
"""
247248
Converts a string representation of a boolean value to a boolean.
248249
@@ -389,11 +390,11 @@ def zip_file_exists(self):
389390
zip_file_path = self.zip_file_path()
390391
return os_path.exists(zip_file_path) and os_path.isfile(zip_file_path)
391392

392-
def add_configuration_file_to_zip(self, zipf, filename):
393+
def add_configuration_file_to_zip(self, zipf, filename) -> None:
393394
if self.vehicle_configuration_file_exists(filename):
394395
zipf.write(os_path.join(self.vehicle_dir, filename), arcname=filename)
395396

396-
def zip_files(self, files_to_zip: list[tuple[bool, str]]):
397+
def zip_files(self, files_to_zip: list[tuple[bool, str]]) -> None:
397398
"""
398399
Zips the intermediate parameter files that were written to, including specific summary files.
399400
@@ -442,14 +443,14 @@ def vehicle_image_exists(self):
442443
return os_path.exists(self.vehicle_image_filepath()) and os_path.isfile(self.vehicle_image_filepath())
443444

444445
@staticmethod
445-
def new_vehicle_dir(base_dir: str, new_dir: str):
446+
def new_vehicle_dir(base_dir: str, new_dir: str) -> str:
446447
return os_path.join(base_dir, new_dir)
447448

448449
@staticmethod
449450
def directory_exists(directory: str) -> bool:
450451
return os_path.exists(directory) and os_path.isdir(directory)
451452

452-
def copy_template_files_to_new_vehicle_dir(self, template_dir: str, new_vehicle_dir: str):
453+
def copy_template_files_to_new_vehicle_dir(self, template_dir: str, new_vehicle_dir: str) -> None:
453454
# Copy the template files to the new vehicle directory
454455
for item in os_listdir(template_dir):
455456
if item in {"apm.pdef.xml", "vehicle.jpg", "last_uploaded_filename.txt", "tempcal_acc.png", "tempcal_gyro.png"}:
@@ -462,7 +463,7 @@ def copy_template_files_to_new_vehicle_dir(self, template_dir: str, new_vehicle_
462463
shutil_copy2(s, d)
463464

464465
@staticmethod
465-
def getcwd():
466+
def getcwd() -> str:
466467
return os_getcwd()
467468

468469
def tempcal_imu_result_param_tuple(self):
@@ -480,7 +481,7 @@ def copy_fc_values_to_file(self, selected_file: str, params: dict[str, float]):
480481
logging_warning(_("Parameter %s not found in the current parameter file"), param)
481482
return ret
482483

483-
def write_last_uploaded_filename(self, current_file: str):
484+
def write_last_uploaded_filename(self, current_file: str) -> None:
484485
try:
485486
with open(os_path.join(self.vehicle_dir, "last_uploaded_filename.txt"), "w", encoding="utf-8") as file:
486487
file.write(current_file)
@@ -577,7 +578,9 @@ def write_param_default_values(self, param_default_values: dict[str, "Par"]) ->
577578
return True
578579
return False
579580

580-
def write_param_default_values_to_file(self, param_default_values: dict[str, "Par"], filename: str = "00_default.param"):
581+
def write_param_default_values_to_file(
582+
self, param_default_values: dict[str, "Par"], filename: str = "00_default.param"
583+
) -> None:
581584
if self.write_param_default_values(param_default_values):
582585
Par.export_to_param(Par.format_params(self.param_default_dict), os_path.join(self.vehicle_dir, filename))
583586

@@ -622,7 +625,7 @@ def download_file_from_url(url: str, local_filename: str, timeout: int = 5) -> b
622625
return False
623626

624627
@staticmethod
625-
def add_argparse_arguments(parser):
628+
def add_argparse_arguments(parser: ArgumentParser) -> ArgumentParser:
626629
parser.add_argument(
627630
"-t",
628631
"--vehicle-type",

MethodicConfigurator/backend_filesystem_configuration_steps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ class ConfigurationSteps:
3434
configuration_steps (dict): A dictionary containing the configuration steps.
3535
"""
3636

37-
def __init__(self, _vehicle_dir: str, vehicle_type: str):
37+
def __init__(self, _vehicle_dir: str, vehicle_type: str) -> None:
3838
self.configuration_steps_filename = "configuration_steps_" + vehicle_type + ".json"
3939
self.configuration_steps: dict[str, dict] = {}
4040
self.forced_parameters: dict[str, dict] = {}
4141
self.derived_parameters: dict[str, dict] = {}
4242
self.log_loaded_file = False
4343

44-
def re_init(self, vehicle_dir: str, vehicle_type: str):
44+
def re_init(self, vehicle_dir: str, vehicle_type: str) -> None:
4545
if vehicle_type == "":
4646
return
4747
self.configuration_steps_filename = "configuration_steps_" + vehicle_type + ".json"

MethodicConfigurator/backend_filesystem_program_settings.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@ class ProgramSettings:
3737
validation of directory names according to specific rules.
3838
"""
3939

40-
def __init__(self):
40+
def __init__(self) -> None:
4141
pass
4242

4343
@staticmethod
44-
def application_icon_filepath():
44+
def application_icon_filepath() -> str:
4545
script_dir = os_path.dirname(os_path.abspath(__file__))
4646
return os_path.join(script_dir, "ArduPilot_icon.png")
4747

4848
@staticmethod
49-
def application_logo_filepath():
49+
def application_logo_filepath() -> str:
5050
script_dir = os_path.dirname(os_path.abspath(__file__))
5151
return os_path.join(script_dir, "ArduPilot_logo.png")
5252

5353
@staticmethod
54-
def create_new_vehicle_dir(new_vehicle_dir: str):
54+
def create_new_vehicle_dir(new_vehicle_dir: str) -> str:
5555
# Check if the new vehicle directory already exists
5656
if os_path.exists(new_vehicle_dir):
5757
return _("Directory already exists, choose a different one")
@@ -64,7 +64,7 @@ def create_new_vehicle_dir(new_vehicle_dir: str):
6464
return ""
6565

6666
@staticmethod
67-
def valid_directory_name(dir_name: str):
67+
def valid_directory_name(dir_name: str) -> bool:
6868
"""
6969
Check if a given directory name contains only alphanumeric characters, underscores, hyphens,
7070
and the OS directory separator.
@@ -84,7 +84,7 @@ def valid_directory_name(dir_name: str):
8484
return re_match(pattern, dir_name) is not None
8585

8686
@staticmethod
87-
def __user_config_dir():
87+
def __user_config_dir() -> str:
8888
user_config_directory = user_config_dir(".ardupilot_methodic_configurator", False, roaming=True, ensure_exists=True)
8989

9090
if not os_path.exists(user_config_directory):
@@ -97,7 +97,7 @@ def __user_config_dir():
9797
return user_config_directory
9898

9999
@staticmethod
100-
def __site_config_dir():
100+
def __site_config_dir() -> str:
101101
site_config_directory = site_config_dir(
102102
".ardupilot_methodic_configurator", False, version=None, multipath=False, ensure_exists=True
103103
)
@@ -140,14 +140,14 @@ def __get_settings_as_dict() -> dict[str, Any]:
140140
return settings
141141

142142
@staticmethod
143-
def __set_settings_from_dict(settings):
143+
def __set_settings_from_dict(settings) -> None:
144144
settings_path = os_path.join(ProgramSettings.__user_config_dir(), "settings.json")
145145

146146
with open(settings_path, "w", encoding="utf-8") as settings_file:
147147
json_dump(settings, settings_file, indent=4)
148148

149149
@staticmethod
150-
def __get_settings_config():
150+
def __get_settings_config() -> tuple[dict[str, Any], str, str]:
151151
settings = ProgramSettings.__get_settings_as_dict()
152152

153153
# Regular expression pattern to match single backslashes
@@ -158,7 +158,7 @@ def __get_settings_config():
158158
return settings, pattern, replacement
159159

160160
@staticmethod
161-
def store_recently_used_template_dirs(template_dir: str, new_base_dir: str):
161+
def store_recently_used_template_dirs(template_dir: str, new_base_dir: str) -> None:
162162
settings, pattern, replacement = ProgramSettings.__get_settings_config()
163163

164164
# Update the settings with the new values
@@ -172,7 +172,7 @@ def store_recently_used_template_dirs(template_dir: str, new_base_dir: str):
172172
ProgramSettings.__set_settings_from_dict(settings)
173173

174174
@staticmethod
175-
def store_template_dir(relative_template_dir: str):
175+
def store_template_dir(relative_template_dir: str) -> None:
176176
settings, pattern, replacement = ProgramSettings.__get_settings_config()
177177

178178
template_dir = os_path.join(ProgramSettings.get_templates_base_dir(), relative_template_dir)
@@ -183,7 +183,7 @@ def store_template_dir(relative_template_dir: str):
183183
ProgramSettings.__set_settings_from_dict(settings)
184184

185185
@staticmethod
186-
def store_recently_used_vehicle_dir(vehicle_dir: str):
186+
def store_recently_used_vehicle_dir(vehicle_dir: str) -> None:
187187
settings, pattern, replacement = ProgramSettings.__get_settings_config()
188188

189189
# Update the settings with the new values
@@ -192,7 +192,7 @@ def store_recently_used_vehicle_dir(vehicle_dir: str):
192192
ProgramSettings.__set_settings_from_dict(settings)
193193

194194
@staticmethod
195-
def get_templates_base_dir():
195+
def get_templates_base_dir() -> str:
196196
current_dir = os_path.dirname(os_path.abspath(__file__))
197197
if platform_system() == "Windows":
198198
site_directory = ProgramSettings.__site_config_dir()
@@ -209,7 +209,7 @@ def get_templates_base_dir():
209209
return os_path.join(site_directory, "vehicle_templates")
210210

211211
@staticmethod
212-
def get_recently_used_dirs():
212+
def get_recently_used_dirs() -> tuple[str, str, str]:
213213
template_default_dir = os_path.join(
214214
ProgramSettings.get_templates_base_dir(), "ArduCopter", "diatone_taycan_mxc", "4.5.x-params"
215215
)
@@ -232,7 +232,7 @@ def display_usage_popup(ptype: str) -> bool:
232232
return bool(display_usage_popup_settings.get(ptype, True))
233233

234234
@staticmethod
235-
def set_display_usage_popup(ptype: str, value: bool):
235+
def set_display_usage_popup(ptype: str, value: bool) -> None:
236236
if ptype in {"component_editor", "parameter_editor"}:
237237
settings, _, _ = ProgramSettings.__get_settings_config()
238238
settings["display_usage_popup"][ptype] = value

MethodicConfigurator/backend_filesystem_vehicle_components.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class VehicleComponents:
3333
vehicle components configurations from a JSON file.
3434
"""
3535

36-
def __init__(self):
36+
def __init__(self) -> None:
3737
self.vehicle_components_json_filename = "vehicle_components.json"
3838
self.vehicle_components = None
3939

@@ -89,11 +89,11 @@ def get_fc_fw_version_from_vehicle_components_json(self) -> str:
8989
return ""
9090

9191
@staticmethod
92-
def supported_vehicles():
92+
def supported_vehicles() -> list[str]:
9393
return ["AP_Periph", "AntennaTracker", "ArduCopter", "ArduPlane", "ArduSub", "Blimp", "Heli", "Rover", "SITL"]
9494

9595
@staticmethod
96-
def get_vehicle_components_overviews():
96+
def get_vehicle_components_overviews() -> dict[str, TemplateOverview]:
9797
"""
9898
Finds all subdirectories of base_dir containing a "vehicle_components.json" file,
9999
creates a dictionary where the keys are the subdirectory names (relative to base_dir)

0 commit comments

Comments
 (0)