Skip to content

Replace black, isort, flake8 and pyupgrade with ruff - #278

Draft
zigpy-review-bot wants to merge 2 commits into
zigpy:devfrom
zigpy-review-bot:zigpy-bot/migrate-to-ruff
Draft

zigpy-review-bot wants to merge 2 commits into
zigpy:devfrom
zigpy-review-bot:zigpy-bot/migrate-to-ruff

Conversation

@zigpy-review-bot

Copy link
Copy Markdown
Collaborator

Problem

pre-commit run fails on Python 3.14. The pinned pyupgrade v3.3.1 crashes on import with AttributeError: module 'ast' has no attribute 'Str': 3.14 removed the long-deprecated ast.Str alias, and the first unaffected pyupgrade is v3.4.0. This is the same problem zigpy/bellows#759 fixed in bellows. CI does not catch it because the shared pre-commit job runs on Python 3.11.

Changes

Rather than bumping pyupgrade alone, this replaces the black / isort / flake8 / autoflake / pyupgrade stack with ruff.

  • Hooks: ruff-check + ruff-format (ruff-pre-commit v0.16.9). codespell goes from v2.3.0 to v2.4.3.
  • [tool.isort] and [tool.flake8] become [tool.ruff]. The rules are B, C, E, F, I, SIM, UP, W, the set zigpy-znp uses, plus D with the pep257 convention and the same docstring ignores that flake8-docstrings had. E501 stays ignored and the tests/* per-file ignores carry over.
  • target-version = "py38" matches requires-python = ">=3.8". That floor means SIM117 has to be ignored in tests/*: merging those nested with statements needs parenthesized context managers, which are not Python 3.8 syntax.
  • requirements_test.txt: drop isort, black, flake8 and Flake8-pyproject, and bump ruff==0.0.261 to ruff==0.16.9 to match the hook.
  • Remove the unused flake8 problem matcher. The shared CI workflow only loads python.json.
  • Second commit: the resulting fixes. Mostly ruff format output (lowercase hex escapes, joined implicit string concatenations) plus a few small test rewrites. By hand: exception chaining for re-raised errors, SIM103 in is_usb_serial_port, merging two identical branches in DeconzAddressEndpoint.serialize (SIM114), and dropping the dead Python < 3.8 branch in tests/async_mock.py.

Not changed here

requires-python = ">=3.8" and zigpy>=0.80.0 look stale. zigpy 0.80.0 itself requires Python 3.9, and the CI matrix is 3.11 to 3.14. Raising the floor would let ruff apply the 3.11 rewrites and drop the SIM117 exception, but it is a packaging change, so it is left for a separate decision.

Verification

  • pre-commit run --all-files passes on Python 3.14. On dev the pyupgrade hook crashes.
  • Tests: 140 passed, the same as on dev.

Context

This is one of a set of draft PRs that align the pre-commit setup of the zigpy libraries on ruff (ruff check + ruff format), which zigpy, zha, zha-device-handlers, zigpy-znp, zigpy-ota and zigpy-ziggurat already use. The same change is proposed for bellows, zigpy-deconz, zigpy-xbee, zigpy-zigate and zigpy-cli, plus small ruff config fixes in zigpy and zigpy-ziggurat.

The commits are kept separate so parts can be dropped or moved into their own PR. Maintainers can ask the bot for that, or for any other change, by commenting @zigpy-review-bot <request> on this PR.

Autofixes: ruff format output (lowercase hex escapes, joined implicit string concatenations, blank line after module docstrings), C416, B011 and single-line merges of nested `with` statements. By hand: exception chaining for re-raised errors, SIM103 in is_usb_serial_port, SIM114 merging two identical branches in DeconzAddressEndpoint.serialize, and dropping the dead Python < 3.8 branch in tests/async_mock.py (UP036). SIM117 is ignored in tests where merging the `with` statements needs parenthesized context managers, which requires-python >= 3.8 does not allow.
@codecov

codecov Bot commented Sep 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.13%. Comparing base (7547a5e) to head (87e18a1).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #278      +/-   ##
==========================================
- Coverage   99.14%   99.13%   -0.01%     
==========================================
  Files           7        7              
  Lines         933      929       -4     
==========================================
- Hits          925      921       -4     
  Misses          8        8              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@zigpy-review-bot zigpy-review-bot added the code quality Improvement to code quality label Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code quality Improvement to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant