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
Themypy.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]
See the run() documentation for detailed information.
Return value format
Both functions return a tuple of three values:What Mypy normally writes to standard output (normal report)
What Mypy normally writes to standard error (error report)
The exit status Mypy normally returns to the operating system
Basic example
Here’s a simple example of using the Mypy API: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