Skip to content
IT Vizag
← Production Python

▶ Sign up free for the video, slides & prompts

The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.

Checking…

Linting and formatting

Lesson 5 of 10 · 2 min read

ruff and black keep code clean automatically.

🎬 Video version coming soon — read it now

Teams don't argue about spaces and quote styles in code review; tools fix them automatically. Formatters make code look consistent. Linters catch likely bugs and bad patterns.

ruff: one fast tool for both

🔒 Code example: sign up free (above) to unlock it.

ruff is written in Rust and checks a large project in well under a second. It combines what used to need several tools (flake8, isort, black and more).

What the linter catches

🔒 Code example: sign up free (above) to unlock it.

Look familiar? Unused imports, the mutable default trap and bare excepts are all bugs you met earlier in this series. The linter finds them automatically.

Configure it in pyproject.toml

🔒 Code example: sign up free (above) to unlock it.

Start with a modest rule set, then add more as the team agrees.

black

black is the formatter that started the "no arguments about style" movement. ruff format produces almost identical output. Many existing projects use black; new ones increasingly use ruff for both jobs.

Automate it

In your editor: install the Ruff extension for VS Code and turn on "format on save". Your code is clean the moment you save.

Before each commit: use pre-commit:

🔒 Code example: sign up free (above) to unlock it.

.pre-commit-config.yaml:

🔒 Code example: sign up free (above) to unlock it.
🔒 Code example: sign up free (above) to unlock it.

Now every git commit runs ruff first and blocks the commit if problems remain.

In CI: run ruff check, ruff format --check, mypy and pytest on every pull request with GitHub Actions, so nothing unchecked reaches the main branch.

Tip: Adding ruff to an old project can show hundreds of warnings. Fix the auto-fixable ones in one commit, then tackle the rest gradually, rule by rule.

In one line

Use ruff to lint and format automatically, configure it in pyproject.toml, and run it on save, on commit and in CI.

Saved on this device. Sign in to keep progress everywhere.

Share with a friend