Skip to content

build(ruff): format and sort imports with ruff instead of Black and isort - #610

Merged
jsenecal merged 6 commits into
mainfrom
chore/594-ruff-format
Nov 25, 2026
Merged

jsenecal merged 6 commits into
mainfrom
chore/594-ruff-format

Conversation

@jsenecal

@jsenecal jsenecal commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

CONTRIBUTING.md said to format Python with Black and order imports with isort, and both were configured in pyproject.toml, but CI ran only ruff check ., and ruff's import rule (I001) was in its ignore list. Neither formatting nor import order was enforced, and main had drifted: ruff format would change 24 files, and the import rule (with the heading setting below) flagged 177 import blocks.

Change

Five commits:

  1. build(ruff): format and sort imports with ruff instead of Black and isort: configuration, tooling and docs only.
    • pyproject.toml: I001 is no longer ignored (I was already selected). New [tool.ruff.lint.isort] with known-third-party = ["litestar", "inquirer"] and length-sort = true, and [tool.ruff.lint.isort.import-heading] with isort's headings (standard-library = "Standard Library", third-party = "Third Party", first-party = "Project", local-folder = "Local"). Ruff adds a missing heading above each import section and replaces a wrong one, and ruff check reports a missing one as I001. Line length stays 100 from [tool.ruff]. force-exclude = true makes the existing exclusions (hyperglass/ui, hyperglass/plugins/external, hyperglass/api/examples/*.py, hyperglass/compat/_sshtunnel.py) apply to paths passed explicitly too (editors, hooks), for both ruff check and ruff format. [tool.black], [tool.isort] and the black and isort dev dependencies are removed.
    • uv.lock: drops black, isort and their now-unused dependencies mypy-extensions, pathspec and pytokens. Nothing else changes.
    • taskipy: format runs ruff format hyperglass, lint runs ruff check hyperglass (it was the old ruff hyperglass syntax), sort runs ruff check --select I --fix hyperglass. sort stays because ruff format does not order imports, and ruff check --fix alone would also apply every other fixable rule (fixable = ["ALL"]).
    • CI: the backend job runs ruff format --check . right after ruff check .. The backend job is in the CI gate job's needs, so the required check covers it.
    • CONTRIBUTING.md says Ruff formats, orders imports and adds the section headings, and that CI checks both, with the commands to run locally.
    • hooks.sh is removed. It held only a commented-out isort call and a call to a validate_examples.py that no longer exists, and nothing referenced it.
    • CHANGELOG.md: one entry, see below.
  2. test(reverse-dns): put each parametrized address on its own line. A # noqa: S104 sat at the end of a line holding ten addresses; ruff format puts each address on its own line, which would have left the noqa on 240.0.0.1 and failed ruff check on 0.0.0.0. This commit splits the list by hand in the layout ruff format produces, with the noqa on 0.0.0.0.
  3. style: remove two stray import heading comments. hyperglass/models/main.py had a second, empty # Standard Library above the real one, and hyperglass/util/tests/test_dns_endpoint.py had a # Standard Library with no standard library imports under it. Ruff only adds or replaces the heading directly above an import section, so it leaves such strays alone. Four deleted lines.
  4. style: format with ruff and sort imports: only the output of ruff format . then ruff check --select I --fix ., with ruff 0.15.17 from uv.lock. It reached a fixed point after one pass. 115 files: 24 reformatted; 88 with imports reordered; 190 heading lines added, 43 files that had no headings and 25 that had some but not for every section. No existing heading was removed or replaced.
  5. chore: ignore the ruff reformat in git blame: .git-blame-ignore-revs lists commit 4.

The only isort setting with no ruff equivalent is balanced_wrapping. The rest map to ruff settings or are covered by the formatter or ruff's defaults: import_heading_* → import-heading, known_third_party, length_sort, line_length; profile = "black", multi_line_output = 3, include_trailing_comma, indent, force_single_line = false. skip_glob is covered by [tool.ruff] exclude.

Verification

  • Backend suite (pytest hyperglass --ignore hyperglass/plugins/external -q -p no:cacheprovider -rf, Node 22, same venv): main 4670 passed, 4 skipped; this branch 4670 passed, 4 skipped.

  • AST comparison of every Python file changed by commit 4 against its parent: 27 files have an identical AST; the other 88 are identical once each contiguous run of import statements is normalised (split into single-name imports and sorted, so reordered, merged or split imports compare equal, while an import moved across other code would not); 0 differ otherwise.

  • ruff format --check . and ruff check . pass.

  • Every heading in the tree sits directly above an import of its own section, except in hyperglass/models/api/rfc8522.py, where a # flake8: noqa line (older than this change) sits between # Standard Library and the imports.

  • A file without headings fails ruff check:

    $ ruff check --select I hyperglass/probe.py --diff
    --- hyperglass/probe.py
    +++ hyperglass/probe.py
    @@ -1,9 +1,12 @@
     """Probe."""
    
    +# Standard Library
     import os
    
    +# Third Party
     import pytest
    
    +# Project
     from hyperglass.log import log
    
    $ ruff check --select I --output-format concise hyperglass/probe.py
    hyperglass/probe.py:3:1: I001 [*] Import block is un-sorted or un-formatted
  • ruff format --check hyperglass/compat/_sshtunnel.py skips the file; with --no-force-exclude it would reformat it.

  • All five commits are signed.

Reviewing tips

Review commit 1 closely; it holds every decision. Commits 2 and 3 are small hand edits. Skim commit 4: it is tool output only, and the AST check covers it.

Changelog

One entry at the end of Unreleased / Changed: Python is formatted and its imports ordered with Ruff instead of Black and isort, CI checks both, task format / task sort / task lint run Ruff (with their commands), and Ruff writes and checks the import heading comments. Main (with #608) is merged into this branch; the only conflict was this CHANGELOG entry.

Notes

  • The repository merges by squash, so the hash in .git-blame-ignore-revs will not exist on main after merge. A follow-up PR will replace it with the squash commit's hash and close Use ruff for Python formatting and import order, enforced in CI #594.
  • Open pull requests that touch Python will need to be reformatted after this lands; fix(exceptions): raise the intended error where calls passed bad arguments #569 is one. task format and task sort, or ruff format . and ruff check --select I --fix ., do it, headings included.
  • Twelve files carry # flake8: noqa, which ruff treats as a file-wide exemption, so ruff check doesn't check their imports: hyperglass/models/api/rfc8522.py, hyperglass/models/tests/test_system_workers.py, hyperglass/plugins/tests/test_bgp_route_{arista,frr,huawei,juniper,mikrotik}.py, hyperglass/plugins/tests/test_traceroute_{frr,huawei,mikrotik}.py, hyperglass/util/tests/test_query_samples.py and hyperglass/util/tests/test_typing.py. Six of them have imports out of order (ruff check --select I --ignore-noqa .). ruff format still covers them. Replacing the blanket # flake8: noqa with specific codes is a follow-up, kept out of this pull request so the reformat commit stays pure tool output.
  • Follow-up: the taskipy help texts for ui-format and ui-lint still say "Run Prettier" and "Run ESLint"; the UI uses Biome.

Part of #594. The follow-up that records the squash commit in .git-blame-ignore-revs will close it.

…sort

Black and isort were configured but never run in CI, and ruff's import
rule (I001) was ignored, so neither formatting nor import order was
enforced. Ruff now does both: I001 is no longer ignored, the isort
ordering moves to [tool.ruff.lint.isort] (length-sort, known third-party
litestar and inquirer), the isort import headings move to
[tool.ruff.lint.isort.import-heading], and CI runs ruff format --check
next to ruff check. force-exclude applies the existing exclusions to
explicitly passed paths too.

Black, isort and their config are removed, the taskipy format, lint and
sort tasks call ruff, and CONTRIBUTING describes the checks. hooks.sh is
removed: it only held a commented-out isort call and a call to a
validate_examples.py that no longer exists, and nothing referenced it.
The S104 noqa sat at the end of a line holding ten addresses. Ruff format
puts each address on its own line, which would leave the noqa on
240.0.0.1 instead of 0.0.0.0. Split the list in the layout ruff format
produces, with the noqa on the address it covers, so the reformat leaves
it in place.
hyperglass/models/main.py had a second, empty `# Standard Library`
heading above the real one, and hyperglass/util/tests/test_dns_endpoint.py
had a `# Standard Library` heading with no standard library imports
under it. Ruff's import-heading setting only adds or replaces the heading
directly above an import section, so it would leave both in place.
Output of `ruff format .` followed by `ruff check --select I --fix .`
(ruff 0.15.17 from uv.lock), with no other changes. The import fix also
adds the import section headings configured in
[tool.ruff.lint.isort.import-heading] where they were missing.
@jsenecal jsenecal self-assigned this Nov 24, 2026
@github-actions github-actions Bot added docs Improve documentation ci Build, test and release pipelines backend Python backend: API, models, state, execution labels Nov 24, 2026
@coveralls

coveralls commented Nov 24, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 89.198%. remained the same — chore/594-ruff-format into main

@jsenecal
jsenecal marked this pull request as ready for review November 25, 2026 20:23
@jsenecal
jsenecal requested a review from WilhelmZA November 25, 2026 20:23
@jsenecal
jsenecal force-pushed the chore/594-ruff-format branch from 8d51b80 to f16dbf0 Compare November 25, 2026 20:45

@WilhelmZA WilhelmZA left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at f16dbf0.

The configuration, CI step, lock file, taskipy and CONTRIBUTING.md changes are correct, and the reformat commit is tool output only. All commits are signed and CI gate passes.

One change before approval:

  • #594 asks for the reformat commit's hash in .git-blame-ignore-revs. This PR records d651f6d, which a squash merge does not put on main, and git ignores unknown hashes without a warning. Closes #594 would close the issue with that item unmet. Please change the body to Part of #594, or open a tracking issue for the follow-up that records the squash commit and link it from the body.

Comment thread .git-blame-ignore-revs Resolved
@WilhelmZA WilhelmZA added dependencies Pull requests that update a dependency file reviewing Issue is being reviewed by maintainers labels Nov 25, 2026
@jsenecal
jsenecal requested a review from WilhelmZA November 25, 2026 21:34

@WilhelmZA WilhelmZA left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at f16dbf06.

The description now says Part of #594, so the issue stays open for the follow-up that records the squash commit in .git-blame-ignore-revs. All commits are verified and the CI gate passed.

@jsenecal
jsenecal merged commit 973ba6f into main Nov 25, 2026
16 checks passed
jsenecal added a commit that referenced this pull request Nov 25, 2026
#610 listed the reformat commit from its branch, which the squash merge did not put on main, so git blame ignored nothing. Point the entry at the squash commit, 973ba6f. That commit also carries the Ruff configuration changes, so blame skips those few lines too.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Python backend: API, models, state, execution ci Build, test and release pipelines dependencies Pull requests that update a dependency file docs Improve documentation reviewing Issue is being reviewed by maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use ruff for Python formatting and import order, enforced in CI

3 participants