Repository navigation
build(ruff): format and sort imports with ruff instead of Black and isort - #610
Merged
Merged
Conversation
…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
marked this pull request as ready for review
November 25, 2026 20:23
# Conflicts: # CHANGELOG.md
jsenecal
force-pushed
the
chore/594-ruff-format
branch
from
November 25, 2026 20:45
8d51b80 to
f16dbf0
Compare
WilhelmZA
reviewed
Nov 25, 2026
WilhelmZA
left a comment
Collaborator
There was a problem hiding this comment.
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 recordsd651f6d, which a squash merge does not put onmain, and git ignores unknown hashes without a warning.Closes #594would close the issue with that item unmet. Please change the body toPart of #594, or open a tracking issue for the follow-up that records the squash commit and link it from the body.
WilhelmZA
approved these changes
Nov 25, 2026
WilhelmZA
left a comment
Collaborator
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
CONTRIBUTING.mdsaid to format Python with Black and order imports with isort, and both were configured inpyproject.toml, but CI ran onlyruff check ., and ruff's import rule (I001) was in its ignore list. Neither formatting nor import order was enforced, andmainhad drifted:ruff formatwould change 24 files, and the import rule (with the heading setting below) flagged 177 import blocks.Change
Five commits:
build(ruff): format and sort imports with ruff instead of Black and isort: configuration, tooling and docs only.pyproject.toml:I001is no longer ignored (Iwas already selected). New[tool.ruff.lint.isort]withknown-third-party = ["litestar", "inquirer"]andlength-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, andruff checkreports a missing one asI001. Line length stays 100 from[tool.ruff].force-exclude = truemakes 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 bothruff checkandruff format.[tool.black],[tool.isort]and theblackandisortdev dependencies are removed.uv.lock: dropsblack,isortand their now-unused dependenciesmypy-extensions,pathspecandpytokens. Nothing else changes.formatrunsruff format hyperglass,lintrunsruff check hyperglass(it was the oldruff hyperglasssyntax),sortrunsruff check --select I --fix hyperglass.sortstays becauseruff formatdoes not order imports, andruff check --fixalone would also apply every other fixable rule (fixable = ["ALL"]).ruff format --check .right afterruff check .. The backend job is in theCI gatejob'sneeds, so the required check covers it.CONTRIBUTING.mdsays Ruff formats, orders imports and adds the section headings, and that CI checks both, with the commands to run locally.hooks.shis removed. It held only a commented-outisortcall and a call to avalidate_examples.pythat no longer exists, and nothing referenced it.CHANGELOG.md: one entry, see below.test(reverse-dns): put each parametrized address on its own line. A# noqa: S104sat at the end of a line holding ten addresses;ruff formatputs each address on its own line, which would have left the noqa on240.0.0.1and failedruff checkon0.0.0.0. This commit splits the list by hand in the layoutruff formatproduces, with the noqa on0.0.0.0.style: remove two stray import heading comments.hyperglass/models/main.pyhad a second, empty# Standard Libraryabove the real one, andhyperglass/util/tests/test_dns_endpoint.pyhad a# Standard Librarywith 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.style: format with ruff and sort imports: only the output ofruff format .thenruff check --select I --fix ., with ruff 0.15.17 fromuv.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.chore: ignore the ruff reformat in git blame:.git-blame-ignore-revslists 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_globis covered by[tool.ruff] exclude.Verification
Backend suite (
pytest hyperglass --ignore hyperglass/plugins/external -q -p no:cacheprovider -rf, Node 22, same venv):main4670 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 .andruff 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: noqaline (older than this change) sits between# Standard Libraryand the imports.A file without headings fails
ruff check:ruff format --check hyperglass/compat/_sshtunnel.pyskips the file; with--no-force-excludeit 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 lintrun 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
.git-blame-ignore-revswill not exist onmainafter 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.task formatandtask sort, orruff format .andruff check --select I --fix ., do it, headings included.# flake8: noqa, which ruff treats as a file-wide exemption, soruff checkdoesn'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.pyandhyperglass/util/tests/test_typing.py. Six of them have imports out of order (ruff check --select I --ignore-noqa .).ruff formatstill covers them. Replacing the blanket# flake8: noqawith specific codes is a follow-up, kept out of this pull request so the reformat commit stays pure tool output.ui-formatandui-lintstill 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-revswill close it.