Integrating mypy into another Python application

It is possible to integrate mypy into another Python 3 application byimporting mypy.api and calling the run function with a parameter of type List[str], containingwhat normally would have been the command line arguments to mypy.

Function run returns a Tuple[str, str, int], namely(<normal_report>, <error_report>, <exit_status>), in which <normal_report>is what mypy normally writes to sys.stdout, <error_report> is what mypynormally writes to sys.stderr and exit_status is the exit status mypy normallyreturns to the operating system.

A trivial example of using the api is the following

  1. import sys
  2. from mypy import api
  3.  
  4. result = api.run(sys.argv[1:])
  5.  
  6. if result[0]:
  7. print('\nType checking report:\n')
  8. print(result[0]) # stdout
  9.  
  10. if result[1]:
  11. print('\nError report:\n')
  12. print(result[1]) # stderr
  13.  
  14. print('\nExit status:', result[2])