Sub topic 15
Migrating from Black, Flake8 and isort
This chapter assumes you already use Black, Flake8 and isort, can read pyproject.toml, and know the basics of ruff check and ruff format. Ruff replaces all three tools, so a migration mostly means translating their settings and deleting them.
Plan the migration
Do it in small steps so each commit is easy to review.
Map each tool to Ruff
| Old tool | Ruff replacement | Main setting |
|---|---|---|
| Black | `ruff format` | `line-length`, `[tool.ruff.format]` |
| Flake8 | `ruff check` | `select`, `ignore`, `per-file-ignores` |
| isort | `ruff check` with the `I` rules | `[tool.ruff.lint.isort]` |
Translate the configuration
Suppose the project currently uses this setup:
[tool.black]
line-length = 100
[tool.isort]
profile = "black"
line_length = 100Its .flake8 file sets max-line-length = 100, extend-ignore = E203 and per-file-ignores = __init__.py:F401. The Ruff equivalent is:
[tool.ruff]
line-length = 100
[tool.ruff.lint]
select = ["E", "F", "W", "I"]
[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"]Several old settings need no equivalent:
- The isort
profile = "black"setting is unnecessary, because Ruff's import sorting is built to match the formatter. - The
E203ignore existed because Black and Flake8 disagreed about whitespace before a colon in slices. Ruff's formatter and linter do not have this conflict. - Ruff's default rule set is smaller than Flake8's, so
selectmust name the families you want to keep. The oldEandWfamilies are also where the formatter conflicts described in the interplay chapter come from. Check that chapter before enabling them.
Compare the formatter output
Ruff's formatter is designed to match Black's output on Black-formatted code, but small differences exist. Preview them before you write anything:
ruff format --diff .If the diff is small, apply it in one commit with ruff format .. Add that commit's hash to a .git-blame-ignore-revs file so git blame skips it.
Compare the lint results
Run the new configuration and review the output:
ruff check .Existing comments keep working. Ruff honors # noqa and # noqa: CODE comments, isort's # isort: skip and # isort: off markers, and Black's # fmt: off and # fmt: skip markers.
If enabling a rule produces many violations, you can add a # noqa comment to each one and fix them gradually:
ruff check --select I --fix .
ruff check --add-noqaThe first command sorts imports. The second writes a # noqa comment for every remaining violation. Enable RUF100 later to find noqa comments that are no longer needed.
Remove the old tools
After the results match, remove Black, Flake8, isort and their plugins from your dependencies. Delete .flake8, [tool.black] and [tool.isort]. Then update the automation. In .pre-commit-config.yaml, replace the three old hooks with Ruff:
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.14.0
hooks:
- id: ruff-check
args: [--fix]
- id: ruff-formatPin rev to the current Ruff release. List the lint hook before the formatter so fixes are formatted afterward. In CI, replace the three jobs with ruff check . and ruff format --check ..
Exercise
A project's .flake8 file has max-line-length = 79, and its [tool.isort] section has profile = "black". What do you set in Ruff, and what can you drop?
Show answer
Set line-length = 79 under [tool.ruff] and add "I" to select under [tool.ruff.lint]. The isort profile setting is unnecessary, so you can drop it.
Exercise
Which command lists the changes the formatter would make without writing them?
Show answer
ruff format --diff .