tooling: Enable ruff A, B, E722, and PERF rules.#294
Conversation
There was a problem hiding this comment.
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.tomlRuff 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"): |
There was a problem hiding this comment.
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.
| 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"] |
| @@ -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 | |||
There was a problem hiding this comment.
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.
| # 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 |
| 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"), |
There was a problem hiding this comment.
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.
| # manifest.py files are evaluated with some global names pre-defined | ||
| "**/manifest.py" = ["F821"] | ||
| "**/examples/*.py" = ["T20", "N806"] | ||
| "**/examples/*.py" = ["T20", "N806", "PERF203"] |
There was a problem hiding this comment.
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.
| "**/examples/*.py" = ["T20", "N806", "PERF203"] | |
| "**/examples/*.py" = ["T20", "N806"] | |
| "lib/mcp23009e/examples/i2c_scan.py" = ["PERF203"] |
|
Commentaires Copilot traités dans 0735ba9 :
|
|
🎉 This PR is included in version 0.2.3 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Summary
Enable 4 new ruff rule sets and fix all violations:
Rules enabled
Fixes applied
id→device_idin apds9960 exception (A002)format→fmtin bq27441 read_word (A002)dir→directionin ism330dl example (A001)lambda: accel→lambda a=accel: ato bind loop variable (B023)for i in→for _ infor unused loop variables (B007 ×2)raise ... from errfor exception chaining (B904)Rules investigated but not enabled
Closes #286, closes #288, closes #290, closes #292
Test plan
make cipasses (196 mock tests + 271 example validations)ruff checkpasses with all new rules