Skip to content

Commit 7f7032f

Browse files
committed
Reorganize command line documentation and related code
This commit reorganizes our documentation about the command line along with several miscellaneous fixes. In short, I moved the stuff about running mypy/managing imports to a separate file, reorganized the command line section, and tweaked the output of `mypy --help`. Specific major changes include: 1. I split up information about running mypy and information about mypy's command line flags into two separate pages: "Running mypy and managing imports" and "The mypy command line". Rationale: I felt the current documentation on running mypy is very dense: it includes a lot of info on managing imports, how mypy discovers files, etc that I think were really only of interest to advanced programmers who are in the middle of trying to get mypy to run on a complex code base. I think splitting up the text into two makes it easier for both beginners and experts to get the info they're looking for. 2. I rewrote the section about how mypy handles the `--ignore-missing-imports` and `--follow-imports` flags (Now located in "Running mypy..." > "How mypy handles imports"). In particular, it more clearly distinguishes how to handle missing imports vs handling imports to files you didn't want type check: the former is applicable to everybody; the latter is only really applicable to people who are progressively adding type hints. 3. I moved information about how mypy searches the file system and resolves imports to the bottom of the "Running mypy..." section. I think most people don't really need that info to start: running `mypy my_whole_codebase` is generally a good starting point. 4. I reorganized the command line section to mirror how the flags are grouped when you run `mypy --help`. Some minor changes: 1. I switched to using [definition lists][0] instead of bullet points to document flags: I think it looks cleaner. 2. I removed the `mypy --help` summary from the command line page: I don't really think anybody was reading that at all. 3. Similarly, I changed `mypy/main.py` to remove that long summary. It now has a brief "getting started" message along with links to the docs. 4. I added documentations about the `--warn-unused-casts`, `-warn-unused-ignores`, `--show-error-contexts`, `--warn-unsed-configs`, and the different report flags. I did *not* document `--cache-fine-grained` and `--memory-xml-report` mostly because I don't actually understand what exactly does do. 5. I reorganized the flags in `mypy --help` slightly. [0]: https://sphinx-rtd-theme.readthedocs.io/en/latest/demo/lists_tables.html#definition-lists
1 parent 219fbf8 commit 7f7032f

8 files changed

+889
-465
lines changed

docs/source/command_line.rst

Lines changed: 440 additions & 439 deletions
Large diffs are not rendered by default.

docs/source/extending_mypy.rst

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+

docs/source/getting_started.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _getting-started:
2+
13
Getting started
24
===============
35

docs/source/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ Mypy is a static type checker for Python 3 and Python 2.7.
4545
:maxdepth: 2
4646
:caption: Configuring and running mypy
4747

48+
running_mypy
4849
command_line
4950
config_file
5051
mypy_daemon
5152
installed_packages
53+
extending_mypy
5254

5355
.. toctree::
5456
:maxdepth: 2

docs/source/more_types.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ argument list ``index: Union[int, slice]`` and a return type of
537537
``Union[T, Sequence[T]]``. If there are no annotations on the
538538
implementation, then the body is not type checked. If you want to
539539
force mypy to check the body anyways, use the ``--check-untyped-defs``
540-
flag (:ref:`more details here <additional-command-line-flags>`).
540+
flag (:ref:`more details here <untyped-definitions-and-calls-flags>`).
541541

542542
The variants must also also be compatible with the implementation
543543
type hints. In the ``MyList`` example, mypy will check that the

docs/source/revision_history.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ detailed release notes):
173173

174174
* Add :ref:`cheat-sheet-py3`.
175175

176-
* Major update to :ref:`finding-imports`.
176+
* Major update to :ref:`mapping-modules-to-paths`.
177177

178178
* Add :ref:`--ignore-missing-imports <ignore-missing-imports>`.
179179

0 commit comments

Comments
 (0)