|
| 1 | +.. _extending-mypy: |
| 2 | + |
| 3 | +Extending and integrating mypy |
| 4 | +============================== |
| 5 | + |
| 6 | +.. _integrating-mypy: |
| 7 | + |
| 8 | +Integrating mypy into another Python application |
| 9 | +************************************************ |
| 10 | + |
| 11 | +It is possible to integrate mypy into another Python 3 application by |
| 12 | +importing ``mypy.api`` and calling the ``run`` function with a parameter of type ``List[str]``, containing |
| 13 | +what normally would have been the command line arguments to mypy. |
| 14 | + |
| 15 | +Function ``run`` returns a ``Tuple[str, str, int]``, namely |
| 16 | +``(<normal_report>, <error_report>, <exit_status>)``, in which ``<normal_report>`` |
| 17 | +is what mypy normally writes to ``sys.stdout``, ``<error_report>`` is what mypy |
| 18 | +normally writes to ``sys.stderr`` and ``exit_status`` is the exit status mypy normally |
| 19 | +returns to the operating system. |
| 20 | + |
| 21 | +A trivial example of using the api is the following |
| 22 | + |
| 23 | +.. code-block:: python |
| 24 | +
|
| 25 | + import sys |
| 26 | + from mypy import api |
| 27 | +
|
| 28 | + result = api.run(sys.argv[1:]) |
| 29 | +
|
| 30 | + if result[0]: |
| 31 | + print('\nType checking report:\n') |
| 32 | + print(result[0]) # stdout |
| 33 | +
|
| 34 | + if result[1]: |
| 35 | + print('\nError report:\n') |
| 36 | + print(result[1]) # stderr |
| 37 | +
|
| 38 | + print ('\nExit status:', result[2]) |
| 39 | +
|
| 40 | +Extending mypy using plugins |
| 41 | +**************************** |
| 42 | + |
| 43 | +Mypy supports a plugin system that lets you customize the way mypy type checks |
| 44 | +code. This can be useful if you want to extend mypy so it can type check code |
| 45 | +that uses a library that is difficult to express using just PEP 484 types, for |
| 46 | +example. |
| 47 | + |
| 48 | +*Warning:* The plugin system is extremely experimental and prone to change. If you want |
| 49 | +to contribute a plugin to mypy, we recommend you start by contacting the mypy |
| 50 | +core developers either on `gitter <https://gitter.im/python/typing>`_ or on mypy's |
| 51 | +`issue tracker <https://github.com/python/mypy/issues>`_. |
| 52 | + |
0 commit comments