mypy.api module provides two functions for running Mypy programmatically: run() for the standard type checker and run_dmypy() for the daemon client.
mypy.api.run()
Runs Mypy’s type checker with the specified command line arguments.Function signature
Parameters
Command line arguments to pass to Mypy. These are the same arguments you would pass when running
mypy from the command line.Example: ['--strict', 'myfile.py']Return value
Returns a tuple with three elements:The normal report that Mypy writes to
sys.stdout. Contains type checking results, success messages, and other informational output.The error report that Mypy writes to
sys.stderr. Contains error messages and warnings about the type checking process itself.The exit status code:
0: Success (no errors found)- Non-zero: Errors were found or an issue occurred
Examples
Basic usage
With strict mode
Checking multiple files
Using configuration options
Thread safety
The
run() function is thread-safe. It uses internal mechanisms to capture stdout and stderr without modifying global state.mypy.api.run_dmypy()
Runs the dmypy daemon client with the specified arguments. The dmypy daemon provides faster incremental type checking by keeping analysis state in memory.Function signature
Parameters
Command line arguments to pass to dmypy. Common commands include:
['run', '--', 'file.py']: Start daemon and check files['check', 'file.py']: Check files using existing daemon['status']: Show daemon status['stop']: Stop the daemon
Return value
Returns the same tuple format asrun():
Normal output from the dmypy command
Error output from the dmypy command
Exit status code (0 for success)
Examples
Starting the daemon and checking files
Checking status
Incremental checking
Important limitations
The daemon stores state in a working directory (
.dmypy.json by default). Make sure this location is writable and consistent across calls.Comparison
| Feature | run() | run_dmypy() |
|---|---|---|
| Thread-safe | Yes | No |
| Incremental checking | No | Yes |
| State management | Stateless | Maintains daemon state |
| Performance | Standard | Faster for repeated checks |
| Use case | One-off checks | Development workflows |
Common patterns
Error checking with exit codes
Capturing specific errors
Integration with CI/CD
See also
- Type system API - Working with Mypy’s type classes
- Error codes - Understanding error classification
- Command line reference - All available Mypy options