Code Analysis

Start Python code analysis with Ruff: it lints and formats. Ruff doesn't check types, so add a type checker, and have pre-commit run both on every commit.

How to choose:

  • Linting and formatting: Ruff
  • Type checks that rely on mypy plugins: mypy
  • A type checker from the makers of Ruff: ty
  • Type checking in your editor: Pyright through Pylance in VS Code, basedpyright elsewhere
  • A new checker that reads your existing mypy config: Pyrefly
  • Running your checks on every commit: pre-commit
  • Rules for which modules may import which: Import Linter
  • Dead code or tangled functions: Vulture, or complexipy for complexity
  • Pylint, pycodestyle, and McCabe under one command: Prospector
  • Inference-based checks on code that isn't fully typed: Pylint
  • A lint plugin of your own: Flake8
  • Security scans with a baseline of known issues: Bandit
  • Refactoring: Rope, or ast-grep to rewrite code by pattern

Listed in editorial order, grouped by use case. Click a column header to re-sort the whole list.

Press / to search. Tags open their category. Click a row for details.

Search and filter

Results

Row number Tags

General 4 projects

A linter that enforces architectural constraints on imports between Python modules.
A tool for finding and analyzing dead Python code.
Cognitive complexity analysis for Python code, written in Rust.
A tool to analyze Python code.

Git Hooks 1 project

A framework for managing and maintaining multi-language pre-commit hooks.
pre-commit/github.com/pre-commit/pre-commit /15,724 stars /

Linters and Formatters 6 projects

An extremely fast Python linter and code formatter.
astral-sh/github.com/astral-sh/ruff /51,806 stars /
The uncompromising Python code formatter.
psf/github.com/psf/black /42,262 stars /
A Python utility / library to sort imports.
PyCQA/github.com/PyCQA/isort /6,967 stars /
A fully customizable source code analyzer.
pylint-dev/github.com/pylint-dev/pylint /5,750 stars /
A wrapper around pycodestyle, pyflakes and McCabe.
PyCQA/github.com/PyCQA/flake8 /3,837 stars /
A tool designed to find common security issues in Python code.
PyCQA/github.com/PyCQA/bandit /8,416 stars /

Refactoring 2 projects

Rope is a Python refactoring library.
python-rope/github.com/python-rope/rope /2,250 stars /
A CLI tool for structural code search, linting, and rewriting based on syntax trees.
ast-grep/github.com/ast-grep/ast-grep /16,888 stars /

Type Checkers 5 projects

A static type checker for Python.
python/github.com/python/mypy /20,755 stars /
An extremely fast Python type checker and language server.
astral-sh/github.com/astral-sh/ty /20,528 stars /
Full-featured static type checker for Python from Microsoft, the engine behind Pylance.
microsoft/github.com/microsoft/pyright /15,772 stars /
A pyright fork with stricter defaults and Pylance features in its language server.
A fast type checker and language server for Python.
pyrefly-org/github.com/pyrefly-org/pyrefly /7,290 stars /

Code Analysis guide

Ruff

Pick Ruff as your linter and formatter: its docs say it can replace Flake8 and dozens of its plugins, Black, and isort. Start with the default rule set: it catches common errors like unused imports with zero configuration. Run ruff check to lint and ruff format to format. The formatter is a drop-in replacement for Black, but it isn't meant to be used interchangeably with Black, so pick one per project. Ruff is a linter, not a type checker, and its docs recommend running one alongside it.

mypy

Pick mypy when your code relies on a mypy plugin, since Pyright and ty have no plugin system. Its plugins teach mypy about libraries that are hard to describe with standard type hints. By default, mypy skips functions without annotations, while Pyright and ty check their bodies too. On an existing codebase, its docs say to start small: get mypy passing on one part of the code before you add annotations.

ty

Pick ty when you already run Ruff and want a type checker from the same team, designed for adoption on partially typed code. It doesn't flag missing annotations and infers types where it can. For that check, its docs point you to Ruff's opt-in ANN rules. Run ty check, and it checks every Python file in your project.

Pyright

Pick Pyright for type checking in VS Code, where its docs recommend the Pylance extension. Pylance runs Pyright's checker and adds features like semantic highlighting. Pyright is a standards-based type checker built for large codebases. Start with a minimal pyrightconfig.json that lists the code to check, run Pyright with its default settings, then add annotations as you go.

basedpyright

Pick basedpyright for Pyright's checks with every diagnostic rule on by default, the less severe ones reported as warnings. It also brings Pylance's features to any editor that supports the language server protocol. On an existing project, write a baseline of the current errors and commit it, so only new or modified code reports errors.

Pyrefly

Try Pyrefly on a project that runs mypy or Pyright today. When it finds that configuration and no Pyrefly config of its own, pyrefly check reads it for the run without writing anything to disk. To switch, run pyrefly init, which attempts to migrate that configuration.

pre-commit

Use pre-commit to run your linters, formatters, and type checkers before every commit. It's a multi-language package manager for git hooks, and Ruff, Black, isort, Flake8, and Bandit all document a hook for it. List your hooks in .pre-commit-config.yaml, run pre-commit install, and run them against all files whenever you add one.

Import Linter

Pick Import Linter to keep the parts of your architecture apart: it imposes constraints on the imports between your modules. Write your contracts in a .importlinter file, and lint-imports fails whenever your code breaks one.

Vulture

Pick Vulture to find unused code in a large codebase, from functions and classes to unreachable code. Point it at your files or packages, like vulture myscript.py mypackage/. Run it on your test suite too, and it also finds untested code.

complexipy

Pick complexipy to flag functions whose control flow is hard to follow. It scores cognitive complexity, where each level of nesting adds to a function's score. Run complexipy . on your project and set the score above which a function fails.

Prospector

Pick Prospector to run several analysis tools as one: it brings together Pylint, pycodestyle, and McCabe complexity, with default profiles meant to be useful out of the box. Run prospector from your project root with no arguments, and it figures out the rest with sensible defaults.

Pylint

Run Pylint next to Ruff for the checks its inference finds. Pylint infers the actual values in your code instead of trusting your type hints, so it finds more in code that isn't fully typed. Ruff's docs agree that Pylint does more type inference, like checking the number of arguments in a call.

Flake8

Keep Flake8 only when you depend on a plugin of your own, since Ruff doesn't support custom lint rules and re-implements popular Flake8 plugins in Rust instead.

Bandit

Keep Bandit next to Ruff when security checks need a workflow of their own. Bandit finds common security issues in Python code, and bandit -r scans a whole directory. It can compare a run against a baseline report to skip known non-issues, report only high-severity issues, or run a single profile of tests, like ShellInjection. Its tests are plugins, so you can write your own.

Rope

Pick Rope for refactorings like renaming and moving code: many IDEs and editors use it to refactor Python, so you usually run it through your editor. From Python code, start by making a project for your folder, since refactorings change only the files inside it.

ast-grep

Pick ast-grep to search and rewrite code by syntax-tree patterns instead of text, in Python and many other languages. It also lints with rules you write yourself: ast-grep new scaffolds a project, and ast-grep scan runs all its rules against your repo.

More code analysis tools

For more, see awesome-static-analysis for linters in every language, awesome-flake8-extensions for Flake8 plugins, and awesome-python-typing for type stubs, plugins, and tools.

Know a project that belongs here?

Tell us what it does and why it stands out.