From 8dfa8180544546ec783f11448197b09c1e718fb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20NEDJAR?= Date: Sat, 28 Mar 2026 19:52:12 +0100 Subject: [PATCH 1/2] tooling: Enable ruff SIM (simplify) rules. --- lib/apds9960/apds9960/device.py | 12 ++++-------- lib/bq27441/bq27441/device.py | 5 +---- lib/mcp23009e/examples/buttons.py | 2 +- pyproject.toml | 4 +++- tests/test_examples.py | 12 +++++------- 5 files changed, 14 insertions(+), 21 deletions(-) diff --git a/lib/apds9960/apds9960/device.py b/lib/apds9960/apds9960/device.py index 7a0a7838..202707c9 100644 --- a/lib/apds9960/apds9960/device.py +++ b/lib/apds9960/apds9960/device.py @@ -101,10 +101,7 @@ def set_mode(self, mode, enable=True): # change bit(s) in ENABLE register */ if mode == APDS9960_MODE_ALL: - if enable: - reg_val = 0x7F - else: - reg_val = 0x00 + reg_val = 0x7F if enable else 0x00 else: if enable: reg_val |= 1 << mode @@ -199,10 +196,9 @@ def gesture(self): self.gesture_data_.total_gestures += 1 # filter and process gesture data, decode near/far state - if self.process_gesture_data(): - if self.decode_gesture(): - # ***TODO: U-Turn Gestures - pass + if self.process_gesture_data() and self.decode_gesture(): + # ***TODO: U-Turn Gestures + pass # reset data self.gesture_data_.index = 0 diff --git a/lib/bq27441/bq27441/device.py b/lib/bq27441/bq27441/device.py index cba16a54..2242704e 100644 --- a/lib/bq27441/bq27441/device.py +++ b/lib/bq27441/bq27441/device.py @@ -491,10 +491,7 @@ def execute_control_word(self, function): sub_command_msb = function >> 8 sub_command_lsb = function & 0x00FF command = [sub_command_lsb, sub_command_msb] - if self._write_reg(0, command, 2): - return True - - return False + return bool(self._write_reg(0, command, 2)) # Extended Data Cmds # Issue a block_data_control() command to enable block data access diff --git a/lib/mcp23009e/examples/buttons.py b/lib/mcp23009e/examples/buttons.py index 760a0c37..c55256a6 100644 --- a/lib/mcp23009e/examples/buttons.py +++ b/lib/mcp23009e/examples/buttons.py @@ -31,7 +31,7 @@ } print("Configuration des boutons...") -for btn_pin in btn_mapping.keys(): +for btn_pin in btn_mapping: mcp.setup(btn_pin, MCP23009_DIR_INPUT, pullup=MCP23009_PULLUP) print("✓ Configuration terminée\n") diff --git a/pyproject.toml b/pyproject.toml index 7a078723..f314bfea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -61,6 +61,7 @@ select = [ "PYI", # flake8-pyi "RSE", # flake8-raise "RUF", # Ruff-specific rules + "SIM", # flake8-simplify "T10", # flake8-debugger "PERF", # Perflint "T20", # flake8-print: no print() in production code @@ -86,7 +87,8 @@ ignore = [ "PLW2901", # overwriting loop variable "RUF012", # mutable class variable "RUF100", # unused noqa - "SIM101", # merge isinstance calls + "SIM101", # merge isinstance calls (micropython compat) + "SIM105", # contextlib.suppress not available in micropython "W191", # tab-indent, redundant when using formatter ] diff --git a/tests/test_examples.py b/tests/test_examples.py index 45ff5c52..e5bd36e0 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -117,11 +117,10 @@ def test_method_calls_exist_in_driver(self, driver_dir, example_path): return # Covered by test_syntax_valid driver_imports = set() for node in ast.walk(tree): - if isinstance(node, ast.ImportFrom): - if node.module and any( - part in driver_dir.name.replace("-", "_") - for part in (node.module.split(".")[0],) - ): + if isinstance(node, ast.ImportFrom) and node.module and any( + part in driver_dir.name.replace("-", "_") + for part in (node.module.split(".")[0],) + ): for alias in node.names: driver_imports.add(alias.asname or alias.name) @@ -132,8 +131,7 @@ def test_method_calls_exist_in_driver(self, driver_dir, example_path): # is assigned from a driver constructor driver_vars = set() for node in ast.walk(tree): - if isinstance(node, ast.Assign): - if isinstance(node.value, ast.Call): + if isinstance(node, ast.Assign) and isinstance(node.value, ast.Call): func = node.value.func func_name = None if isinstance(func, ast.Name): From abfb68489d61e549d8cd61f2eeff9f9c3d3b16a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20NEDJAR?= Date: Sat, 28 Mar 2026 20:33:39 +0100 Subject: [PATCH 2/2] tooling: Fix indentation after collapsing nested if statements. --- tests/test_examples.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/test_examples.py b/tests/test_examples.py index e5bd36e0..4c2177c5 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -121,8 +121,8 @@ def test_method_calls_exist_in_driver(self, driver_dir, example_path): part in driver_dir.name.replace("-", "_") for part in (node.module.split(".")[0],) ): - for alias in node.names: - driver_imports.add(alias.asname or alias.name) + for alias in node.names: + driver_imports.add(alias.asname or alias.name) if not driver_imports: pytest.skip(f"No driver import found in {example_path.name}") @@ -132,16 +132,16 @@ def test_method_calls_exist_in_driver(self, driver_dir, example_path): driver_vars = set() for node in ast.walk(tree): if isinstance(node, ast.Assign) and isinstance(node.value, ast.Call): - func = node.value.func - func_name = None - if isinstance(func, ast.Name): - func_name = func.id - elif isinstance(func, ast.Attribute): - func_name = func.attr - if func_name in driver_imports: - for target in node.targets: - if isinstance(target, ast.Name): - driver_vars.add(target.id) + func = node.value.func + func_name = None + if isinstance(func, ast.Name): + func_name = func.id + elif isinstance(func, ast.Attribute): + func_name = func.attr + if func_name in driver_imports: + for target in node.targets: + if isinstance(target, ast.Name): + driver_vars.add(target.id) if not driver_vars: pytest.skip(