| 1 |
.. _mypy_daemon: |
| 2 |
|
| 3 |
.. program:: dmypy |
| 4 |
|
| 5 |
Mypy daemon (mypy server) |
| 6 |
========================= |
| 7 |
|
| 8 |
Instead of running mypy as a command-line tool, you can also run it as |
| 9 |
a long-running daemon (server) process and use a command-line client to |
| 10 |
send type-checking requests to the server. This way mypy can perform type |
| 11 |
checking much faster, since program state cached from previous runs is kept |
| 12 |
in memory and doesn't have to be read from the file system on each run. |
| 13 |
The server also uses finer-grained dependency tracking to reduce the amount |
| 14 |
of work that needs to be done. |
| 15 |
|
| 16 |
If you have a large codebase to check, running mypy using the mypy |
| 17 |
daemon can be *10 or more times faster* than the regular command-line |
| 18 |
``mypy`` tool, especially if your workflow involves running mypy |
| 19 |
repeatedly after small edits -- which is often a good idea, as this way |
| 20 |
you'll find errors sooner. |
| 21 |
|
| 22 |
.. note:: |
| 23 |
|
| 24 |
The command-line interface of mypy daemon may change in future mypy |
| 25 |
releases. |
| 26 |
|
| 27 |
.. note:: |
| 28 |
|
| 29 |
Each mypy daemon process supports one user and one set of source files, |
| 30 |
and it can only process one type checking request at a time. You can |
| 31 |
run multiple mypy daemon processes to type check multiple repositories. |
| 32 |
|
| 33 |
|
| 34 |
Basic usage |
| 35 |
*********** |
| 36 |
|
| 37 |
The client utility ``dmypy`` is used to control the mypy daemon. |
| 38 |
Use ``dmypy run -- <flags> <files>`` to type check a set of files |
| 39 |
(or directories). This will launch the daemon if it is not running. |
| 40 |
You can use almost arbitrary mypy flags after ``--``. The daemon |
| 41 |
will always run on the current host. Example:: |
| 42 |
|
| 43 |
dmypy run -- prog.py pkg/*.py |
| 44 |
|
| 45 |
``dmypy run`` will automatically restart the daemon if the |
| 46 |
configuration or mypy version changes. |
| 47 |
|
| 48 |
The initial run will process all the code and may take a while to |
| 49 |
finish, but subsequent runs will be quick, especially if you've only |
| 50 |
changed a few files. (You can use :ref:`remote caching <remote-cache>` |
| 51 |
to speed up the initial run. The speedup can be significant if |
| 52 |
you have a large codebase.) |
| 53 |
|
| 54 |
.. note:: |
| 55 |
|
| 56 |
Mypy 0.780 added support for following imports in dmypy (enabled by |
| 57 |
default). This functionality is still experimental. You can use |
| 58 |
``--follow-imports=skip`` or ``--follow-imports=error`` to fall |
| 59 |
back to the stable functionality. See :ref:`follow-imports` for |
| 60 |
details on how these work. |
| 61 |
|
| 62 |
.. note:: |
| 63 |
|
| 64 |
The mypy daemon requires ``--local-partial-types``, which is enabled |
| 65 |
by default starting from mypy 2.0. |
| 66 |
|
| 67 |
|
| 68 |
Daemon client commands |
| 69 |
********************** |
| 70 |
|
| 71 |
While ``dmypy run`` is sufficient for most uses, some workflows |
| 72 |
(ones using :ref:`remote caching <remote-cache>`, perhaps), |
| 73 |
require more precise control over the lifetime of the daemon process: |
| 74 |
|
| 75 |
* ``dmypy stop`` stops the daemon. |
| 76 |
|
| 77 |
* ``dmypy start -- <flags>`` starts the daemon but does not check any files. |
| 78 |
You can use almost arbitrary mypy flags after ``--``. |
| 79 |
|
| 80 |
* ``dmypy restart -- <flags>`` restarts the daemon. The flags are the same |
| 81 |
as with ``dmypy start``. This is equivalent to a stop command followed |
| 82 |
by a start. |
| 83 |
|
| 84 |
* Use ``dmypy run --timeout SECONDS -- <flags>`` (or |
| 85 |
``start`` or ``restart``) to automatically |
| 86 |
shut down the daemon after inactivity. By default, the daemon runs |
| 87 |
until it's explicitly stopped. |
| 88 |
|
| 89 |
* ``dmypy check <files>`` checks a set of files using an already |
| 90 |
running daemon. |
| 91 |
|
| 92 |
* ``dmypy recheck`` checks the same set of files as the most recent |
| 93 |
``check`` or ``recheck`` command. (You can also use the :option:`--update` |
| 94 |
and :option:`--remove` options to alter the set of files, and to define |
| 95 |
which files should be processed.) |
| 96 |
|
| 97 |
* ``dmypy status`` checks whether a daemon is running. It prints a |
| 98 |
diagnostic and exits with ``0`` if there is a running daemon. |
| 99 |
|
| 100 |
Use ``dmypy --help`` for help on additional commands and command-line |
| 101 |
options not discussed here, and ``dmypy <command> --help`` for help on |
| 102 |
command-specific options. |
| 103 |
|
| 104 |
Additional daemon flags |
| 105 |
*********************** |
| 106 |
|
| 107 |
.. option:: --status-file FILE |
| 108 |
|
| 109 |
Use ``FILE`` as the status file for storing daemon runtime state. This is |
| 110 |
normally a JSON file that contains information about daemon process and |
| 111 |
connection. The default path is ``.dmypy.json`` in the current working |
| 112 |
directory. |
| 113 |
|
| 114 |
.. option:: --log-file FILE |
| 115 |
|
| 116 |
Direct daemon stdout/stderr to ``FILE``. This is useful for debugging daemon |
| 117 |
crashes, since the server traceback is not always printed by the client. |
| 118 |
This is available for the ``start``, ``restart``, and ``run`` commands. |
| 119 |
|
| 120 |
.. option:: --timeout TIMEOUT |
| 121 |
|
| 122 |
Automatically shut down server after ``TIMEOUT`` seconds of inactivity. |
| 123 |
This is available for the ``start``, ``restart``, and ``run`` commands. |
| 124 |
|
| 125 |
.. option:: --update FILE |
| 126 |
|
| 127 |
Re-check ``FILE``, or add it to the set of files being |
| 128 |
checked (and check it). This option may be repeated, and it's only available for |
| 129 |
the ``recheck`` command. By default, mypy finds and checks all files changed |
| 130 |
since the previous run and files that depend on them. However, if you use this option |
| 131 |
(and/or :option:`--remove`), mypy assumes that only the explicitly |
| 132 |
specified files have changed. This is only useful to |
| 133 |
speed up mypy if you type check a very large number of files, and use an |
| 134 |
external, fast file system watcher, such as `watchman`_ or |
| 135 |
`watchdog`_, to determine which files got edited or deleted. |
| 136 |
*Note:* This option is never required and is only available for |
| 137 |
performance tuning. |
| 138 |
|
| 139 |
.. option:: --remove FILE |
| 140 |
|
| 141 |
Remove ``FILE`` from the set of files being checked. This option may be |
| 142 |
repeated. This is only available for the |
| 143 |
``recheck`` command. See :option:`--update` above for when this may be useful. |
| 144 |
*Note:* This option is never required and is only available for performance |
| 145 |
tuning. |
| 146 |
|
| 147 |
.. option:: --fswatcher-dump-file FILE |
| 148 |
|
| 149 |
Collect information about the current internal file state. This is |
| 150 |
only available for the ``status`` command. This will dump JSON to |
| 151 |
``FILE`` in the format ``{path: [modification_time, size, |
| 152 |
content_hash]}``. This is useful for debugging the built-in file |
| 153 |
system watcher. *Note:* This is an internal flag and the format may |
| 154 |
change. |
| 155 |
|
| 156 |
.. option:: --perf-stats-file FILE |
| 157 |
|
| 158 |
Write performance profiling information to ``FILE``. This is only available |
| 159 |
for the ``check``, ``recheck``, and ``run`` commands. |
| 160 |
|
| 161 |
.. option:: --export-types |
| 162 |
|
| 163 |
Store all expression types in memory for future use. This is useful to speed |
| 164 |
up future calls to ``dmypy inspect`` (but uses more memory). Only valid for |
| 165 |
``check``, ``recheck``, and ``run`` command. |
| 166 |
|
| 167 |
Static inference of annotations |
| 168 |
******************************* |
| 169 |
|
| 170 |
The mypy daemon supports (as an experimental feature) statically inferring |
| 171 |
draft function and method type annotations. Use ``dmypy suggest FUNCTION`` to |
| 172 |
generate a draft signature in the format |
| 173 |
``(param_type_1, param_type_2, ...) -> ret_type`` (types are included for all |
| 174 |
arguments, including keyword-only arguments, ``*args`` and ``**kwargs``). |
| 175 |
|
| 176 |
This is a low-level feature intended to be used by editor integrations, |
| 177 |
IDEs, and other tools (for example, the `mypy plugin for PyCharm`_), |
| 178 |
to automatically add annotations to source files, or to propose function |
| 179 |
signatures. |
| 180 |
|
| 181 |
In this example, the function ``format_id()`` has no annotation: |
| 182 |
|
| 183 |
.. code-block:: python |
| 184 |
|
| 185 |
def format_id(user): |
| 186 |
return f"User: {user}" |
| 187 |
|
| 188 |
root = format_id(0) |
| 189 |
|
| 190 |
``dmypy suggest`` uses call sites, return statements, and other heuristics (such as |
| 191 |
looking for signatures in base classes) to infer that ``format_id()`` accepts |
| 192 |
an ``int`` argument and returns a ``str``. Use ``dmypy suggest module.format_id`` to |
| 193 |
print the suggested signature for the function. |
| 194 |
|
| 195 |
More generally, the target function may be specified in two ways: |
| 196 |
|
| 197 |
* By its fully qualified name, i.e. ``[package.]module.[class.]function``. |
| 198 |
|
| 199 |
* By its location in a source file, i.e. ``/path/to/file.py:line``. The path can be |
| 200 |
absolute or relative, and ``line`` can refer to any line number within |
| 201 |
the function body. |
| 202 |
|
| 203 |
This command can also be used to find a more precise alternative for an existing, |
| 204 |
imprecise annotation with some ``Any`` types. |
| 205 |
|
| 206 |
The following flags customize various aspects of the ``dmypy suggest`` |
| 207 |
command. |
| 208 |
|
| 209 |
.. option:: --json |
| 210 |
|
| 211 |
Output the signature as JSON, so that `PyAnnotate`_ can read it and add |
| 212 |
the signature to the source file. Here is what the JSON looks like: |
| 213 |
|
| 214 |
.. code-block:: python |
| 215 |
|
| 216 |
[{"func_name": "example.format_id", |
| 217 |
"line": 1, |
| 218 |
"path": "/absolute/path/to/example.py", |
| 219 |
"samples": 0, |
| 220 |
"signature": {"arg_types": ["int"], "return_type": "str"}}] |
| 221 |
|
| 222 |
.. option:: --no-errors |
| 223 |
|
| 224 |
Only produce suggestions that cause no errors in the checked code. By default, |
| 225 |
mypy will try to find the most precise type, even if it causes some type errors. |
| 226 |
|
| 227 |
.. option:: --no-any |
| 228 |
|
| 229 |
Only produce suggestions that don't contain ``Any`` types. By default mypy |
| 230 |
proposes the most precise signature found, even if it contains ``Any`` types. |
| 231 |
|
| 232 |
.. option:: --flex-any FRACTION |
| 233 |
|
| 234 |
Only allow some fraction of types in the suggested signature to be ``Any`` types. |
| 235 |
The fraction ranges from ``0`` (same as ``--no-any``) to ``1``. |
| 236 |
|
| 237 |
.. option:: --callsites |
| 238 |
|
| 239 |
Only find call sites for a given function instead of suggesting a type. |
| 240 |
This will produce a list with line numbers and types of actual |
| 241 |
arguments for each call: ``/path/to/file.py:line: (arg_type_1, arg_type_2, ...)``. |
| 242 |
|
| 243 |
.. option:: --use-fixme NAME |
| 244 |
|
| 245 |
Use a dummy name instead of plain ``Any`` for types that cannot |
| 246 |
be inferred. This may be useful to emphasize to a user that a given type |
| 247 |
couldn't be inferred and needs to be entered manually. |
| 248 |
|
| 249 |
.. option:: --max-guesses NUMBER |
| 250 |
|
| 251 |
Set the maximum number of types to try for a function (default: ``64``). |
| 252 |
|
| 253 |
Statically inspect expressions |
| 254 |
****************************** |
| 255 |
|
| 256 |
The daemon allows one to get the declared or inferred type of an expression (or other |
| 257 |
information about an expression, such as known attributes or definition location) |
| 258 |
using the ``dmypy inspect LOCATION`` command. The location of the expression should be |
| 259 |
specified in the format ``path/to/file.py:line:column[:end_line:end_column]``. |
| 260 |
Both line and column are 1-based. Both start and end position are inclusive. |
| 261 |
These rules match how mypy prints the error location in error messages. |
| 262 |
|
| 263 |
If a span is given (i.e. all 4 numbers), then only an exactly matching expression |
| 264 |
is inspected. If only a position is given (i.e. 2 numbers, line and column), mypy |
| 265 |
will inspect all expressions that include this position, starting from the |
| 266 |
innermost one. |
| 267 |
|
| 268 |
Consider this Python code snippet: |
| 269 |
|
| 270 |
.. code-block:: python |
| 271 |
|
| 272 |
def foo(x: int, longer_name: str) -> None: |
| 273 |
x |
| 274 |
longer_name |
| 275 |
|
| 276 |
Here to find the type of ``x`` one needs to call ``dmypy inspect src.py:2:5:2:5`` |
| 277 |
or ``dmypy inspect src.py:2:5``. While for ``longer_name`` one needs to call |
| 278 |
``dmypy inspect src.py:3:5:3:15`` or, for example, ``dmypy inspect src.py:3:10``. |
| 279 |
Please note that this command is only valid after daemon had a successful type |
| 280 |
check (without parse errors), so that types are populated, e.g. using |
| 281 |
``dmypy check``. In case where multiple expressions match the provided location, |
| 282 |
their types are returned separated by a newline. |
| 283 |
|
| 284 |
Important note: it is recommended to check files with :option:`--export-types` |
| 285 |
since otherwise most inspections will not work without :option:`--force-reload`. |
| 286 |
|
| 287 |
.. option:: --show INSPECTION |
| 288 |
|
| 289 |
What kind of inspection to run for expression(s) found. Currently the supported |
| 290 |
inspections are: |
| 291 |
|
| 292 |
* ``type`` (default): Show the best known type of a given expression. |
| 293 |
* ``attrs``: Show which attributes are valid for an expression (e.g. for |
| 294 |
auto-completion). Format is ``{"Base1": ["name_1", "name_2", ...]; "Base2": ...}``. |
| 295 |
Names are sorted by method resolution order. If expression refers to a module, |
| 296 |
then module attributes will be under key like ``"<full.module.name>"``. |
| 297 |
* ``definition`` (experimental): Show the definition location for a name |
| 298 |
expression or member expression. Format is ``path/to/file.py:line:column:Symbol``. |
| 299 |
If multiple definitions are found (e.g. for a Union attribute), they are |
| 300 |
separated by comma. |
| 301 |
|
| 302 |
.. option:: --verbose |
| 303 |
|
| 304 |
Increase verbosity of types string representation (can be repeated). |
| 305 |
For example, this will print fully qualified names of instance types (like |
| 306 |
``"builtins.str"``), instead of just a short name (like ``"str"``). |
| 307 |
|
| 308 |
.. option:: --limit NUM |
| 309 |
|
| 310 |
If the location is given as ``line:column``, this will cause daemon to |
| 311 |
return only at most ``NUM`` inspections of innermost expressions. |
| 312 |
Value of 0 means no limit (this is the default). For example, if one calls |
| 313 |
``dmypy inspect src.py:4:10 --limit=1`` with this code |
| 314 |
|
| 315 |
.. code-block:: python |
| 316 |
|
| 317 |
def foo(x: int) -> str: .. |
| 318 |
def bar(x: str) -> None: ... |
| 319 |
baz: int |
| 320 |
bar(foo(baz)) |
| 321 |
|
| 322 |
This will output just one type ``"int"`` (for ``baz`` name expression). |
| 323 |
While without the limit option, it would output all three types: ``"int"``, |
| 324 |
``"str"``, and ``"None"``. |
| 325 |
|
| 326 |
.. option:: --include-span |
| 327 |
|
| 328 |
With this option on, the daemon will prepend each inspection result with |
| 329 |
the full span of corresponding expression, formatted as ``1:2:1:4 -> "int"``. |
| 330 |
This may be useful in case multiple expressions match a location. |
| 331 |
|
| 332 |
.. option:: --include-kind |
| 333 |
|
| 334 |
With this option on, the daemon will prepend each inspection result with |
| 335 |
the kind of corresponding expression, formatted as ``NameExpr -> "int"``. |
| 336 |
If both this option and :option:`--include-span` are on, the kind will |
| 337 |
appear first, for example ``NameExpr:1:2:1:4 -> "int"``. |
| 338 |
|
| 339 |
.. option:: --include-object-attrs |
| 340 |
|
| 341 |
This will make the daemon include attributes of ``object`` (excluded by |
| 342 |
default) in case of an ``atts`` inspection. |
| 343 |
|
| 344 |
.. option:: --union-attrs |
| 345 |
|
| 346 |
Include attributes valid for some of possible expression types (by default |
| 347 |
an intersection is returned). This is useful for union types of type variables |
| 348 |
with values. For example, with this code: |
| 349 |
|
| 350 |
.. code-block:: python |
| 351 |
|
| 352 |
from typing import Union |
| 353 |
|
| 354 |
class A: |
| 355 |
x: int |
| 356 |
z: int |
| 357 |
class B: |
| 358 |
y: int |
| 359 |
z: int |
| 360 |
var: Union[A, B] |
| 361 |
var |
| 362 |
|
| 363 |
The command ``dmypy inspect --show attrs src.py:10:1`` will return |
| 364 |
``{"A": ["z"], "B": ["z"]}``, while with ``--union-attrs`` it will return |
| 365 |
``{"A": ["x", "z"], "B": ["y", "z"]}``. |
| 366 |
|
| 367 |
.. option:: --force-reload |
| 368 |
|
| 369 |
Force re-parsing and re-type-checking file before inspection. By default |
| 370 |
this is done only when needed (for example file was not loaded from cache |
| 371 |
or daemon was initially run without ``--export-types`` mypy option), |
| 372 |
since reloading may be slow (up to few seconds for very large files). |
| 373 |
|
| 374 |
.. TODO: Add similar section about find usages when added, and then move |
| 375 |
this to a separate file. |
| 376 |
|
| 377 |
|
| 378 |
.. _watchman: https://facebook.github.io/watchman/ |
| 379 |
.. _watchdog: https://pypi.org/project/watchdog/ |
| 380 |
.. _PyAnnotate: https://github.com/dropbox/pyannotate |
| 381 |
.. _mypy plugin for PyCharm: https://github.com/dropbox/mypy-PyCharm-plugin |