Skip to content

Commit 34a0d74

Browse files
authored
tooling: Enable ruff SIM (simplify) rules. (#295)
* tooling: Enable ruff SIM (simplify) rules. * tooling: Fix indentation after collapsing nested if statements.
1 parent 028496f commit 34a0d74

5 files changed

Lines changed: 26 additions & 33 deletions

File tree

lib/apds9960/apds9960/device.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ def set_mode(self, mode, enable=True):
101101

102102
# change bit(s) in ENABLE register */
103103
if mode == APDS9960_MODE_ALL:
104-
if enable:
105-
reg_val = 0x7F
106-
else:
107-
reg_val = 0x00
104+
reg_val = 0x7F if enable else 0x00
108105
else:
109106
if enable:
110107
reg_val |= 1 << mode
@@ -199,10 +196,9 @@ def gesture(self):
199196
self.gesture_data_.total_gestures += 1
200197

201198
# filter and process gesture data, decode near/far state
202-
if self.process_gesture_data():
203-
if self.decode_gesture():
204-
# ***TODO: U-Turn Gestures
205-
pass
199+
if self.process_gesture_data() and self.decode_gesture():
200+
# ***TODO: U-Turn Gestures
201+
pass
206202

207203
# reset data
208204
self.gesture_data_.index = 0

lib/bq27441/bq27441/device.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,7 @@ def execute_control_word(self, function):
491491
sub_command_msb = function >> 8
492492
sub_command_lsb = function & 0x00FF
493493
command = [sub_command_lsb, sub_command_msb]
494-
if self._write_reg(0, command, 2):
495-
return True
496-
497-
return False
494+
return bool(self._write_reg(0, command, 2))
498495

499496
# Extended Data Cmds
500497
# Issue a block_data_control() command to enable block data access

lib/mcp23009e/examples/buttons.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
}
3232

3333
print("Configuration des boutons...")
34-
for btn_pin in btn_mapping.keys():
34+
for btn_pin in btn_mapping:
3535
mcp.setup(btn_pin, MCP23009_DIR_INPUT, pullup=MCP23009_PULLUP)
3636
print("✓ Configuration terminée\n")
3737

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ select = [
6161
"PYI", # flake8-pyi
6262
"RSE", # flake8-raise
6363
"RUF", # Ruff-specific rules
64+
"SIM", # flake8-simplify
6465
"T10", # flake8-debugger
6566
"PERF", # Perflint
6667
"T20", # flake8-print: no print() in production code
@@ -86,7 +87,8 @@ ignore = [
8687
"PLW2901", # overwriting loop variable
8788
"RUF012", # mutable class variable
8889
"RUF100", # unused noqa
89-
"SIM101", # merge isinstance calls
90+
"SIM101", # merge isinstance calls (micropython compat)
91+
"SIM105", # contextlib.suppress not available in micropython
9092
"W191", # tab-indent, redundant when using formatter
9193
]
9294

tests/test_examples.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,12 @@ def test_method_calls_exist_in_driver(self, driver_dir, example_path):
117117
return # Covered by test_syntax_valid
118118
driver_imports = set()
119119
for node in ast.walk(tree):
120-
if isinstance(node, ast.ImportFrom):
121-
if node.module and any(
122-
part in driver_dir.name.replace("-", "_")
123-
for part in (node.module.split(".")[0],)
124-
):
125-
for alias in node.names:
126-
driver_imports.add(alias.asname or alias.name)
120+
if isinstance(node, ast.ImportFrom) and node.module and any(
121+
part in driver_dir.name.replace("-", "_")
122+
for part in (node.module.split(".")[0],)
123+
):
124+
for alias in node.names:
125+
driver_imports.add(alias.asname or alias.name)
127126

128127
if not driver_imports:
129128
pytest.skip(f"No driver import found in {example_path.name}")
@@ -132,18 +131,17 @@ def test_method_calls_exist_in_driver(self, driver_dir, example_path):
132131
# is assigned from a driver constructor
133132
driver_vars = set()
134133
for node in ast.walk(tree):
135-
if isinstance(node, ast.Assign):
136-
if isinstance(node.value, ast.Call):
137-
func = node.value.func
138-
func_name = None
139-
if isinstance(func, ast.Name):
140-
func_name = func.id
141-
elif isinstance(func, ast.Attribute):
142-
func_name = func.attr
143-
if func_name in driver_imports:
144-
for target in node.targets:
145-
if isinstance(target, ast.Name):
146-
driver_vars.add(target.id)
134+
if isinstance(node, ast.Assign) and isinstance(node.value, ast.Call):
135+
func = node.value.func
136+
func_name = None
137+
if isinstance(func, ast.Name):
138+
func_name = func.id
139+
elif isinstance(func, ast.Attribute):
140+
func_name = func.attr
141+
if func_name in driver_imports:
142+
for target in node.targets:
143+
if isinstance(target, ast.Name):
144+
driver_vars.add(target.id)
147145

148146
if not driver_vars:
149147
pytest.skip(

0 commit comments

Comments
 (0)