Skip to content

tooling: Enable ruff preview rules for comment and operator spacing. #342

@nedseb

Description

@nedseb

Problem

The PR #337 contained several PEP 8 violations that ruff did not catch:

  • `#buzzer initialisation` — missing space after `#` (E265)
  • `buzzer_ch.pulse_width_percent(0) #comment` — missing two spaces before inline comment (E261)
  • `1500- (distance * 5)` — missing space around operator (E225)

These were caught during manual code review but should have been flagged by the linter.

Root cause

These rules are in ruff preview mode and require `preview = true` in the ruff configuration to be active:

Rule Description Status
E265 No space after block comment `#` Preview
E261 At least two spaces before inline comment Preview
E262 Inline comment must start with `# ` Preview
E225 Missing whitespace around operator Preview

Our `pyproject.toml` selects `"E"` but ruff only enables stable E-rules by default. The preview rules are silently skipped.

Proposed fix

Add `preview = true` to the ruff lint configuration in `pyproject.toml`:

```toml
[tool.ruff.lint]
preview = true
select = [
...existing rules...
]
```

Impact assessment needed

Enabling preview mode activates all preview rules in the selected groups, not just E265/E261/E262/E225. Before merging:

  1. Run `ruff check --preview` on the full codebase to see how many new violations appear
  2. Add any false-positive or MicroPython-incompatible preview rules to the `ignore` list
  3. Fix or auto-fix the legitimate violations

Alternative: select specific preview rules

If enabling full preview mode causes too many new violations, an alternative is to explicitly select just the comment spacing rules and enable preview only for those:

```toml
[tool.ruff.lint]
preview = true
explicit-preview-rules = true
select = [
...existing rules...,
"E225",
"E261",
"E262",
"E265",
]
```

With `explicit-preview-rules = true`, only the preview rules explicitly listed in `select` are activated — the rest stay off.

Tasks

  • Run `ruff check --preview` on the full codebase and assess impact
  • Choose between full preview or `explicit-preview-rules`
  • Add new rules and fix existing violations
  • Update CONTRIBUTING.md linting section if needed

Related

Metadata

Metadata

Assignees

Labels

releasedtoolingDeveloper tooling, build system, hooks

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions