Skip to content

Commit d1c37f9

Browse files
committed
fix(codepilot AI): pylint fixes and fixes suggested by codepilot AI
1 parent da4d887 commit d1c37f9

File tree

5 files changed

+50
-42
lines changed

5 files changed

+50
-42
lines changed

ardupilot_methodic_configurator/data_model_motor_test.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
from ardupilot_methodic_configurator.backend_filesystem_program_settings import ProgramSettings
2323
from ardupilot_methodic_configurator.backend_flightcontroller import FlightController
2424

25+
# pylint: disable=too-many-lines
2526

26-
class MotorTestDataModel: # pylint: disable=too-many-public-methods
27+
28+
class MotorTestDataModel: # pylint: disable=too-many-public-methods, too-many-instance-attributes
2729
"""
2830
Data model for motor test functionality.
2931
@@ -537,7 +539,7 @@ def set_test_duration(self, duration: float) -> bool:
537539
except ValueError as exc:
538540
logging_error(_("Invalid motor test duration setting: %(error)s"), {"error": str(exc)})
539541
return False
540-
except Exception as exc:
542+
except Exception as exc: # pylint: disable=broad-exception-caught
541543
logging_error(_("Failed to save duration setting: %(error)s"), {"error": str(exc)})
542544
return False
543545

@@ -584,7 +586,7 @@ def set_test_throttle_pct(self, throttle_pct: int) -> bool:
584586
except ValueError as exc:
585587
logging_error(_("Invalid motor test throttle percentage setting: %(error)s"), {"error": str(exc)})
586588
return False
587-
except Exception as exc:
589+
except Exception as exc: # pylint: disable=broad-exception-caught
588590
logging_error(_("Failed to save throttle percentage setting: %(error)s"), {"error": str(exc)})
589591
return False
590592

@@ -646,7 +648,7 @@ def update_frame_configuration(self, frame_class: int, frame_type: int) -> tuple
646648
logging_error(error_msg)
647649
return False, error_msg
648650

649-
def get_frame_options(self) -> dict[str, dict[int, str]]:
651+
def get_frame_options(self) -> dict[str, dict[int, str]]: # pylint: disable=too-many-branches
650652
"""
651653
Get all available frame configuration options.
652654
@@ -689,7 +691,7 @@ def get_frame_options(self) -> dict[str, dict[int, str]]:
689691
)
690692

691693
# Fallback: Use doc_dict only if motor data loader didn't provide data
692-
if not frame_options and hasattr(self.filesystem, "doc_dict") and self.filesystem.doc_dict:
694+
if not frame_options and hasattr(self.filesystem, "doc_dict") and self.filesystem.doc_dict: # pylint: disable=too-many-nested-blocks
693695
logging_debug(_("Motor data unavailable, using doc_dict as fallback for frame options"))
694696

695697
# Get FRAME_TYPE options (these contain both class and type info)

ardupilot_methodic_configurator/data_model_vehicle_components_base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ def update_json_structure(self) -> None:
261261

262262
# Merge existing data onto default structure (preserves existing values)
263263
self._data = self._deep_merge_dicts(default_structure, self._data)
264+
self._data["Program version"] = __version__
264265

265266
def _deep_merge_dicts(self, default: dict[str, Any], existing: dict[str, Any]) -> dict[str, Any]:
266267
"""

ardupilot_methodic_configurator/data_model_vehicle_components_import.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# from logging import debug as logging_debug
1212
from logging import error as logging_error
1313
from math import log2
14+
from typing import Any
1415

1516
from ardupilot_methodic_configurator import _
1617
from ardupilot_methodic_configurator.data_model_vehicle_components_base import ComponentDataModelBase
@@ -57,7 +58,9 @@ def _reverse_key_search(
5758
logging_error(_("No values found for %s in the metadata"), param_name)
5859
return fallbacks
5960

60-
def _verify_dict_is_uptodate(self, doc: dict, dict_to_check: dict, doc_key: str, doc_dict: str) -> bool:
61+
def _verify_dict_is_uptodate(
62+
self, doc: dict[str, Any], dict_to_check: dict[str, dict[str, Any]], doc_key: str, doc_dict: str
63+
) -> bool:
6164
"""
6265
Verify that a dictionary is up-to-date with the apm.pdef.xml documentation metadata.
6366
@@ -80,8 +83,8 @@ def _verify_dict_is_uptodate(self, doc: dict, dict_to_check: dict, doc_key: str,
8083

8184
def process_fc_parameters(
8285
self,
83-
fc_parameters: dict,
84-
doc: dict,
86+
fc_parameters: dict[str, Any],
87+
doc: dict[str, Any],
8588
) -> None:
8689
"""
8790
Process flight controller parameters and update the data model accordingly.
@@ -214,7 +217,7 @@ def _set_serial_type_from_fc_parameters(self, fc_parameters: dict) -> bool: # p
214217

215218
return esc >= 2
216219

217-
def _set_esc_type_from_fc_parameters(self, fc_parameters: dict, doc: dict) -> None:
220+
def _set_esc_type_from_fc_parameters(self, fc_parameters: dict[str, Any], doc: dict[str, Any]) -> None:
218221
"""Process ESC parameters and update the data model."""
219222
mot_pwm_type = fc_parameters.get("MOT_PWM_TYPE", 0)
220223
try:
@@ -240,7 +243,7 @@ def _set_esc_type_from_fc_parameters(self, fc_parameters: dict, doc: dict) -> No
240243
protocol = str(MOT_PWM_TYPE_DICT[str(mot_pwm_type)]["protocol"])
241244
self.set_component_value(("ESC", "FC Connection", "Protocol"), protocol)
242245

243-
def _set_battery_type_from_fc_parameters(self, fc_parameters: dict) -> None:
246+
def _set_battery_type_from_fc_parameters(self, fc_parameters: dict[str, Any]) -> None:
244247
"""Process battery monitor parameters and update the data model."""
245248
if "BATT_MONITOR" in fc_parameters:
246249
try:
@@ -258,7 +261,7 @@ def _set_battery_type_from_fc_parameters(self, fc_parameters: dict) -> None:
258261
except (ValueError, KeyError, TypeError) as e:
259262
logging_error(_("Error processing BATT_MONITOR parameter: %s"), str(e))
260263

261-
def _set_motor_poles_from_fc_parameters(self, fc_parameters: dict) -> None:
264+
def _set_motor_poles_from_fc_parameters(self, fc_parameters: dict[str, Any]) -> None:
262265
"""Process motor parameters and update the data model."""
263266
if "MOT_PWM_TYPE" in fc_parameters:
264267
mot_pwm_type_str = str(fc_parameters["MOT_PWM_TYPE"])

ardupilot_methodic_configurator/frontend_tkinter_component_editor_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from logging import info as logging_info
2020
from sys import exit as sys_exit
2121
from tkinter import messagebox, ttk
22-
from typing import Optional, Union, cast
22+
from typing import Any, Optional, Union, cast
2323
from unittest.mock import patch
2424

2525
from ardupilot_methodic_configurator import _, __version__
@@ -324,7 +324,7 @@ def set_vehicle_configuration_template(self, configuration_template: str) -> Non
324324
"""Set the configuration template name in the data."""
325325
self.data_model.set_configuration_template(configuration_template)
326326

327-
def set_values_from_fc_parameters(self, fc_parameters: dict, doc: dict) -> None:
327+
def set_values_from_fc_parameters(self, fc_parameters: dict[str, Any], doc: dict[str, Any]) -> None:
328328
"""
329329
Process flight controller parameters and update the data model.
330330

tests/test_data_model_vehicle_components_base.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ def test_safe_cast_value_none_handling(self, model_with_datatypes) -> None:
770770
771771
GIVEN: A model with type casting capability
772772
WHEN: Casting None values to different datatypes
773-
THEN: Should return appropriate default values (missing lines 134, 137-143)
773+
THEN: Should return appropriate default values
774774
"""
775775
path = ("Battery", "Specifications", "Test")
776776

@@ -800,17 +800,17 @@ def test_safe_cast_value_none_handling_edge_cases(self, model_with_datatypes) ->
800800
801801
GIVEN: A model with type casting capability
802802
WHEN: Casting None values to unusual datatypes
803-
THEN: Should handle gracefully (covers lines 141-143)
803+
THEN: Should handle gracefully
804804
"""
805805
path = ("Battery", "Specifications", "Test")
806806

807-
# Test None to dict - covers line 141
807+
# Test None to dict
808808
result = model_with_datatypes._safe_cast_value(None, dict, path)
809809
assert result == {}
810810

811-
# Test None with unknown datatype - covers line 143 (fallback case)
812-
class CustomType:
813-
pass
811+
# Test None with unknown datatype
812+
class CustomType: # pylint: disable=too-few-public-methods
813+
"""Dummy, just for testing."""
814814

815815
result = model_with_datatypes._safe_cast_value(None, CustomType, path)
816816
assert result == "" # Should return empty string for unknown types
@@ -821,16 +821,16 @@ def test_get_component_datatype_isinstance_comprehensive_coverage(self, model_wi
821821
822822
GIVEN: A model with component datatypes
823823
WHEN: Accessing datatypes with various path scenarios
824-
THEN: Should execute isinstance check paths (covers lines 113-114)
824+
THEN: Should execute isinstance check paths
825825
"""
826826
# Set up component datatypes with both type objects and non-type values
827827
model_with_datatypes._component_datatypes = {
828828
"Battery": {
829829
"Specifications": {
830-
"ValidType": int, # This is a type - line 113
831-
"InvalidType": "not_a_type", # This is not a type - line 114
832-
"AnotherValidType": str, # Another type - line 113
833-
"NonCallable": 42, # Not callable - line 114
830+
"ValidType": int, # This is a type
831+
"InvalidType": "not_a_type", # This is not a type
832+
"AnotherValidType": str, # Another type
833+
"NonCallable": 42, # Not callable
834834
}
835835
}
836836
}
@@ -839,15 +839,15 @@ def test_get_component_datatype_isinstance_comprehensive_coverage(self, model_wi
839839
datatype = model_with_datatypes._get_component_datatype(("Battery", "Specifications", "ValidType"))
840840
assert datatype is int
841841

842-
# Test another valid type (covers line 113 again)
842+
# Test another valid type
843843
datatype = model_with_datatypes._get_component_datatype(("Battery", "Specifications", "AnotherValidType"))
844844
assert datatype is str
845845

846-
# Test invalid type - string (covers line 114: else case)
846+
# Test invalid type - string
847847
datatype = model_with_datatypes._get_component_datatype(("Battery", "Specifications", "InvalidType"))
848848
assert datatype is None
849849

850-
# Test invalid type - number (covers line 114: else case)
850+
# Test invalid type - number
851851
datatype = model_with_datatypes._get_component_datatype(("Battery", "Specifications", "NonCallable"))
852852
assert datatype is None
853853

@@ -857,18 +857,18 @@ def test_safe_cast_value_list_dict_special_handling_direct(self, model_with_data
857857
858858
GIVEN: A model with type casting capability
859859
WHEN: Attempting to cast to list or dict types
860-
THEN: Should execute special handling code and log error (covers lines 152-154)
860+
THEN: Should execute special handling code and log error
861861
"""
862862
path = ("Battery", "Specifications", "TestField")
863863

864-
# Test list datatype - should hit line 152 check, log error, and fall back to _process_value
864+
# Test list datatype - log error, and fall back to _process_value
865865
result = model_with_datatypes._safe_cast_value("some_value", list, path)
866866
assert result == "some_value" # Falls back to _process_value which returns the original value
867867
assert "Failed to cast value" in caplog.text
868868

869869
caplog.clear()
870870

871-
# Test dict datatype - should hit line 152 check, log error, and fall back to _process_value
871+
# Test dict datatype - log error, and fall back to _process_value
872872
result = model_with_datatypes._safe_cast_value("another_value", dict, path)
873873
assert result == "another_value" # Falls back to _process_value which returns the original value
874874
assert "Failed to cast value" in caplog.text
@@ -879,20 +879,22 @@ def test_safe_cast_value_attribute_error_handling(self, model_with_datatypes, ca
879879
880880
GIVEN: A model with type casting capability
881881
WHEN: A callable datatype raises AttributeError during instantiation
882-
THEN: Should catch AttributeError and fall back to _process_value (covers line 159)
882+
THEN: Should catch AttributeError and fall back to _process_value
883883
"""
884884
path = ("Battery", "Specifications", "TestField")
885885

886886
# Create a mock class that raises AttributeError when called
887887
class AttributeErrorType(type):
888+
"""Dummy, just for testing."""
889+
888890
def __call__(cls, *args, **kwargs) -> None:
889891
msg = "Mock AttributeError for testing"
890892
raise AttributeError(msg)
891893

892-
class MockDatatype(metaclass=AttributeErrorType):
893-
pass
894+
class MockDatatype(metaclass=AttributeErrorType): # pylint: disable=too-few-public-methods
895+
"""Dummy, just for testing."""
894896

895-
# This should trigger the AttributeError handling at line 159
897+
# This should trigger the AttributeError handling
896898
result = model_with_datatypes._safe_cast_value("test_value", MockDatatype, path)
897899

898900
# Should log the error and fall back to _process_value
@@ -906,9 +908,9 @@ def test_deep_merge_dicts_recursive_comprehensive(self, model_with_datatypes) ->
906908
907909
GIVEN: A model instance with _deep_merge_dicts method
908910
WHEN: Merging nested dictionaries with various structures
909-
THEN: Should handle all recursive paths (covers lines 259-260)
911+
THEN: Should handle all recursive paths
910912
"""
911-
# Test deep recursive merging (covers line 259: recursive call)
913+
# Test deep recursive merging
912914
default = {
913915
"level1": {
914916
"level2": {
@@ -936,7 +938,7 @@ def test_deep_merge_dicts_recursive_comprehensive(self, model_with_datatypes) ->
936938

937939
result = model_with_datatypes._deep_merge_dicts(default, existing)
938940

939-
# Verify deep recursive merging occurred (line 259)
941+
# Verify deep recursive merging occurred
940942
assert result["level1"]["level2"]["level3"]["default_value"] == "from_default"
941943
assert result["level1"]["level2"]["level3"]["existing_value"] == "from_existing"
942944
assert result["level1"]["level2"]["level3"]["shared_key"] == "existing_shared"
@@ -947,7 +949,7 @@ def test_deep_merge_dicts_recursive_comprehensive(self, model_with_datatypes) ->
947949
assert result["top_level_default"] == "default"
948950
assert result["top_level_existing"] == "existing"
949951

950-
# Test case where existing value is not a dict (covers line 260: else case)
952+
# Test case where existing value is not a dict
951953
default_with_dict = {"mixed_key": {"nested": "should_not_appear"}}
952954

953955
existing_with_string = {
@@ -956,7 +958,7 @@ def test_deep_merge_dicts_recursive_comprehensive(self, model_with_datatypes) ->
956958

957959
result = model_with_datatypes._deep_merge_dicts(default_with_dict, existing_with_string)
958960

959-
# existing value should be preserved (line 260: result[key] = existing[key])
961+
# existing value should be preserved
960962
assert result["mixed_key"] == "string_value"
961963
assert not isinstance(result["mixed_key"], dict)
962964

@@ -966,14 +968,14 @@ def test_process_value_none_and_version_handling(self, model_with_datatypes) ->
966968
967969
GIVEN: A model instance
968970
WHEN: Processing None values and Version fields
969-
THEN: Should handle appropriately (covers line 173)
971+
THEN: Should handle appropriately
970972
"""
971-
# Test None value handling (covers line 173: if value is None)
973+
# Test None value handling
972974
path = ("Battery", "Specifications", "TestField")
973975
result = model_with_datatypes._process_value(path, None)
974976
assert result == ""
975977

976-
# Test Version field special handling (covers version check)
978+
# Test Version field special handling
977979
version_path = ("Battery", "Product", "Version")
978980
result = model_with_datatypes._process_value(version_path, "1.2.3-beta")
979981
assert result == "1.2.3-beta"

0 commit comments

Comments
 (0)