Skip to content

tooling: Enable ruff A, B, E722, and PERF rules.#294

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

tooling: Enable ruff A, B, E722, and PERF rules.#294
nedseb merged 2 commits into
mainfrom
tooling/ruff-e722-perf

Conversation

@nedseb
Copy link
Copy Markdown
Contributor

@nedseb nedseb commented Mar 28, 2026

Summary

Enable 4 new ruff rule sets and fix all violations:

Rules enabled

  • A (flake8-builtins): prevent shadowing Python builtins
  • B (flake8-bugbear): catch common bug patterns
  • E722: re-enabled bare except detection (was ignored, 0 violations)
  • PERF (Perflint): detect performance anti-patterns

Fixes applied

  • iddevice_id in apds9960 exception (A002)
  • formatfmt in bq27441 read_word (A002)
  • dirdirection in ism330dl example (A001)
  • lambda: accellambda a=accel: a to bind loop variable (B023)
  • for i infor _ in for unused loop variables (B007 ×2)
  • raise ... from err for exception chaining (B904)
  • PERF203 ignored in examples (try/except in I2C bus scan is intentional)

Rules investigated but not enabled

Closes #286, closes #288, closes #290, closes #292

Test plan

  • make ci passes (196 mock tests + 271 example validations)
  • ruff check passes with all new rules

Copilot AI review requested due to automatic review settings March 28, 2026 17:40
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 additional Ruff lint rule sets (A, B, E722, PERF) and updates code/examples to resolve newly-detected violations across the repository.

Changes:

  • Updated pyproject.toml Ruff configuration to select A, B, and PERF rules, and to stop ignoring E722.
  • Refactored a few drivers/tests to avoid builtin-shadowing and improve exception chaining.
  • Cleaned up examples to address bugbear/perf warnings (unused loop variables, closure binding, builtin-shadowing).

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/runner/mpremote_bridge.py Adds exception chaining for JSON decode failures.
pyproject.toml Enables new Ruff rule sets and adjusts per-file ignores for examples.
lib/ssd1327/examples/random_pixels.py Fixes unused loop variable warning.
lib/ssd1327/examples/framebuf_scroll.py Fixes unused loop variable warning.
lib/lis2mdl/examples/tilt_compensated_heading.py Binds loop variable in lambda to satisfy bugbear closure rule.
lib/ism330dl/examples/static_orientation.py Renames dir to avoid builtin shadowing.
lib/bq27441/bq27441/device.py Renames format parameter to avoid builtin shadowing.
lib/apds9960/apds9960/exceptions.py Renames id parameter to avoid builtin shadowing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


# Read a 16 - bit command word from the BQ27441-G1A, def format = little endian int16
def read_word(self, sub_address, format="<h"):
def read_word(self, sub_address, fmt="<h"):
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.

read_word() renamed its second parameter from format to fmt. This avoids shadowing the builtin, but it is a breaking change for any callers using the format= keyword. Consider keeping backward compatibility (e.g., accept format via **kwargs/alias with a deprecation path) or making this an explicitly private helper if it’s not intended as public API.

Suggested change
def read_word(self, sub_address, fmt="<h"):
def read_word(self, sub_address, fmt="<h", **kwargs):
# Backward compatibility: accept legacy 'format=' keyword argument
if "format" in kwargs:
fmt = kwargs["format"]

Copilot uses AI. Check for mistakes.
Comment thread lib/bq27441/bq27441/device.py Outdated
@@ -470,9 +470,9 @@ def soft_reset(self):
return self.execute_control_word(BQ27441_CONTROL_SOFT_RESET)

# Read a 16 - bit command word from the BQ27441-G1A, def format = little endian int16
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.

The comment above read_word() still refers to the parameter name format, but the argument is now called fmt. Update the comment to match the new signature to avoid confusing readers.

Suggested change
# Read a 16 - bit command word from the BQ27441-G1A, def format = little endian int16
# Read a 16-bit command word from the BQ27441-G1A, default fmt = little-endian int16

Copilot uses AI. Check for mistakes.
Comment on lines 1 to +6
class APDS9960InvalidDevId(ValueError):
def __init__(self, id, valid_ids):
def __init__(self, device_id, valid_ids):
Exception.__init__(
self,
"Device id 0x{} is not a valid one (valid: {})!".format(
format(id, "02x"),
format(device_id, "02x"),
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.

APDS9960InvalidDevId.__init__ renamed the parameter from id to device_id. If any external code instantiates this exception using the id= keyword, it will now fail. Consider accepting the old keyword as an alias (with a deprecation note) or documenting this as an intentional API break.

Copilot uses AI. Check for mistakes.
Comment thread pyproject.toml Outdated
# manifest.py files are evaluated with some global names pre-defined
"**/manifest.py" = ["F821"]
"**/examples/*.py" = ["T20", "N806"]
"**/examples/*.py" = ["T20", "N806", "PERF203"]
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.

PERF203 is now ignored for all **/examples/*.py. Since the known violation is localized (e.g. lib/mcp23009e/examples/i2c_scan.py has a try/except inside a loop), consider scoping the ignore to just that file (or adding an inline # noqa: PERF203) so future examples can still benefit from PERF203 feedback.

Suggested change
"**/examples/*.py" = ["T20", "N806", "PERF203"]
"**/examples/*.py" = ["T20", "N806"]
"lib/mcp23009e/examples/i2c_scan.py" = ["PERF203"]

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

nedseb commented Mar 28, 2026

Commentaires Copilot traités dans 0735ba9 :

  1. read_word comment obsolète — mis à jour pour refléter le nouveau nom fmt.
  2. read_word format→fmt breaking — non modifié. Vérifié : aucun appel avec format= en keyword dans la codebase. Le paramètre n'est jamais passé par nom.
  3. APDS9960InvalidDevId id→device_id breaking — non modifié. Exception interne, jamais instanciée avec id= en keyword.
  4. PERF203 scope trop large — corrigé. Restreint à lib/mcp23009e/examples/i2c_scan.py uniquement au lieu de tous les exemples.

@nedseb nedseb merged commit 3f2bc44 into main Mar 28, 2026
9 checks passed
@nedseb nedseb deleted the tooling/ruff-e722-perf branch March 28, 2026 17:46
@semantic-release-updater
Copy link
Copy Markdown

🎉 This PR is included in version 0.2.3 🎉

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

2 participants