Skip to content

Commit 2cc9d59

Browse files
committed
fix(ruff): fix lint errors found by the latest ruff
1 parent 8827b33 commit 2cc9d59

File tree

7 files changed

+36
-26
lines changed

7 files changed

+36
-26
lines changed

ardupilot_methodic_configurator/annotate_params.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ def __eq__(self, other: object) -> bool:
157157
return self.value == other.value and self.comment == other.comment
158158
return False
159159

160+
def __hash__(self) -> int:
161+
"""Hash operation for using Par objects in sets and as dict keys."""
162+
return hash((self.value, self.comment))
163+
160164
@staticmethod
161165
def load_param_file_into_dict(param_file: str) -> dict[str, "Par"]:
162166
"""
@@ -319,8 +323,7 @@ def export_to_param(formatted_params: list[str], filename_out: str) -> None:
319323
try:
320324
# Ensure newline character is LF, even on windows
321325
with open(filename_out, "w", encoding="utf-8", newline="\n") as output_file:
322-
for line in formatted_params:
323-
output_file.write(line + "\n")
326+
output_file.writelines(line + "\n" for line in formatted_params)
324327
except OSError as e:
325328
msg = f"ERROR: writing to file {filename_out}: {e}"
326329
raise SystemExit(msg) from e

ardupilot_methodic_configurator/backend_filesystem.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,7 @@ def copy_template_files_to_new_vehicle_dir(
511511
with open(source, encoding="utf-8") as file:
512512
lines = file.readlines()
513513
with open(dest, "w", encoding="utf-8") as file:
514-
for line in lines:
515-
file.write(line.split("#")[0].strip() + "\n")
514+
file.writelines(line.split("#")[0].strip() + "\n" for line in lines)
516515
elif os_path.isdir(source):
517516
shutil_copytree(source, dest)
518517
else:
@@ -742,7 +741,7 @@ def get_upload_local_and_remote_filenames(self, selected_file: str) -> tuple[str
742741
def get_git_commit_hash() -> str:
743742
# Try to get git hash using git command
744743
try:
745-
result = run( # noqa: S603
744+
result = run(
746745
["git", "rev-parse", "HEAD"], # noqa: S607
747746
capture_output=True,
748747
text=True,

extract_missing_translations.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ def output_to_files(missing_translations: list[tuple[int, str]], output_file_bas
211211

212212
# Write untranslated msgids along with their indices to the output file
213213
with open(current_output_file, "w", encoding="utf-8") as f:
214-
for index, item in missing_translations[start_index:end_index]:
215-
f.write(f"{index}:{item}\n")
214+
f.writelines(f"{index}:{item}\n" for index, item in missing_translations[start_index:end_index])
216215

217216

218217
def main() -> None:

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ indent-width = 4
211211
"ardupilot_methodic_configurator/backend_mavftp.py" = ["PGH004", "N801", "ANN001"]
212212
"ardupilot_methodic_configurator/backend_mavftp_example.py" = ["ANN001"]
213213
"ardupilot_methodic_configurator/tempcal_imu.py" = ["ANN001"]
214+
"ardupilot_methodic_configurator/annotate_params.py" = ["PLC0415"]
215+
"ardupilot_methodic_configurator/frontend_tkinter_component_editor_base.py" = ["PLC0415"]
216+
"ardupilot_methodic_configurator/frontend_tkinter_pair_tuple_combobox.py" = ["PLC0415"]
214217

215218
[tool.ruff.lint.mccabe]
216219
max-complexity = 22 # Default is 10

scripts/crawl_ardupilot_wiki.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,22 +200,19 @@ def main() -> None:
200200
except (requests.RequestException, requests.Timeout) as e:
201201
logging.error("Network error crawling %s: %s", current_url, str(e))
202202
broken_urls.add(current_url)
203-
if current_url in visited_urls:
204-
visited_urls.remove(current_url)
203+
visited_urls.discard(current_url)
205204
except (KeyError, ValueError) as e:
206205
logging.error("URL processing error for %s: %s", current_url, str(e))
207206
broken_urls.add(current_url)
208-
if current_url in visited_urls:
209-
visited_urls.remove(current_url)
207+
visited_urls.discard(current_url)
210208
output_urls(visited_urls, broken_urls, start_time)
211209

212210

213211
def output_urls(visited_urls: set[str], broken_urls: set[str], start_time: float) -> None:
214212
# Write all html URLs to file
215213
raw_pages = len(visited_urls)
216214
with open("gurubase.io_url_list_raw.txt", "w", encoding="utf-8") as f:
217-
for url in sorted(visited_urls):
218-
f.write(f"{url}\n") # Output to file
215+
f.writelines(f"{url}\n" for url in sorted(visited_urls)) # Output to file
219216

220217
visited_urls -= set(URL_BLACKLIST)
221218
dedup_urls = remove_duplicates(visited_urls)

tests/test_backend_filesystem.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -951,17 +951,28 @@ def test_copy_template_files_with_blank_change_reason( # pylint: disable=too-ma
951951
mock_isdir.side_effect = lambda path: path.endswith("dir1")
952952

953953
lfs = LocalFilesystem(
954-
"vehicle_dir", "vehicle_type", None, allow_editing_template_files=False, save_component_to_system_templates=False
954+
"vehicle_dir",
955+
"vehicle_type",
956+
"4.5.0",
957+
allow_editing_template_files=False,
958+
save_component_to_system_templates=False,
955959
)
956960

957961
# Test with blank_change_reason=True
958962
lfs.copy_template_files_to_new_vehicle_dir("template_dir", "new_vehicle_dir", blank_change_reason=True)
959963

960964
# Verify file handling when blank_change_reason=True
961-
write_calls = mock_open_file().write.call_args_list
962-
# Check that comments were removed for param files
963-
assert any(call[0][0] == "PARAM1,1.0\n" for call in write_calls)
964-
assert any(call[0][0] == "PARAM2,2.0\n" for call in write_calls)
965+
# First check if writelines was called at all
966+
assert mock_open_file().writelines.called, "writelines should have been called for .param files"
967+
968+
# Get the arguments passed to writelines
969+
writelines_calls = mock_open_file().writelines.call_args_list
970+
assert len(writelines_calls) > 0, "Expected at least one writelines call"
971+
972+
# Convert the generator to list to check content
973+
lines_written = list(writelines_calls[0][0][0]) # Convert generator to list
974+
assert "PARAM1,1.0\n" in lines_written, f"Expected 'PARAM1,1.0\\n' in {lines_written}"
975+
assert "PARAM2,2.0\n" in lines_written, f"Expected 'PARAM2,2.0\\n' in {lines_written}"
965976

966977
# Verify directory was copied with copytree
967978
mock_copytree.assert_called_with("template_dir/dir1", "new_vehicle_dir/dir1")
@@ -1055,10 +1066,8 @@ def test_merge_forced_or_derived_parameters_none_parameters() -> None:
10551066
"vehicle_dir", "vehicle_type", None, allow_editing_template_files=False, save_component_to_system_templates=False
10561067
)
10571068
test_file = "test.json"
1058-
lfs.file_parameters = {test_file: {}}
1059-
1060-
# Test with None parameters
1061-
lfs.merge_forced_or_derived_parameters(test_file, None, [])
1069+
lfs.file_parameters = {test_file: {}} # Test with empty dict instead of None (None is not valid type)
1070+
lfs.merge_forced_or_derived_parameters(test_file, {}, [])
10621071
assert lfs.file_parameters[test_file] == {}
10631072

10641073
# Test with empty dict

tests/test_battery_cell_voltages.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212

1313
import unittest
14-
from math import nan
14+
from math import isnan
1515

1616
from ardupilot_methodic_configurator.battery_cell_voltages import BatteryCell, battery_cell_voltages
1717

@@ -34,15 +34,15 @@ def test_limit_min_voltage(self) -> None:
3434

3535
def test_recommended_max_voltage(self) -> None:
3636
assert BatteryCell.recommended_max_voltage("LiIon") == 4.1
37-
assert BatteryCell.recommended_max_voltage("NonExistentChemistry") is nan
37+
assert isnan(BatteryCell.recommended_max_voltage("NonExistentChemistry"))
3838

3939
def test_recommended_low_voltage(self) -> None:
4040
assert BatteryCell.recommended_low_voltage("LiIon") == 3.1
41-
assert BatteryCell.recommended_low_voltage("NonExistentChemistry") is nan
41+
assert isnan(BatteryCell.recommended_low_voltage("NonExistentChemistry"))
4242

4343
def test_recommended_crit_voltage(self) -> None:
4444
assert BatteryCell.recommended_crit_voltage("LiIon") == 2.8
45-
assert BatteryCell.recommended_crit_voltage("NonExistentChemistry") is nan
45+
assert isnan(BatteryCell.recommended_crit_voltage("NonExistentChemistry"))
4646

4747
def test_voltage_monoticity(self) -> None:
4848
for chemistry in BatteryCell.chemistries():

0 commit comments

Comments
 (0)