Sub topic 15
Migrating from Black, Flake8 and isort
Ruff can replace Black, Flake8, isort and several Flake8 plugins with one tool and one configuration section. This chapter shows how to move an existing project over without a noisy diff or a broken CI pipeline.
This chapter assumes you have read the earlier chapters on ruff check, ruff format, rule selection, and configuration in pyproject.toml. It also assumes your project already uses at least one of Black, Flake8 or isort.
What maps to what
Each old tool has a direct Ruff counterpart. The mapping is not one-to-one in every detail, so check each row before you delete anything.
| Old tool | Ruff replacement | Notes |
|---|---|---|
| Black | `ruff format` | Designed to match Black's output. Differences are small but exist. |
| Flake8 (core) | `ruff check` with `E`, `F`, `W`, `C90` | Ruff implements only part of pycodestyle's layout rules. The formatter covers the rest. |
| isort | `ruff check --select I` | Configured under `[tool.ruff.lint.isort]`. |
| flake8-bugbear | `B` rules | Same codes as the plugin. |
| pyupgrade | `UP` rules | Fixes rewrite old syntax. |
| flake8-comprehensions | `C4` rules | Same codes as the plugin. |
| pep8-naming | `N` rules | Same codes as the plugin. |
| flake8-bandit | `S` rules | Often ignored in tests. |
Ruff reuses the original plugins' rule codes where it can. A # noqa: B008 comment written for Flake8 keeps working.
Ruff does not load third-party Flake8 plugins. If your project depends on a custom or niche plugin, you must either keep Flake8 for that plugin alone or drop the checks. Look up each plugin in Ruff's rule list before you remove it.
A safe migration order
Migrate in small steps, and keep formatting changes in their own commit. A mixed commit of formatting changes and config changes is hard to review.
Step 1: Translate the configuration
Here is a typical old setup. Flake8 lives in setup.cfg, and Black and isort live in pyproject.toml.
[flake8]
max-line-length = 88
extend-ignore = E203, W503
max-complexity = 10
exclude = migrations
per-file-ignores =
__init__.py: F401
tests/*: S101
[isort]
profile = black
known_first_party = myapp[tool.black]
line-length = 88
target-version = ["py311"]The Ruff equivalent is one block in pyproject.toml:
[tool.ruff]
line-length = 88
target-version = "py311"
extend-exclude = ["migrations"]
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "C90", "I", "S"]
[tool.ruff.lint.mccabe]
max-complexity = 10
[tool.ruff.lint.isort]
known-first-party = ["myapp"]
[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"]
"tests/*" = ["S101"]
[tool.ruff.format]
quote-style = "double"Notice what changed and what did not carry over:
E203andW503are gone. They exist in Flake8 mostly to stop it from fighting Black. Ruff does not implementW503, and its formatter does not produce the code that triggersE203, so you do not need to ignore them.max-line-lengthbecomesline-lengthat the top level. The formatter and theE501rule both read it.- The
profile = blacksetting disappears. Ruff's import sorting is Black-compatible by default. - Option names use hyphens, not underscores:
known-first-party, notknown_first_party.
After you write the new block, check that Ruff reads what you intended:
ruff check --show-settings . | head -n 40Step 2: Compare lint results
Flake8's default rule set differs from Ruff's. Flake8 selects all of E, F, W and C90 and ignores a short default list. Ruff's default is only E4, E7, E9 and F. If you want Flake8's coverage, select the families you used before.
E501 is worth a decision of its own. Black does not wrap long strings or comments, so a line can exceed the limit after formatting. Flake8 users who kept E501 on will see it keep firing for those lines. You can keep the rule, raise the limit, or leave it out and let the formatter own layout.
Run the linter and look at the counts before you fix anything:
ruff check --statistics .The output lists each rule code with its count. Large counts for one code usually mean a settings mismatch, not a real pile of bugs. For example, thousands of I001 hits mean your isort settings differ from the old ones.
If the results are too noisy to fix at once, record the existing violations and enforce rules only on new code:
ruff check --add-noqa .This appends a # noqa: CODE comment to every line that currently fails. It is a fast way to get CI green, but the comments are debt. Later, enable RUF100 to flag noqa comments that no longer suppress anything. You can then delete them as you fix the underlying code.
Step 3: Check import sorting
Import sorting is the area where Ruff and isort are most likely to disagree. Preview the changes without writing them:
ruff check --select I --diff .Common causes of differences:
- First-party detection. Ruff decides what counts as first-party from the
srcsetting, which defaults to the project root andsrc/. If your package lives elsewhere, setsrc = ["lib"]or list it inknown-first-party. - Section settings. If you used
force_sort_within_sections,combine_as_importsor customsectionsin isort, find the matching key under[tool.ruff.lint.isort]. Ruff names them with hyphens, for exampleforce-sort-within-sections. - Skip comments. Ruff honors
# isort: skip,# isort: offand# isort: on, so existing markers keep working.
When the diff looks right, apply it:
ruff check --select I --fix .Step 4: Run the formatter once, in its own commit
Black and ruff format agree on almost all code, but not on every line. Preview before you write:
ruff format --diff .Review the diff. Typical differences are small: how some multi-line expressions are split, and handling of a few unusual constructs. If the diff is empty or trivial, apply it:
ruff format .Commit this change alone, with a message such as "Format with ruff format". Then list that commit hash in a .git-blame-ignore-revs file so git blame skips it:
git config blame.ignoreRevsFile .git-blame-ignore-revsThe # fmt: off, # fmt: on and # fmt: skip comments from Black are supported, so existing exclusions stay in place.
Step 5: Replace hooks and CI
Replace the Black, Flake8 and isort entries in .pre-commit-config.yaml with the Ruff hooks. Run the linter hook before the formatter hook, so fixes are formatted afterwards.
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 a specific release and update it deliberately. Ruff adds and promotes rules between versions, so an unpinned version can turn CI red without a code change.
In CI, run two read-only commands:
ruff check .
ruff format --check .Use the same Ruff version locally, in pre-commit and in CI. A mismatch is the most common reason a check passes on a laptop and fails in the pipeline.
Step 6: Remove the old tools
Only after CI is green with Ruff should you clean up:
- Delete
black,flake8,isortand everyflake8-*plugin from your dev dependencies. - Delete
[tool.black],[tool.isort],.flake8, and the[flake8]section ofsetup.cfgortox.ini. - Remove old editor settings that still point at Black or Flake8.
- Search the repository for leftover mentions in
Makefile,tox.ini,noxfile.pyand contributor docs.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Hundreds of `E501` errors | Flake8 used `max-line-length` and Ruff has the default | Set `line-length` at the top level |
| Imports reordered everywhere | `src` or `known-first-party` differs from isort | Set them, then re-run `--diff` |
| A plugin's rule has no Ruff code | The plugin is not ported | Keep Flake8 for it, or drop the check |
| Formatter and `ruff check --fix` keep changing the same lines | Rules like `COM812` or `ISC001` overlap with the formatter | Ignore those rules |
| Unused `noqa` comments after migration | Rules changed or were removed | Enable `RUF100` and fix |
Exercise
Your project's setup.cfg has extend-ignore = E203, W503 and [isort] profile = black. After moving to Ruff, which of these settings do you need to carry over, and why?
Show answer
None of them. E203 and W503 were ignored only to stop Flake8 from conflicting with Black. Ruff does not implement W503, and the Ruff formatter does not produce code that triggers E203. The isort profile = black setting is not needed because Ruff's import sorting is Black-compatible by default.
Exercise
You run ruff check --statistics . after migrating and see 2,400 I001 violations. Name two settings to check before running --fix, and the command that shows what the fix would change without writing files.
Show answer
Check src (or known-first-party) to confirm Ruff identifies your own packages as first-party, and check the isort options such as force-sort-within-sections under [tool.ruff.lint.isort]. Preview the changes with ruff check --select I --diff ..