Skip to content

tooling: Enable ruff SIM (simplify) rules.#295

Merged
nedseb merged 2 commits into
mainfrom
tooling/ruff-sim
Mar 28, 2026
Merged

tooling: Enable ruff SIM (simplify) rules.#295
nedseb merged 2 commits into
mainfrom
tooling/ruff-sim

Conversation

@nedseb
Copy link
Copy Markdown
Contributor

@nedseb nedseb commented Mar 28, 2026

Summary

Enable the SIM (flake8-simplify) rule set in ruff and fix all violations.

Fixes applied

  • SIM108 — ternary operator in apds9960 enable mode
  • SIM102 — collapse nested if statements (apds9960, test_examples ×2)
  • SIM103 — return condition directly in bq27441 execute_control_word
  • SIM118key in dict instead of key in dict.keys() in mcp23009e example

Rules ignored (MicroPython incompatible)

  • SIM101 — merge isinstance calls (already ignored)
  • SIM105contextlib.suppress not available in MicroPython

Closes #287

Test plan

  • make ci passes (196 mock tests + 271 example validations)
  • ruff check passes with SIM enabled

Copilot AI review requested due to automatic review settings March 28, 2026 18:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 SIM rules in pyproject.toml, and ignore SIM105 (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.

Comment thread tests/test_examples.py Outdated
Comment on lines 120 to 125
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)
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread tests/test_examples.py Outdated
Comment on lines 134 to 137
if isinstance(node, ast.Assign) and isinstance(node.value, ast.Call):
func = node.value.func
func_name = None
if isinstance(func, ast.Name):
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
@nedseb
Copy link
Copy Markdown
Contributor Author

nedseb commented Mar 28, 2026

Les deux commentaires Copilot ont été corrigés dans abfb684 :

  1. et 2. Indentation incorrecte après collapse des if imbriqués — les blocs internes étaient sur-indentés d'un niveau. Dé-indenté aux deux endroits dans test_examples.py. CI passe.

@nedseb nedseb merged commit 34a0d74 into main Mar 28, 2026
9 checks passed
@nedseb nedseb deleted the tooling/ruff-sim branch March 28, 2026 19:36
@semantic-release-updater
Copy link
Copy Markdown

🎉 This PR is included in version 0.2.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tooling: Enable ruff SIM (simplify) rules.

2 participants