Skip to content

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

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 can no longer be installed in this repo. The fsouza/autoflake8 hook repository has been deleted from GitHub:

An unexpected error has occurred: CalledProcessError: command: ('/usr/bin/git', 'fetch', 'origin', '--tags')
    remote: Repository not found.
    fatal: repository 'https://github.com/fsouza/autoflake8/' not found

The last CI run, in April, passed. The shared workflow installs hooks fresh whenever it has no cached pre-commit environment, and GitHub evicts caches that go unused for 7 days, so the next CI run will fail the same way.

Changes

  • Hooks: drop black, flake8 (with its plugin pins), isort, pyupgrade and autoflake8 in favour of ruff-check + ruff-format (ruff-pre-commit v0.16.9). debug-statements stays; mirrors-mypy goes from v1.20.1 to v2.3.1.
  • [tool.isort] and [tool.flake8] become [tool.ruff]. The rules are B, C, E, F, I, SIM, UP, W, the set zigpy-znp uses, with max-complexity = 25, plus YTT because flake8-2020 was installed. E501 stays ignored and the tests/* per-file ignores are unchanged. The isort settings carry over.
  • 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 autofixes: import sorting and pyupgrade rewrites for the Python 3.11 floor (typing.Dict → dict, timezone.utc → UTC, str.format → f-strings). The old pyupgrade hook ran without a --pyXX-plus flag, so these had not been applied. By hand: the remaining %-formatting (struct format strings and __str__ methods), a %-formatted LOGGER.exception call → lazy logging arguments, explicit zip(strict=...) keeping the current behaviour, and exception chaining for re-raised errors.

Verification

  • pre-commit run --all-files passes on Python 3.14. On dev it fails while installing hooks.
  • Tests: 47 passed.

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.

Mostly autofixes: import sorting, pyupgrade rewrites for the Python 3.11 floor (typing.Dict -> dict, timezone.utc -> UTC, str.format -> f-strings) and ruff format. By hand: remaining %-formatting (struct format strings and __str__ methods) -> f-strings, a %-formatted LOGGER.exception call -> lazy logging arguments, explicit zip(strict=...) keeping the current behaviour, and exception chaining for re-raised errors.
@codecov

codecov Bot commented Sep 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 21.05263% with 30 lines in your changes missing coverage. Please review.
✅ Project coverage is 46.49%. Comparing base (3d82221) to head (45082be).

Files with missing lines Patch % Lines
zigpy_zigate/tools/flasher.py 0.00% 20 Missing ⚠️
zigpy_zigate/api.py 44.44% 5 Missing ⚠️
zigpy_zigate/types.py 33.33% 4 Missing ⚠️
zigpy_zigate/zigbee/application.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #167      +/-   ##
==========================================
+ Coverage   46.46%   46.49%   +0.03%     
==========================================
  Files           8        8              
  Lines        1300     1299       -1     
==========================================
  Hits          604      604              
+ Misses        696      695       -1     

☔ 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