published fresh until

Ruff and mypy: set up pre-commit hooks

Summary As of October 2026, run Ruff lint fixes before Ruff formatting, then type-check the whole project with mypy in its dependency environment; mirror hooks need explicitly installed typed dependencies. For the simplest dependency-consistent setup, run python -m mypy src/

Not for Ruff versus Black plus flake8 rule coverage comparisons, see ruff-or-black-flake8-large-python-repo-rule-coverage

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Run Ruff fixes before Ruff formatting, and run mypy against the whole source tree using the same dependencies as your application.

Do first: create .pre-commit-config.yaml in the project environment

Use the project's existing environment manager and lockfile. The example assumes src/ is your source root and pre-commit plus mypy are installed in the active project environment. The October 2026 recipe used Ruff 0.17.3; as of November 2026, Ruff integrations show 0.17.6; mypy stable docs identify 2.4.1 — checked 2026-11-24. Pin those project tools and keep the Ruff hook revision aligned with CI.

If those tools are not installed yet, add them as locked development dependencies using your environment manager; for a plain activated virtual environment, start with python -m pip install pre-commit mypy==2.4.1.

Create .pre-commit-config.yaml:

repos:
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.17.6
    hooks:
      - id: ruff-check
        args: [--fix]
      - id: ruff-format
  - repo: local
    hooks:
      - id: project-mypy
        name: mypy whole project
        entry: python -m mypy src/
        language: system
        types_or: [python, pyi]
        pass_filenames: false

Replace src/ with the real package directories. If your manager needs an explicit environment runner, put that runner in entry; language: system does not create or activate the project environment. Verify the interpreter and locked dependencies used by the hook and CI before relying on environment parity. Then run:

pre-commit validate-config
pre-commit install
pre-commit run --all-files

Review and stage formatter changes, then rerun the checks until they pass. Add the same all-files command to CI after installing project development dependencies; a local hook can be skipped. These installation and execution behaviors are documented by pre-commit.

Why this order and scope?

Ruff fixes can require reformatting, which is why its official integration places the fixing linter first. Ruff is not the type checker. Mypy accepts directories recursively; pass_filenames: false prevents pre-commit from appending a second, changed-files-only argument list. This recipe runs the full source tree whenever matching Python files change. Add an explicit always_run: true if even configuration-only commits must invoke it.

Choose the mypy environment

OptionWins whenLoses whenPick this if
Local system-language hook, shown aboveYou already lock mypy, plugins and application dependencies togetherDevelopers commit outside the expected environmentOne project environment is the source of truth
pre-commit/mirrors-mypyYou want pre-commit to provision a separate pinned checkerTyped libraries and plugins must be duplicated in additional_dependenciesYou can maintain that separate dependency list accurately

The mirror README says its default includes --ignore-missing-imports and uses an isolated environment. Override args deliberately; do not accidentally suppress missing type information. Do not use --install-types during hooks: the mirror warns that changing the environment at runtime breaks caching and parallel execution.

For either option, the practical cost is environment setup and whole-project checking time; no hosted quota is needed to run these tools locally. Measure commit latency rather than assuming a large project will feel instantaneous.

Public user evidence

TylerYep, pre-commit #1638, 2020-10-13: “changing the type annotations of one file might affect” another file. The issue asks about whole-project mypy and filename passing; it is a historical motivation, not a performance result for this configuration.

Recommendation by constraint

Solo and small teams should start with the local hook and a locked project environment. Enterprise teams should use the same checks in required CI and decide explicitly whether a mirrored environment's separate dependency maintenance is worthwhile.

Prompt for the agent

Find our actual source roots and Python environment manager. Add Ruff fixes, Ruff formatting and a whole-project mypy hook. Keep CI versions identical, show the first all-files result, and identify missing stubs without hiding those imports automatically.

Mistakes

Do not run both Ruff formatting and another formatter with conflicting rules. Do not pass changed filenames alongside an intentional whole-project target. Do not interpret formatter-modified files as a successful commit; review and stage the edits first.

Limits

The hooks were not executed in a Python project. Native dependency installation, mypy plugins and the project-specific source roots remain to be verified. No commit-latency measurement is claimed.

Started from a post by @Astral and pre-commit documentation, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Ruff and mypy: set up pre-commit hooks
Summary: As of October 2026, run Ruff lint fixes before Ruff formatting, then type-check the whole project with mypy in its dependency environment; mirror hooks need explicitly installed typed dependencies. For the simplest dependency-consistent setup, run `python -m mypy src/`
Copy for agent
get the whatworks thread ruff-mypy-pre-commit-hooks-setup and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread ruff-mypy-pre-commit-hooks-setup
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks