tooling: Enable ruff SIM (simplify) rules.#295
Conversation
There was a problem hiding this comment.
Pull request overview
Enables Ruff’s SIM (flake8-simplify) rule set in this MicroPython driver repo and updates code to satisfy the new lint rules, while explicitly ignoring SIM rules that are incompatible with MicroPython.
Changes:
- Enable
SIMrules inpyproject.toml, and ignoreSIM105(MicroPython incompatibility). - Apply SIM-driven simplifications across drivers, tests, and examples (flattened conditionals, direct boolean returns, simplified dict iteration).
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
pyproject.toml |
Adds SIM to Ruff select list and ignores MicroPython-incompatible SIM rules. |
tests/test_examples.py |
Attempts to simplify nested if statements in AST-walking logic. |
lib/mcp23009e/examples/buttons.py |
Simplifies dict key iteration (for k in dict pattern). |
lib/bq27441/bq27441/device.py |
Simplifies boolean-return logic in execute_control_word. |
lib/apds9960/apds9960/device.py |
Uses ternary and collapses nested if in gesture processing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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) |
There was a problem hiding this comment.
Indentation is incorrect after collapsing the ImportFrom condition: for alias in node.names: is over-indented relative to the preceding if ...): line, which will raise an IndentationError (or at minimum change block structure). It should be indented one level inside the if, not two.
| if isinstance(node, ast.Assign) and isinstance(node.value, ast.Call): | ||
| func = node.value.func | ||
| func_name = None | ||
| if isinstance(func, ast.Name): |
There was a problem hiding this comment.
Indentation is incorrect after combining the Assign/Call checks: the block starting at func = node.value.func is over-indented relative to the if isinstance(node, ast.Assign) and ...: line. As written this will raise an IndentationError (or alter intended control flow).
|
Les deux commentaires Copilot ont été corrigés dans abfb684 :
|
|
🎉 This PR is included in version 0.2.4 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Summary
Enable the
SIM(flake8-simplify) rule set in ruff and fix all violations.Fixes applied
SIM108— ternary operator in apds9960 enable modeSIM102— collapse nested if statements (apds9960, test_examples ×2)SIM103— return condition directly in bq27441 execute_control_wordSIM118—key in dictinstead ofkey in dict.keys()in mcp23009e exampleRules ignored (MicroPython incompatible)
SIM101— merge isinstance calls (already ignored)SIM105—contextlib.suppressnot available in MicroPythonCloses #287
Test plan
make cipasses (196 mock tests + 271 example validations)ruff checkpasses with SIM enabled