Skip to content

MyPy

Static type checker for Python, designed to ensure code correctness and catch type errors before runtime.


What is it used for here?

!

  • type checking Python code to catch type errors early
  • enforcing and verifying type annotations
  • checking code quality before every commit and during CI pipelines

How to use it?

  • In this case, the Makefile does the same as running the commands directly via uv. The Makefile is only needed to shorten commands.

!

make typecheck
mypy --config-file=pyproject.toml ./src/
uv run mypy --config-file=pyproject.toml ./src/

How to use it with pre-commit?

To use MyPy with pre-commit, you need to add the appropriate configuration to .pre-commit-config.yaml.

!

.pre-commit-config.yaml
- repo: https://github.com/pre-commit/mirrors-mypy
  rev: v1.16.0
  hooks:
    - id: mypy
      args: [ --config-file=pyproject.toml ]
      files: ^src/
.pre-commit-config.yaml
- repo: local
  hooks:
    # check types
    - id: mypy
      name: Mypy
      description: Run Mypy for type checking types in Python code
      entry: uv run mypy
      types: [python]
      language: system
      always_run: true
      pass_filenames: false
      args: [ --config-file=pyproject.toml, ./src/ ]

Configuration

All configuration for Mypy in this template is located in pyproject.toml under the [tool.mypy] section.


For more details on configuration, see the MyPy Documentation