Skip to content

Commit 55e13ff

Browse files
author
Guido van Rossum
committed
Document many command line flags.
1 parent a62caad commit 55e13ff

File tree

5 files changed

+204
-3
lines changed

5 files changed

+204
-3
lines changed

docs/source/basics.rst

+5-3
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ The presence or absence of the ``typing`` module does not affect
8787
whether your code is type checked; it is only required when you use
8888
one or more special features it defines.
8989

90-
Type checking and running programs
91-
**********************************
90+
Type checking programs
91+
**********************
9292

9393
You can type check a program by using the ``mypy`` tool, which is
94-
basically a linter it checks your program for errors without actually
94+
basically a linter -- it checks your program for errors without actually
9595
running it::
9696

9797
$ mypy program.py
@@ -102,6 +102,8 @@ to ignore, if you so wish.
102102
The `README <https://github.com/python/mypy/blob/master/README.md>`_
103103
explains how to download and install mypy.
104104

105+
More command line options are documented in :ref:`command-line`.
106+
105107
.. note::
106108

107109
Depending on how mypy is configured, you may have to explicitly use

docs/source/command_line.rst

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
.. _command-line:
2+
3+
The mypy command line
4+
=====================
5+
6+
This section documents many of mypy's command line flags. A quick
7+
summary of command line flags can always be printed using the ``-h``
8+
flag (or its long form ``--help``)::
9+
10+
$ mypy -h
11+
usage: mypy [-h] [-v] [-V] [--python-version x.y] [--py2] [-s] [--silent]
12+
[--almost-silent] [--disallow-untyped-calls]
13+
[--disallow-untyped-defs] [--check-untyped-defs] [--fast-parser]
14+
[-i] [-f] [--pdb] [--use-python-path] [--stats] [--inferstats]
15+
[--custom-typing MODULE] [--html-report DIR]
16+
[--old-html-report DIR] [--xslt-html-report DIR]
17+
[--xml-report DIR] [--txt-report DIR] [--xslt-txt-report DIR]
18+
[--linecount-report DIR] [-m MODULES] [-c COMMAND] [-p PACKAGE]
19+
[files [files ...]]
20+
(etc., too long to show everything here)
21+
22+
Specifying files and directories to be checked
23+
**********************************************
24+
25+
You've already seen ``mypy program.py`` as a way to type check the
26+
file ``program.py``. More generally you can pass any number of files
27+
and directories on the command line and they will all be type checked
28+
together.
29+
30+
- Files ending in ``.py`` (and stub files ending in ``.pyi``) are
31+
checked as Python modules.
32+
33+
- Files not ending in ``.py`` or ``.pyi`` are assumed to be Python
34+
scripts and checked as such.
35+
36+
- Directories representing Python packages (i.e. containing a
37+
``__init__.py[i]`` file) are checked as Python packages; all
38+
submodules and subpackages will be checked (subpackages must
39+
themselves have a ``__init__.py[i]`` file).
40+
41+
- Directories that don't represent Python packages (i.e. not directly
42+
containing an ``__init__.py[i]`` file) are checked as follows:
43+
44+
- All ``*.py[i]`` files contained directly therein are checked as
45+
toplevel Python modules;
46+
47+
- All packages contained directly therein (i.e. immediate
48+
subdirectories with an ``__init__.py[i]`` file) are checked as
49+
toplevel Python packages.
50+
51+
One more thing about checking modules and packages: if the directory
52+
*containing* a module or package specified on the command line has an
53+
``__init__.py[i]`` file, mypy assigns these an absolute module name by
54+
crawling up the path until no ``__init__.py[i]`` file is found. For
55+
example, suppose we run the command ``mypy foo/bar/baz.py`` where
56+
``foo/bar/__init__.py`` exists but ``foo/__init__.py`` does not. Then
57+
the module name assumed is ``bar.baz`` and the directory ``foo`` is
58+
added to mypy's module search path. On the other hand, if
59+
``foo/bar/__init__.py`` did not exist, ``foo/bar`` would be added to
60+
the module search path instead, and the module name assumed is just
61+
``baz``.
62+
63+
If a script (a file not ending in ``.py[i]``) is processed, the module
64+
name assumed is always ``__main__`` (matching the behavior of the
65+
Python interpreter).
66+
67+
Other ways of specifying code to be checked
68+
*******************************************
69+
70+
The flag ``-m`` (long form: ``--module``) lets you specify a module
71+
name to be found using the default module search path. The module
72+
name may contain dots. For example::
73+
74+
$ mypy -m html.parser
75+
76+
will type check the module ``html.parser`` (this happens to be a
77+
library stub).
78+
79+
The flag ``-p`` (long form: ``--package``) is similar to ``-m`` but
80+
you give it a package name and it will type check all submodules and
81+
subpackages (recursively) of that package. (If you pass a package
82+
name to ``-m`` it will just type check the package's ``__init__.py``
83+
and anything imported from there.) For example::
84+
85+
$ mypy -m html
86+
87+
will type check the entore ``html`` package (of library stubs).
88+
89+
Finally the flag ``-c`` (long form: ``--command``) will take a string
90+
from the command line and type check it as a small program. For
91+
example::
92+
93+
$ mypy -c 'x = [1, 2]; print(x())'
94+
95+
will type check that little program (and complain that ``List[int]``
96+
is not callable).
97+
98+
Following imports or not?
99+
*************************
100+
101+
When you're first attacking a large existing code base (without
102+
annotations) with mypy, you may only want it to type check selected
103+
files (for example, the files to which you have added annotations).
104+
While mypy's command line flags don't (yet) help you choose which
105+
files to type check, you *can* prevent it to type check other files
106+
that may be imported from the files and/or packages you are explicitly
107+
passing on the command line. For example, suppose your entire program
108+
consists of the files ``a.py`` and ``b.py``, and ``a.py`` contains
109+
``import b``. Now let's say you have added annotations to ``a.py``
110+
but not yet to ``b.py``. However, when you run::
111+
112+
$ mypy a.py
113+
114+
this will also type check ``b.py`` (because of the import). There
115+
might be errors in ``b.py`` that you don't care to deal with right
116+
now. In this case the ``-s`` flag (``--silent-imports``) is handy::
117+
118+
$ mypy -s a.py
119+
120+
will only type check ``a.py`` and ignore the ``import b``. When you're ready to also type check ``b.py``, you can add it to the command line::
121+
122+
$ mypy -s a.py b.py
123+
124+
or you can of course remove the ``-s`` from the command line::
125+
126+
$ mypy a.py
127+
128+
However these are not quite equivalent! If you keep the ``-s`` flag,
129+
any *other* imports in either ``a.py`` or ``b.py`` (say, ``import
130+
pylons``) will still be ignored silently. On the other hand if you
131+
remove the ``-s`` flag, mypy will try to follow those imports and
132+
issue an error if the target module is not found. Pick your poison!
133+
134+
The behavior of ``-s`` is actually a bit more subtle that that,
135+
though. Even with ``-s``, an import that resolve to a stub file
136+
(i.e. a file with a ``.pyi`` extension) will always be followed. In
137+
particular, this means that imports for which the typeshed package
138+
(see :ref:`library-stubs`) supplies a stub will still be followed.
139+
This is good, because it means mypy will always take the definitions
140+
in stubs into account when it type checks your code. If mypy decides
141+
not to follow an import (because it leads to a ``.py`` file that
142+
wasn't specified on the command line), it will pretend the module
143+
object itself (and anything imported from it) has type ``Any`` which
144+
pretty much shuts up all uses. While that's probably what you want
145+
when you're just getting started, it's also sometimes confusing. For
146+
example, this code::
147+
148+
from somewhere import BaseClass
149+
150+
class MyClass(BaseClass):
151+
152+
def finagle(self) -> int:
153+
return super().finnagle() + 1
154+
155+
probably contains a subtle misspelling of the super method; however if
156+
``somewhere`` is ignored by ``-s``, the type of ``BaseClass`` will be
157+
``Any``, and mypy will assume there may in fact be a ``finnagle()``
158+
method, so it won't flag the error.
159+
160+
For an effect similar to ``-s`` that's a little less silent you can
161+
use ``--almost-silent``. This uses the same rules for deciding
162+
whether to check an imported module as ``-s``, but it will issue
163+
errors for those imports so that you can double-check whether maybe
164+
you should add another file to the command line. This won't directly
165+
flag the error in the above fragment, but it will help you realize
166+
that ``BaseClass`` is not really imported.
167+
168+
Other flags changing what's checked
169+
***********************************
170+
171+
Here are some more useful flags:
172+
173+
- ``--disallow-untyped-calls`` reports an error whenever a function
174+
with type annotations calls a function defined without annotations.
175+
176+
- ``--disallow-untyped-defs`` reports an error whenever it encounters
177+
a function definition without type annotations.
178+
179+
- ``--check-untyped-defs`` is less severe than the previous option --
180+
it type checks the body of every function, regardless of whether it
181+
has type annotations. (By default the bodies of functions without
182+
annotations are not type checked.) It will assume all arguments
183+
have type ``Any`` and alwats infer ``Any`` as the return type.
184+
185+
For the remaining flags you can read the full ``mypy -h`` output.
186+
187+
.. note::
188+
189+
Command line flags are liable to change between releases.

docs/source/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Welcome to Mypy documentation!
2525
supported_python_features
2626
additional_features
2727
planned_features
28+
command_line
2829
faq
2930
revision_history
3031

docs/source/revision_history.rst

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ Revision history
33

44
List of major changes to this document:
55

6+
- May 2016
7+
Publish ``mypy-lang`` version 0.4 on PyPI.
8+
9+
Document :ref:`command-line`.
10+
611
- Feb 2016
712
Publish ``mypy-lang`` version 0.3.1 on PyPI.
813

mypy/main.py

+4
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ def parse_version(v):
187187
"you should really investigate how to obtain stubs for that module.\n"
188188
"See https://github.com/python/mypy/issues/1411 for more discussion."
189189
)
190+
# --silent is deprecated; warn about this.
191+
if args.silent:
192+
print("Warning: --silent is deprecated; use --silent-imports",
193+
file=sys.stderr)
190194

191195
# Check for invalid argument combinations.
192196
code_methods = sum(bool(c) for c in [args.modules, args.command, args.package, args.files])

0 commit comments

Comments
 (0)