Python Best Practices

Chapter 2 · Practice 2 of 5

Lint in CI and fail the build on new warnings

From Style, formatting and linting

Why it matters

A linter finds bugs that tests miss: unused imports, names that are never defined, mutable default arguments, except blocks that swallow everything. Ruff has over 900 built-in rules and describes itself as a drop-in replacement for Flake8, isort, Black, pyupgrade and others, so one tool replaces a stack of plugins.

A practical setup:

  1. Start from a small rule set: E, F (pyflakes), I (import sorting), B (bugbear).
  2. Run ruff check and ruff format --check in CI.
  3. Add rule families one at a time and fix or ignore their findings in one pull request.

Use # noqa: B008 with the specific code when you deliberately break a rule. A bare # noqa hides every future warning on that line.

Example

Shell
ruff check .
ruff format --check .
ruff check --fix .   # apply safe autofixes locally