Skip to main content
Mypy provides a simple, stable API for using it as part of a Python application. The API allows you to run Mypy’s type checker programmatically and access the results.

Core principles

The Mypy API follows these design principles:
  • Simple and non-intrusive: The API mimics command line activation without starting a new interpreter
  • Immediate compatibility: Changes in the command line version of Mypy are immediately usable through the API
  • No incremental support: The API doesn’t support incremental generation of error messages

Available functions

The mypy.api module provides two main functions:

mypy.api.run()

Runs Mypy’s type checker on the provided arguments. This is the primary way to use Mypy programmatically. Signature: run(args: list[str]) -> tuple[str, str, int] See the run() documentation for detailed information.

mypy.api.run_dmypy()

Runs the dmypy daemon client with the provided arguments. Signature: run_dmypy(args: list[str]) -> tuple[str, str, int]
run_dmypy() is not thread-safe and modifies sys.stdout and sys.stderr during its invocation.
See the run() documentation for detailed information.

Return value format

Both functions return a tuple of three values:
stdout
str
What Mypy normally writes to standard output (normal report)
stderr
str
What Mypy normally writes to standard error (error report)
exit_status
int
The exit status Mypy normally returns to the operating system

Basic example

Here’s a simple example of using the Mypy API:
import sys
from mypy import api

result = api.run(sys.argv[1:])

if result[0]:
    print('\nType checking report:\n')
    print(result[0])  # stdout

if result[1]:
    print('\nError report:\n')
    print(result[1])  # stderr

print('\nExit status:', result[2])

Thread safety

The run() function is thread-safe, but run_dmypy() is not. If you need to run type checking from multiple threads, use run() only.

Next steps

run() function

Learn about the main API functions

Type system

Explore Mypy’s type system classes

Error codes

Understand error classification